How To Use Mocha for Node Testing in Windows (2017)

Using Mocha on Windows is so much easier these days!  The previous post on this topic showed how to get past the Linux-specific instructions in Mocha’s Getting Started.  That approach is outdated, and using Mocha on Windows is now a breeze.  So, let’s see how easy it is:

After creating directory for your project, use npm to create a package.json file. (You can skip this step if adding mocha to an existing project)

npm init

You’ll be presented with several options. For this example, accept defaults for all except test command which you’ll type mocha.  Accept the resulting json displayed and your package.json will be written to disk.

If you started with an existing project, i.e., package.json already existed, you just need to set test to mocha in the scripts section.

Once the package.json exists, install mocha.  The save-dev option puts mocha in the devDependencies section rather dependencies.

npm install –save-dev mocha

Now you’re ready to create a test!  Create a subdir test and use your favorite editor to create test.js there.

var assert = require('assert');
describe('Array', function() {
  describe('#indexOf()', function() {
    it('should return -1 when the value is not present', function() {
      assert.equal(-1, [1,2,3].indexOf(4));

After saving the file, ensure all packages are up to date.  Go back to the project directory (above test) and run:

npm update –dev

The dev option causes devDependencies to be processed, too.

That’s it!  Now just run the test:

npm test

The output will look like this:

    ✓ should return -1 when the value is not present

 1 passing (9ms)

This site uses Akismet to reduce spam. Learn how your comment data is processed.