Mocha
Overview
Mocha is a feature-rich JavaScript test framework that runs on Node.js and in the browser, making asynchronous testing simple. It provides the test structure (describe/it), hooks, and reporters but leaves assertions to a library of your choice (commonly Chai). Tests run serially for accurate reporting and correct mapping of uncaught exceptions. Read this when organizing suites, testing promises/callbacks, picking reporters, or configuring Mocha.
Official sources
Install / setup
npm install --save-dev mocha
Source: https://mochajs.org/getting-started/ (Mocha 11.x — current latest, 11.7.x — requires Node.js ^18.18.0 || ^20.9.0 || >=21.1.0; the upcoming v12, still in beta, will require ^20.19.0 || >=22.12.0; run with npx mocha).
Core concepts
describe / it — group suites and declare individual test cases (BDD-style).
- Hooks —
before, after, beforeEach, afterEach set up and tear down state.
- Async support — return a promise,
await, or call the done callback to test async code.
- Pluggable assertions — bring Chai, Node's
assert, or another library; Mocha ships none.
- Reporters —
spec, dot, tap, json, etc. format results via --reporter.
- Exclusivity/skip —
.only and .skip focus or omit suites and tests.
- Configuration —
.mocharc.{js,json,yml} sets specs, timeouts, requires, and flags.
- Browser support — runs the same tests in browsers alongside Node.
Best practices
Common pitfalls
- Calling
done() and returning a promise both → pick one async style, not both.
- Arrow-function tests break
this.timeout() → use function () { ... }.
- Unhandled rejection passes silently → always
await/return the async assertion.
Examples
const assert = require('assert');
describe('Array', () => {
describe('#indexOf()', () => {
it('returns -1 when the value is not present', () => {
assert.equal([1, 2, 3].indexOf(4), -1);
});
});
});
Further reading
Related skills
- ../jasmine — batteries-included BDD framework with built-in assertions
- ../jest — all-in-one runner with bundled assertions and mocking
- ../vitest — Vite-native runner with a Jest-style API
1---2name: mocha3description: Mocha is a flexible JavaScript test framework for Node.js and the browser that runs tests serially with rich async support and pluggable assertions/reporters; consult when structuring describe/it suites, choosing an assertion library like Chai, testing async code, or configuring .mocharc.4domain: stack5category: testing6tags: [mocha, javascript, node, test-runner, bdd, async, chai]7official_sources:8 - https://mochajs.org/9 - https://github.com/mochajs/mocha10 - https://mochajs.org/getting-started/11verified: 2026-06-1712metadata:13 source: https://skillmd.com/skills/gabriel7silva/mocha14---1516# Mocha1718## Overview19Mocha is a feature-rich JavaScript test framework that runs on Node.js and in the browser, making asynchronous testing simple. It provides the test structure (`describe`/`it`), hooks, and reporters but leaves assertions to a library of your choice (commonly Chai). Tests run serially for accurate reporting and correct mapping of uncaught exceptions. Read this when organizing suites, testing promises/callbacks, picking reporters, or configuring Mocha.2021## Official sources22- Docs: https://mochajs.org/23- Repo: https://github.com/mochajs/mocha24- Install: https://mochajs.org/getting-started/2526## Install / setup27```bash28npm install --save-dev mocha29```30Source: https://mochajs.org/getting-started/ (Mocha 11.x — current latest, 11.7.x — requires Node.js `^18.18.0 || ^20.9.0 || >=21.1.0`; the upcoming v12, still in beta, will require `^20.19.0 || >=22.12.0`; run with `npx mocha`).3132## Core concepts33- **`describe` / `it`** — group suites and declare individual test cases (BDD-style).34- **Hooks** — `before`, `after`, `beforeEach`, `afterEach` set up and tear down state.35- **Async support** — return a promise, `await`, or call the `done` callback to test async code.36- **Pluggable assertions** — bring Chai, Node's `assert`, or another library; Mocha ships none.37- **Reporters** — `spec`, `dot`, `tap`, `json`, etc. format results via `--reporter`.38- **Exclusivity/skip** — `.only` and `.skip` focus or omit suites and tests.39- **Configuration** — `.mocharc.{js,json,yml}` sets specs, timeouts, requires, and flags.40- **Browser support** — runs the same tests in browsers alongside Node.4142## Best practices43- Use `--require` to load Babel/TS or setup hooks consistently (https://mochajs.org/interfaces/require/).44- Set realistic timeouts per slow test rather than globally inflating them (https://mochajs.org/features/timeouts/).45- Avoid arrow functions when you need Mocha's `this` context for timeouts (https://mochajs.org/features/arrow-functions/).46- Return promises instead of mixing `done` with async/await (https://mochajs.org/features/asynchronous-code/).4748## Common pitfalls49- Calling `done()` and returning a promise both → pick one async style, not both.50- Arrow-function tests break `this.timeout()` → use `function () { ... }`.51- Unhandled rejection passes silently → always `await`/return the async assertion.5253## Examples54```js55const assert = require('assert');5657describe('Array', () => {58 describe('#indexOf()', () => {59 it('returns -1 when the value is not present', () => {60 assert.equal([1, 2, 3].indexOf(4), -1);61 });62 });63});64```6566## Further reading67- https://mochajs.org/running/cli/ — CLI flags and config68- https://www.chaijs.com/ — Chai assertion library commonly paired with Mocha6970## Related skills71- ../jasmine — batteries-included BDD framework with built-in assertions72- ../jest — all-in-one runner with bundled assertions and mocking73- ../vitest — Vite-native runner with a Jest-style API