While writing test scripts, it's common to import different modules, or part of modules, for usage throughout the script. In k6, it is possible to import three different kinds of modules:
These modules are provided through the k6 core, and gives access to the functionality built into k6. This could, for instance, be the http client used for making requests against the system under test. For a full list of built-in modules, see the API documentation.
These modules are stored on the local filesystem, and accessed either through relative or absolute filesystem paths. For a module residing in the local filesystem to be compatible with k6, the module itself may only use relative or absolute filesystem imports to access it's dependencies.
These modules are accessed over HTTP(S), from a source like the k6 JSLib or from any publicly accessible web server. The imported modules will be downloaded and executed at runtime, making it extremely important to make sure the code is legit and trusted before including it in a test script.
JSLib is a set of libraries known to play nicely with k6, and available at https://jslib.k6.io/.
These libraries can either be downloaded and included with the test project or loaded directly using HTTP imports as shown above.
⚠️ Limited compatibility
k6 is not NodeJS, nor is it a browser. Packages that rely on APIs provided by NodeJS, for instance the os and fs modules, will not work in k6. The same goes for browser-specific APIs like the window object.
The steps of this tutorial are as follows:
- Setting up the bundler
- Running the bundling
- Running the tests
A ready-to-use Webpack and Babel starter is available on GitHub.
would be automatically resolved by the node resolution algorithm by searching:
- The current directory
- Any node_modules folder in the directory
- Any node_modules folder in a parent directory, up to the closest package.json file.
As the implementation of import in k6 lacks support for the node module resolution algorithm, node modules that resolve external dependencies will first need to be transformed into a self-contained, isolated, bundle.
This is done with the help of a bundling tool, like Webpack, which analyses the test script, identifies all external dependencies, and then continues to create a self-contained bundle including everything necessary to run the script.
If the test script has no external dependencies, already has them vendored in a k6 compatible way, or only uses ES5.1+ features, using a bundler will not be necessary.
Picking a bundler
Due to its flexibility, ease of use, relatively low resource consumption, and known compatibility with k6, it is recommended to use webpack unless you have a specific reason to choose something else.
Things to consider
In general, all external modules added to a test project have a negative impact on performance, as they further increase the memory footprint and CPU usage.
When bundling using the configuration described in this article, babel and corejs automatically adds the features needed, thus allowing us to run our script without these extensions, using --compatibility-mode=base. For more details on the performance benefits of running in the base compatibility mode, see this article.
Setting up a Babel and Webpack project from scratch might sound like a big undertaking, but is usually accomplished within minutes. Start by creating a project folder and initializing npm:
Then, install the packages needed:
|webpack||The bundler part of Webpack|
|webpack-cli||The CLI part of Webpack, which allows us to use it from the terminal|
|k6||A dummy package used to provide type-ahead in VSCode and similar|
|babel-loader||A loader used by Webpack to leverage babel functionality while bundling|
|@babel/core||The core functionality of Babel|
|@babel/preset-env||A smart preset using browserlist, compat-table and electron-to-chromium to determine what code to transpile and polyfill.|
|core-js||A modular standard library for JS including polyfills|
Once these packages have been added, the next step will be to set up a webpack.config.js file:
Tells Webpack to automatically use the optimizations associated with the mode. Additional details available in the webpack docs.
The files Webpack will use as its entry points while performing the bundling. From these points, Webpack will automatically traverse all imports recursively until every possible dependency path has been exhausted. For instance:
would result in Webpack bundling login.test.js, some.service.js and all upstream dependencies utilized by lodash.
The path key takes an absolute path which is where the finished bundle will be placed. In this example, path.resolve is used to concatenate __dirname and 'dist' into an absolute path.
The libraryTarget key configures how the library will be exposed. Setting it to commonjs will result in it being exported using module.exports. Additional details available in the Webpack docs.
The filename key, as the name suggests, configures the name of the finished bundles. In this example, the template string [name] is used to add a dynamic part to the output filename.
Adding a bundle command
Open the package.json file and add a new script entry, used for running the bundling process.
Running webpack will now output two different test bundles, that may be executed independently:
When running k6 in a Docker container you must make sure to mount the necessary folders from the host into the container, using Docker volumes, so that k6 can see all the JS modules it needs to import.
For example, say you have the following structure on your host machine:
To run index.js and make the modules available for import we execute the following Docker command with the /home/k6/example/src host folder mounted at /src in the container:
Note that on Windows, you also need to make sure that your drive in question, say C:\, has been marked for sharing in the Docker settings: