Mocha Test Framework

11.7.5 · active · verified Sat Apr 18

Mocha is a feature-rich JavaScript test framework for Node.js and browsers, designed to make asynchronous testing simple and enjoyable. It offers a flexible base for writing tests and integrates well with various assertion libraries. The current stable version is 11.7.5, with version 12 actively in beta, demonstrating continuous development and frequent updates.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates a basic Mocha test suite using `describe` and `it` with the Chai assertion library. To run, save this code as `test.ts`, install dependencies (`npm install mocha chai ts-node`), and execute with `npx mocha --require ts-node/register test.ts`.

import { describe, it } from 'mocha';
import { expect } from 'chai';

describe('Array', () => {
  it('should return -1 when the value is not present', () => {
    expect([1, 2, 3].indexOf(4)).to.equal(-1);
  });

  it('should return the correct index when present', () => {
    expect([1, 2, 3].indexOf(2)).to.equal(1);
  });
});

view raw JSON →