Wallaby supports native ECMAScript Modules in relevant versions of node.js, if your testing framework supports them.
Please note that:
- If you are using Babel or TypeScript and are compiling your ES modules to CommonJs, then the documentation section below is not relevant to you.
- If you are manually configuring Wallaby.js (not using Automatic Configuration) then you need to be running your tests in
node
and not the browser (i.e.env.type = 'node'
must be set in your Wallaby Configuration file).
Jest
Jest supports ES modules, however the support is still in its early stages. Jest also uses the vm
API (https://nodejs.org/api/vm.html) and currently the ESM parts of this API is still flagged in node (--experimental-vm-modules
).
To run automatically configured Jest with Wallaby and ES modules, you will either need to add wallaby
section that sets the required node flag to your package.json
file:
package.json
{
...
"wallaby": {
"env": {
"params": {
"runner": "--experimental-vm-modules"
}
}
}
}
or to create and use a small wallaby.js configuration file:
wallaby.js
export default function () {
return {
env: {
params: {
runner: '--experimental-vm-modules'
}
}
};
};
Note that the config file has to be ES module if your package.json
has the "type": "module"
setting.
If you are using Jest with manual configuration file, you will need to add the above described env.params.runner
setting to your wallaby config file.
Mocha
Mocha supports ES modules, however watch
does not support ES module test files, so Wallaby needs to be configured to reflect the limitation and to restart its worker processes between test runs:
module.exports = function () {
return {
files: [
'package.json', // IMPORTANT
...
],
tests: [
...
],
env: {
type: 'node'
},
symlinkNodeModules: true, // can be removed if `package.json` contains `"type": "module"`
workers: { restart: true } // IMPORTANT
};
};
Ava
Wallaby uses the vm
API (https://nodejs.org/api/vm.html) to support Ava with ES modules and requires the node.js
flag: --experimental-vm-modules
. If you are using
Ava with ES modules then Wallaby needs to be configured as shown below:
module.exports = function () {
return {
files: [
'package.json', // IMPORTANT
...
],
tests: [
...
],
env: {
type: 'node'
params: {
runner: '--experimental-vm-modules' // IMPORTANT
}
},
workers: { restart: true } // IMPORTANT
};
};