/xk6-chai

Embed Chai.js (k6chaijs) into the k6 binary

Primary LanguageGoApache License 2.0Apache-2.0

Go Report Card GitHub Actions codecov

xk6-chai

A k6 extension that embeds Chai.js (actually k6chaijs 4.3.4.3) into the k6 binary.

To use the extension, simply change the k6chaijs import path to k6/x/chai. This way, the test will not have runtime external JavaScript dependencies.

import http from 'k6/http';
import { describe, expect } from 'k6/x/chai';

export let options = {
  thresholds: {
    checks: [{ threshold: 'rate == 1.00', abortOnFail: true }],
    http_req_failed: [{ threshold: 'rate == 0.00', abortOnFail: true }],
  },
};

export default function () {
  describe('Get the answer from httpbin', () => {
    const response = http.get('https://httpbin.test.k6.io/get?answer=42');

    expect(response.status, 'response status').to.equal(200);
    expect(response).to.have.validJsonBody();
    expect(response.json().args.answer, 'args.answer').to.equal('42');
  });
}

Note This is a simple wrapper plugin. Making Chai.js k6 compatible is the credit of the k6 team.

Download

You can download pre-built k6 binaries from Releases page. Check Packages page for pre-built k6 Docker images.

Build

You can build the k6 binary on various platforms, each with its requirements. The following shows how to build k6 binary with this extension on GNU/Linux distributions.

Prerequisites

You must have the latest Go version installed to build the k6 binary. The latest version should match k6 and xk6.

  • Git for cloning the project
  • xk6 for building k6 binary with extensions

Install and build the latest tagged version

  1. Install xk6:

    go install go.k6.io/xk6/cmd/xk6@latest
  2. Build the binary:

    xk6 build --with github.com/szkiba/xk6-chai@latest

Build for development

If you want to add a feature or make a fix, clone the project and build it using the following commands. The xk6 will force the build to use the local clone instead of fetching the latest version from the repository. This process enables you to update the code and test it locally.

git clone git@github.com:szkiba/xk6-chai.git && cd xk6-chai
xk6 build --with github.com/szkiba/xk6-chai@latest=.

Docker

You can also use pre-built k6 image within a Docker container. In order to do that, you will need to execute something like the following:

Linux

docker run -v $(pwd):/scripts -it --rm ghcr.io/szkiba/xk6-chai:latest run /scripts/script.js

Windows

docker run -v %cd%:/scripts -it --rm ghcr.io/szkiba/xk6-chai:latest run /scripts/script.js

Example scripts

There are many examples in the scripts directory that show how to use various features of the extension.