How to Setup Mocha for Software Testing (MochaJS)

This article covers setting up Mocha (MochaJS) for automated software testing.

Step 1. Create a new test project folder

Create a test project folder and initialize it with npm.

If you don’t have npm installed, click here.

On a Mac or Linux machine, open up a terminal window and do the following:

mkdir mocha-101
cd mocha-101
npm init -y

Step 2. Add the mochajs package

Install a copy of the MochaJS package as a dev dependency in your project.

You should install it as a dev dependency. That’s because it is not needed when you package your code for distribution.

npm install --save-dev mocha

To verify that it was installed, run the following command:

cat package.json

Look for a section called devDependencies. It should contain an entry for mocha.

Step 3. Add a test script to package.json

Add an npm script entry to run mocha by doing the following:

  • Edit package.json
  • Update the scripts/test section of package.json to call mocha:
  "scripts": {
    "test": "mocha"
  },
  • Save the file

Be careful not to use a text editor that may replace the double quotes with smart quotes. That could lead to compile errors.

Step 4. Create a test folder

By default, MochaJS will look for test files in a subfolder called test.

mkdir test

Step 5. Create a test file

Add a new test file to the test folder.

touch test/smoke-test.js

Step 6. Add a test

Paste the contents below into test/smoke-test.js and save the file:

"use strict";

describe('smoke test', function() {
	it('should pass if mocha is working', function(done) {
		done();
	});
});

MochaJS supports a variety of testing formats. In this case the format is BDD (Behavior Driven Development). It uses the keyword describe - to describe what is being tested. Then it uses it to describe what behavior ‘it’ is being tested for - in a human readable format.

Because all that our test is doing is making sure that the setup works, the done() function is called to end the test. If there were no fails and done is called with no arguments, the test passes.

Step 7. Run the test

In an earlier step, I had you update a script entry called test in package.json. The script was setup to run mocha. This is how you can run it.

npm test

You should see output like this:

  smoke test
    ✓ should pass if mocha is working

  1 passing (4ms)

Step 8. Add a failing test

To see what a fail looks like, add a failing test to the file:

"use strict";

describe('smoke test', function() {
	it('should pass if mocha is working', function(done) {
		done();
	});
	it('should fail a test on error', function(done) {
		throw new Error('This test should fail!');
		done();
	});
});

Step 9. Run with the failing test

Run the smoke test again:

npm test

You should see output like this:

  smoke test
    ✓ should pass if mocha is working
    1) should fail a test on error

  1 passing (5ms)
  1 failing

  1) smoke test
       should fail a test on error:
     Error: This test should fail!
      at Context.<anonymous> (test/smoke-test.js:8:9)
      at processImmediate (internal/timers.js:439:21)

npm ERR! Test failed.  See above for more details.

This line tells you that the failure happened at line 8, where the error was thrown:

at Context.<anonymous> (test/smoke-test.js:8:9)

Step 10. Skip a failing test

If you skip all tests that fail, that sort of defeats the purpose of automating the tests!

But sometimes you end up with a “flakey” test that needs work. Or it is agreed that the feature you are testing still needs work. You don’t want to just delete the whole test. In that case you can mark it for skipping.

Update the smoke test to skip a failing test by changing the second it to it.skip:

"use strict";

describe('smoke test', function() {
	it('should pass if mocha is working', function(done) {
		done();
	});
	it.skip('should fail a test on error', function(done) {
		throw new Error('This test should fail!');
		done();
	});
});

Step 11. Run with the skipped test

Run the smoke test again:

npm test

You should see output like this:

  smoke test
    ✓ should pass if mocha is working
    - should fail a test on error

  1 passing (5ms)
  1 pending

The skipped test is now flagged as pending instead of failing.

Troubleshooting

Mocha not found

If for some reason your npm test script can’t find mocha, try coding a relative path like this:

  "scripts": {
    "test": "./node_modules/mocha/bin/mocha"
  },

When you installed mocha via npm, it put the package under a subfolder called node_modules. The executable binary is in that packages bin subfolder.

Conclusion

In this article I showed you how to install and run MochaJS for software testing.

You learned how to:

  • setup a project for testing
  • install the MochaJS package as a dev dependency
  • write a test suite
  • write a passing test
  • write a failing test
  • skip a test

For this introduction I decided to keep things simple and focus on the framework only. I will cover testing actual components and using assertion libraries in other articles.

References

  • Get npm [1]
  • MochaJS Interfaces [2]
  • Behavior-Driven Development [3]



About the Author

Mitch Allen tests cloud services for a robotics company in New England.