DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Testing, Deployment, and Maintenance
  3. Testing, Tools, and Frameworks
  4. Unit Testing With Webpack and Mocha

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.

Vitaliy Zakharov user avatar by
Vitaliy Zakharov
·
Dec. 27, 16 · Tutorial
Like (4)
Save
Tweet
Share
12.02K Views

Join the DZone community and get the full member experience.

Join For Free

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.

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.

unit test Mocha (JavaScript framework) Test suite

Published at DZone with permission of Vitaliy Zakharov. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • How to Submit a Post to DZone
  • API Design Patterns Review
  • 7 Awesome Libraries for Java Unit and Integration Testing
  • What Is a Kubernetes CI/CD Pipeline?

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: