In this post I’m going to list the steps for setting up a Node.js project with unit testing.
The Tools
Mocha
Mocha is a JavaScript test framework. You can use Mocha in both server (Node.js) and the browser. Given the asynchronous nature of JavaScript programming model in both Node.js and browser, Mocha provides a neat syntax for performing asynchronous test.
Chai
Chai is a BDD/TDD assertion library.
Chai comes in two flavors, BDD and the clasical TDD assert style. In this how to, I’m going to use the ‘Should’ (BDD) interface, since it’s the one I enjoy the most.
Prerrequisites
Node.js >=0.12 and npm
Setting up your project
- Create a new directory. In your terminal run:
mochachaitest && cd mochachaitest
- Initialize a new Node project. Run ‘npm init’, and follow the steps.
npm init name: (mochachaitest) version: (1.0.0) description: entry point: (index.js) test command: git repository: keywords: author: Otto Giron license: (ISC) MIT
- Install Mocha and Chai. In your terminal run:
npm install mocha chai --save-dev
Note that I’ve added the ‘–save-dev’ flag at the end, since these dependencies will be useful only in your development environment.
Creating a simple test file
By default, Mocha runs *.js test files inside a test folder under your root directory (mochachaitest).
- Create a test folder
mkdir test
- Create a file ‘first-test.js’ under test folder with the following content
// require chai and initialize 'shoud()' var should = require('chai').should(); //Describe the purpose of your unit tests suite describe('Showcase of mocha and chai', function() { it('should show an example of synchronous unit test', function() { //simple test var sum = 1 + 1; //after initializing chai.should() every variable in //this scope has the special property 'should' sum.should.be.equal(2); }); //For an asynchronous test we can add the especial parameter 'done' (you can use any name) // which is a callback that informs mocha when the test has finished it('should show an example of an asynchronous unit test', function(done) { //some async call setTimeout(function() { //simple test var sum = 1 + 1; sum.should.be.equal(2); done(); //inform mocha that this test is done }, 1000); }); });
Running your tests
- In your package.json edit scripts.test property and set the following value
//package.json file ... "scripts": { "test": "node ./node_modules/.bin/mocha" }
- In your terminal run ‘npm test’
npm test > mochachaitest@1.0.0 test /home/otto/tutorial/mochachaitest > node ./node_modules/.bin/mocha Showcase of mocha and chai ✓ should show an example of synchronous unit test ✓ should show an example of an asynchronous unit test (1005ms) 2 passing (1s)
This will run the script you’ve just added to the package.json file.
Conclusion
There are several options for implementing your tests in Node.js. Check HapiJS Lab it is another test framework I liked. You can choose the option that best suit to your needs, style, and existing tooling. When reviewing your options you might take into account features such asynchronous support, compatible assertion libraries, options for tests code coverage and code style (eslint, jslint etc).
Code for this example is available on github https://github.com/ottogiron/mochachaitest