k6chaijs is a library to provide BDD assertions in k6 based on ChaiJS. You can use k6chaijs as an alternative to check and group.
With this library, you get the following:
- BDD style of assertions for more expressive language
- chainable assertions
- more powerful assertions functions such as: deep, nested, ordered, etc.
- automatic assertion messages
- exception handling for better test stability
Installation
There's nothing to install. This library is hosted on jslib and can be imported in the k6 script directly.
Alternatively, you can use a copy of this file stored locally. The source code is available on GitHub.
Example
The following example tests a hypothetical HTTP API that returns a JSON array of objects. Copy the following code, and save it as script.js:
When you run this test with k6 run script.js, the output at the end of the test shows:
If you are familiar with k6, the result is the same as using check and group. Note that expect might add or extend the assertion message.
API
API | Description |
---|---|
config | Options to change k6chaijs behaviour. |
describe | A wrapper of group that catches exceptions to allow continuing the test execution. It returns a boolean to indicate the success of all its k6chaijs assertions. |
expect | A wrapper of check that provides BDD style of assertions. |
Plugins
It's possible to extend the default functionality with Chai plugins. To use a plugin or build a Chai version with plugins, follow the instructions in this example.