Test units with mocha

Open terminal and execute following commands:

# install mocha globally, if necessary
sudo npm install --global mocha

Go to directory with your project and execute following commands:

cd /your_project_dir

# install mocha as dependency for your project
# if you don't want globally:
# npm install --save-dev mocha

# create test subdirectory
mkdir test

Add test.js file to test subdirectory with following content:

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

Now execute mocha in terminal.

# if global installation
mocha

# if dependency installation
# ./node_modules/mocha/bin/mocha

If all is ok you will see in terminal:

Array
    #indexOf()
      ✓ should return -1 when the value is not present


  1 passing (9ms)

You can add mocha as test script into package.json

{...,
"scripts": {
  "test": "mocha"
}}

In this case, you can run tests from the project directory like this:

npm test