Mocha


Mocha is a popular JavaScript testing framework that is used for running unit and integration tests in Node.js and the browser. It provides a powerful, flexible, and easy-to-use API for writing test cases and organizing them into suites.

Mocha supports a variety of testing styles, including BDD (Behavior Driven Development), TDD (Test Driven Development), and QUnit style. It also supports asynchronous testing and provides built-in support for various assertion libraries such as Chai, Expect, and Should.

Mocha uses a simple and flexible syntax for defining test cases and test suites, making it easy to write and read tests. It provides several hooks that can be used to set up and tear down the test environment, as well as to perform actions before and after each test case or test suite.

Mocha can be easily integrated with various tools and libraries such as Chai, Sinon, Istanbul, and many others. It also has support for running tests in the browser, making it an ideal choice for testing both server-side and client-side code.

Examples

// index.js

function add(x, y) {
  return x + y;
}

module.exports = add;
// test.js

const assert = require('chai').assert;
const add = require('./index');

describe('add function', function() {
  it('should add two numbers', function() {
    assert.equal(add(2, 3), 5);
  });

  it('should return NaN if the input is not a number', function() {
    assert.isNaN(add('a', 'b'));
  });
});

META

Status:: #wiki/notes/mature
Plantations:: Node.JS
References:: ILOG