Unit Testing With Webpack and Mocha
After moving our build infrastructure to Webpack, we defined our requirements for the testing infrastructure and created a setup.
Join the DZone community and get the full member experience.
Join For FreeAfter moving our build infrastructure to Webpack, one of the hurdles we had to overcome was finding a good way to run unit tests. Quite a few tutorials and how-tos are available for using mocha with Webpack, but none of them gave us all the things we wanted from our test setup.
Therefore, as described below, we defined our requirements for the testing infrastructure and created a setup that enables us to build a test bundle from any number of spec files in a directory, incrementally rebuild on each spec change, and rerun the test suite via mocha on every change.
Test Requirements
We wanted our tests to do the following.
Run Fast
First and foremost, unit tests need to be fast to create and run. It’s hard to argue against the idea that the best time to write unit tests is during development, growing the suite alongside the code. Writing tests after the code is written becomes a chore, and if the suite also takes too long to run, then it’s only natural that code coverage will suffer. I want to be able to write a test, see it fails immediately, write the code, and just as promptly see it pass. Rinse and repeat.
Run in the Terminal
There are legitimate reasons for running unit tests in a browser. For performance and portability, however, we’d rather run them in the terminal.
Use Automatic File Globbing
This one is not an obvious problem at first, but including a whole directory tree of files in Webpack requires a few tricks (i.e., I want to create a *.spec.js file in a directory, and have my suite run it immediately).
The Solution
To address our requirements, we created a master test suite file that will include all tests in the specified directory.
All-tests.js:
var context = require.context('./tests', true, /\.js$/);
context.keys().forEach(context);
module.exports = context;
This will include all js files in the tests subdirectory into the Webpack build.
Now we can do something like:
webpack --entry all-test.js --output-file testBundle.js
However, we still need to be able to run our unit tests after our build finishes. For that purpose, we are going to use webpack-shell-plugin.
npm install --save-dev webpack-shell-plugin
Then, we need to configure Webpack to use the plugin. Let’s create a Webpack config file to include the plugin, as well as persist our entry and output files. We are also including the webpack-node-externals file so our webpack build doesn’t bundle node system files.
webpack-test.config.js:
var webpack = require('webpack');
var nodeExternals = require('webpack-node-externals');
var WebpackShellPlugin = require('webpack-shell-plugin');
var config = {
entry: 'all-tests.js',
output: {
filename: 'testBundle.js'
},
target: 'node',
externals: [nodeExternals()],
node: {
fs: 'empty'
},
plugins: [
new WebpackShellPlugin({
onBuildExit: "mocha testBundle.js"
})
]
};
module.exports = config;
Now we should be able to run:
webpack -w --config webpack-test.config.js
Note the -w param, which instructs Webpack to watch for file changes in our test directory as well as do incremental rebuilds. Now our build will execute mocha runner on every test change.
The Results
With this setup, we can build a test bundle from any number of spec files in a directory, incrementally rebuild on each spec change, and rerun the test suite via mocha on every change. Have a look at this working example.
Published at DZone with permission of Vitaliy Zakharov. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments