🧪
Testing8M+/wkMIT

mocha

Mocha is a feature-rich JavaScript test framework that runs on Node.js and in the browser, providing a flexible foundation for writing and organizing tests. Unl

Installation

npm
npm install -D mocha
yarn
yarn add -D mocha
pnpm
pnpm add -D mocha

Import

ESM
// mocha tests use globals: describe, it

Quick Example

usage
import assert from 'assert';

describe('Array', () => {
  describe('#indexOf()', () => {
    it('should return -1 when not found', () => {
      assert.strictEqual([1, 2, 3].indexOf(4), -1);
    });
  });
});

About mocha

Mocha is a feature-rich JavaScript test framework that runs on Node.js and in the browser, providing a flexible foundation for writing and organizing tests. Unlike Jest's all-in-one approach, Mocha focuses on being an excellent test runner while allowing developers to choose their preferred assertion library (Chai, Should.js, expect.js, or Node's built-in assert), mocking framework (Sinon, testdouble), and coverage tool (Istanbul/nyc). Mocha provides BDD (describe/it) and TDD (suite/test) interfaces, configurable timeouts, before/after hooks at suite and test levels, grep filtering for running specific tests, and support for parallel test execution. The framework handles asynchronous testing through callbacks, Promises, and async/await with automatic detection. Mocha's reporter system includes built-in spec, dot, nyan, and JSON reporters, with numerous third-party reporters available. Mocha's flexibility and maturity make it popular in enterprises and projects with specific testing stack requirements. The framework supports test retries, root-level hooks, pending tests, and exclusive/inclusive test running via .only() and .skip().

Quick Facts

Packagemocha
CategoryTesting
Weekly Downloads8M+
LicenseMIT
Installnpm install -D mocha

Related Packages

Browse npm Packages by Category

Explore our reference of 200 popular npm packages with install commands, examples, and quick-start guides.