w3resource

Introduction to Mocha


Introduction to Mocha

Testing is a crucial aspect of the software development life cycle (SDLC). An application cannot be considered viable or reliable without thorough testing. This tutorial introduces Mocha, a popular testing framework often encountered when developing Node.js applications, including those built with Angular, Vue, Express, etc.

What is Mocha?

Mocha is a feature-rich JavaScript testing framework that runs on Node.js and in the browser, making asynchronous testing simple and enjoyable. Mocha tests run serially, allowing for flexible and accurate reporting while mapping uncaught exceptions to the correct test cases.

Key Features of Mocha:

Mocha boasts numerous features, which you will explore throughout this tutorial series. Some of its notable features include:

  • Browser support
  • Auto-exit to prevent "hanging" with an active loop
  • Simple asynchronous support, including promises
  • Easily meta-generate suites & test cases
  • Test coverage reporting
  • Configuration 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
  • 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 prefer
  • Asynchronous test timeout support
  • Extensible reporting, bundled with 9+ reporters
  • Test retry support
  • Extensible test DSLs or "interfaces"
  • Test-specific timeouts
  • 'before', 'after', 'beforeEach', 'afterEach' hooks
  • Growl support
  • Arbitrary transpiler support (e.g., CoffeeScript)
  • Reports test durations
  • TextMate bundle
  • Highlights slow tests
  • File watcher support
  • Global variable leak detection
  • Optionally run tests that match a regexp

Installation

  • To install Mocha globally using npm, run:
  • npm install --global mocha
    

    Alternatively, you can add it as a development dependency for your project:

    npm install --save-dev mocha
    

    Please note that Mocha requires Node.js v6.0.0 or newer from Mocha v6.0.0 onwards.

    Getting Started

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

    1. Install Mocha:
    2.    npm install mocha
       
    3. Create a 'test' folder:
    4.    mkdir test
      
    5. Create a test file:
      Open your favorite editor and create `test/test.js` with the following content:
    6.    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);
             });
           });
         });
      
    7. Run your tests:
      Back in your terminal, run:
         ./node_modules/mocha/bin/mocha
        
      You should see the following output:
      Array
      #indexOf()
      should return -1 when the value is not present
      1 passing (9ms)
    8. Set up a test script in `package.json`:
    9.    "scripts": {
           "test": "mocha"
         }
      
    10. Finally, run your tests with:
    11.    npm test
       

    Continue with the tutorial series to learn more about Mocha's powerful features and how to leverage them in your projects.

    

    Follow us on Facebook and Twitter for latest update.