w3resource

Introduction to Mocha


Testing is an important aspect of the SDLC (software development life cycle). You really cannot say that you have developed a viable or reliable application if you have not tested it. Today we will introduce you to a very popular test framework, you most likely have seen it as a testing option when creating a Node powered application, be it Angular, Vue, Express etc.

So what is Mocha: Mocha is a feature-rich JavaScript test framework that runs on Node.js and in the browser, it makes asynchronous testing simple and fun. All Mocha test run serially, enabling flexible and accurate reporting, while at the same time mapping uncaught exceptions to the correct test cases

Mocha has lots of features which you will learn about in this tutorial series, they are:

  • browser support
  • auto-exit to prevent "hanging" with an active loop
  • simple async support, including promises
  • easily meta-generate suites & test-cases
  • test coverage reporting
  • config file support
  • string diff support
  • mocha.opts file support
  • javascript API for running tests
  • clickable suite titles to filter test execution
  • proper exit status for CI support etc
  • node debugger support
  • auto-detects and disables coloring for non-ttys
  • detects multiple calls to done()
  • maps uncaught exceptions to the correct test case
  • use any assertion library you want
  • async test timeout support
  • extensible reporting, bundled with 9+ reporters
  • test retry support
  • extensible test DSLs or "interfaces"
  • test-specific timeouts
  • before, after, before each, after each hooks
  • Growl support
  • arbitrary transpiler support (coffee-script etc)
  • reports test durations
  • TextMate bundle
  • highlights slow tests
  • file watcher support
  • global variable leak detection
  • optionally run tests that match a regexp
  • Installation

    You can install mocha globally using npm by running:

    npm install --global mocha
    mocha installation

    or you can add it as a development dependency for your project like this:

    npm install --save-dev mocha

    It should be noted that Mocha requires Node.js V6.0.0 or newer as from Mocha v6.0.0

    Getting started

    In this section we will help you create your first test file.

    npm install mocha
    mkdir test
    //creates a folder called test
     $EDITOR test/test.js # or you open with your favorite editor

    edit the test.js file in your editor to have the content below:

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

    Back inside your terminal run:

    ./node_modules/mocha/bin/mocha
    
      Array
        #indexOf()
          ? has to return -1 when the value is not present
    
    
      1 passing (9ms)

    Then you should set up a test script in package.json:

    "scripts": {
      "test": "mocha"
    }

    Finally run tests with:

    npm test