# How to build plugins for ChaiJS?

**URL:** <https://community.grafana.com/t/how-to-build-plugins-for-chaijs/97010>\
**Category:** OSS Support\
**Created:** [November 1, 2022, 3:48pm UTC](https://community.grafana.com/t/how-to-build-plugins-for-chaijs/97010 "2022-11-01T15:48:34Z")\
**Posts on this page:** 1\
**Showing post:** 3

<div class="post-metadata">

**Author:** ![imiric](https://sea2.discourse-cdn.com/grafana/user_avatar/community.grafana.com/imiric/32/36245_2.png) [@imiric](https://community.grafana.com/u/imiric)\
**Post date:** [November 1, 2022, 5:30pm UTC](https://community.grafana.com/t/how-to-build-plugins-for-chaijs/97010/3 "2022-11-01T17:30:16Z")

</div>

Hi there!

Sorry, this is not well documented. 😞

Here are the steps that worked for me:

1. Clone the [k6chaijs repository](https://github.com/grafana/k6-jslib-k6chaijs) locally.

2. Install the Chai plugin you wish to use. E.g. `npm i chai-match`. `--save-dev` is not strictly necessary, unless you want to build your own version of k6chaijs. I’ll explain why you might want to do that below.

3. In your k6 script, import both `chai` and `chaiMatch`, and setup Chai to use the plugin. Since k6 doesn’t support Node’s `require()` function, the syntax is a bit different than what’s suggested on the [chai-match page](https://www.chaijs.com/plugins/chai-match/).

4. Then in your k6 `default` function, you can use the plugin as usual. The `capture()` examples in the documentation worked for me.

So while this would work, it’s slightly cumbersome to ship, since you need to import directly from `node_modules`, and setup Chai to use the plugin in every k6 script.

An alternative approach would be to build your own k6chaijs version that bundles any plugins you need.

This is slightly more complicated:

1. Run `npm install` in the kchaijs directory, to install all dependencies.

2. Install any Chai plugins you need. `--save-dev` is important here, so for chai-match you would run `npm i --save-dev chai-match`.

3. Change `src/index.ts` to setup the plugins you need. For example:

4. Run `npm run-script build` to build your k6chaijs bundle. It will be available in the `./build` directory.

5. Then in your k6 script you can just import `describe` and `expect`:

The advantage of the second aproach is that you can just move the `k6chaijs.min.js` file around along with your k6 script, and it will have everything you need.

> Should I use webpack

Webpack is not necessary in this case, since k6chaijs already uses esbuild, which is all we need to create the JS bundle. It’s specified as a dev dependency in the `package.json`, so `npm run-script build` should work without you installing it separately. (If it doesn’t, make sure that `node_modules/.bin` is specified in your `$PATH` environment variable.)

Hope this helps, and we’ll make sure to update the documentation and explain this in more detail.

---

_[View the full topic](https://community.grafana.com/t/how-to-build-plugins-for-chaijs/97010)._
