Starting with Mocha for Testing Node.js Modules

Mocha is our choice of testing framework for Node.js modules. We specially like the ease it provides for testing asynchronous executions that gives the Node.js its powers but may be challenging when testing. In addition, the ability to write and run tests with the TTD interface was another factor when choosing Mocha. In this post, we cover installation, configuration and writing tests with Mocha.

1. Install and Declare Dependency
To install:

sudo npm install mocha

This install the mocha module locally. Add ‘-g’ tag if you would like to be installed globally.

Afterwards, declare the dependency, so other projects consuming this module can run the tests by adding the following in ./package.json

...
 "dependencies": {
...
     "mocha": "~1.10.0"
}

2. Configure Mocha interface, etc
There are two ways configure Mocha
a)Manually by editing ./test/mocha.opts file

--require should
--reporter spec
--ui tdd

Here we specify Mocha to include ‘should’ module for the tests. We also specify the reporting and ui interface. We found the reporting type of ‘spec’ to be most descriptive while just enough verbose. The ‘–ui’ specifies the interface to be ‘TDD’ for writing tests.

b)Configure via Command
Alternative is to configure Mocha testing framework via command line:

mocha --ui tdd
mocha --reporter spec
mocha --require should

3. Enable Async Testing
Mocha makes it simple for testing async execution. All you have to do is pass paramater ‘done’ to the test function and then call ‘done()’ afterwards as following:

suite(...
...
test('ensure its saved in redis',function(done){
            db.hgetall('review:1', function(err, redisReview){
                if(err){
                    console.log('error when retrieving value');
                    done();
                }
                redisReview.should.have.property('id');
                redisReview.should.have.property('attOne');
                redisReview.should.have.property('attTwo');
                done();
            });
        });
...
);

The ‘done’ is the Mocha callback function. In the first highlighted line, it is passed into the test. Afterwards, it is called to let Mocha know the particular async process has completed

3. Running Test in CoffeeScript
If your module is developed in CoffeeScript instead JavaScript, then the first line of the test file has to include CoffeeScript as following:

require('coffee-script');
...

This is common practice for running CoffeeScript code in Node.js not only for Mocha test framework
Make sure to install CoffeeScript module and declare it as the dependency.

4. Running With “–watch” Parameter
if you want to run the test whenever the changes is made, then use the ‘–watch’ tag

Setup vs SuiteSetup
For TDD interface, the ‘Setup’ block is executed multiple times for each test declared. If you need to have only one time setup then ‘SuiteSetup’ is the solution. The same applies for teardown() and suiteTearDown()

Issues:

1. ReferenceError: suite is not defined
a)Make sure the proper TDD Interface has been configured mocha.opts(step-2)

2. ReferenceError: assert is not defined
It seems that configuring to import ‘assert’ from the configuration file doesn’t work. Fix is in the test file include it manually as following:

var assert = require('assert');

Useful Links

  • http://visionmedia.github.io/mocha/#mocha.opts
  • http://stackoverflow.com/questions/9795254/nodejs-mocha-suite-is-not-defined-error
  • http://net.tutsplus.com/tutorials/javascript-ajax/better-coffeescript-testing-with-mocha/
  • http://stackoverflow.com/questions/12159846/testing-asynchronous-function-with-mocha
  • http://codebetter.com/glennblock/2013/01/17/debugging-mocha-unit-tests-with-webstorm-step-by-step/

One thought on “Starting with Mocha for Testing Node.js Modules

  1. some how i am not able to run mocha.
    1) I have installed mocha globally using npm install -g mocha.
    2) when run mocha –version . It just hangs here nothing happens…interestingly when i run _mocha –version . It shows version 1.12.0
    I would like to know what is the issue with mocha command.
    when i run only mocha nothing happens but when i use _mocha. it shows 0 passed result. somehow _mocha is working not mocha. I have looked at both file. both files looks quite similar.
    your help is appreciated.

Leave a Reply

Your email address will not be published. Required fields are marked *