Live Demo
Blog   >   DevSecOps   >   Unit Testing With Webpack & Mocha

Unit Testing With Webpack & Mocha

After 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:

1. 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 fail immediately, write the code, and just as promptly see it pass. Rinse and repeat.

2. 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.

3. 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.


var context = require.context('./tests', true, /.js$/);
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

But 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.


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.