PROVIDER Β· the flexible Node.js test runner
Mocha
Mocha is among Node.js's earliest test runners handling only organisation and execution with free composition of assertions and mocks, suiting minimal-dependency library and backend work as new projects shift to all-in-one tools.
Pros
Cons
Product Information
Detailed Notes
Overview
Mocha was created by TJ Holowaychuk in 2011 as one of the earliest Node.js test runners, handling only organisation and execution through describe and it blocks, hooks, and async support with assertion and mocking libraries chosen by the user such as Chai and Sinon.
For Programming Languages & Frameworks work the value is free composition, since assertion styles are selectable among expect, should, and assert with mocking and coverage tools swappable, and this unbound design suits Node backend and library authors preferring it over Jest's all-in-one approach with async support an early advantage.
See the current state, because built-in assertions, snapshots, and coverage are absent requiring multiple extra libraries, ecosystem growth has slowed with new Node projects choosing Vitest or the built-in test runner, and docs plus examples update slowly.
Core Strengths
- A runner only with assertions and mocks composed freely
- BDD-style describe and it structure with strong readability
- Native async and promise-returning test support
- Suits library authors and minimal-dependency backend projects
Product Ecosystem
Core structure
describe and it with before and after hooks, only and skip filters, and async tests.
Common pairings
Chai assertions, Sinon mocks, nyc coverage, and ts-mocha with ts-node.
Running
The mocha CLI, .mocharc configuration, watch mode, and CI reporters.
Drawbacks
- No built-in assertions, snapshots, or coverage requiring extra libraries
- Slowing ecosystem with new projects choosing Vitest or Node's runner
- Scattered configuration raising maintenance cost
- Slower doc updates with weaker TypeScript experience than Vitest
Use Cases
- Node backend and library tests (β β β β ) - Free composition
- Projects preferring minimal dependencies (β β β β ) - Build as needed
- Maintaining existing Mocha suites (β β β β ) - Stable
- All-in-one testing for new projects (β β ) - Choose Vitest or Jest
Pricing
| Plan | Price | Scope |
|---|---|---|
| Mocha | $0 | MIT licensed, free |
| Chai, Sinon, and nyc | $0 | Open source and free |
| CI runtime cost | Platform billing | GitHub Actions and similar |
Entirely free with each composed library also free, where the real cost is configuration and maintenance since assertions, mocks, coverage, and TypeScript each need a library kept compatible.
FAQ
-
Mocha or Jest? Mocha for free composition and minimal dependencies while Jest or Vitest bring built-in assertions, snapshots, and coverage.
-
Which assertion library? Chai is most common with expect, should, and assert styles, or use Node's built-in assert to cut dependencies.
-
Is it TypeScript friendly? Workable through ts-node or tsx though typing experience and configuration trail Vitest.
Code Examples
Sync and async assertions
import { expect } from 'chai';
import { filterCheap } from '../src/plans.js';
describe('filterCheap', () => {
it('keeps only cheap plans', () => {
expect(filterCheap([{ price: 99 }, { price: 59 }], 80)).to.have.lengthOf(1);
});
it('supports async assertions', async () => {
const items = await fetchPlans();
expect(items).to.be.an('array');
});
});
npx mocha --watch keeps running and promise-returning tests are awaited automatically.
Hooks and skipping
describe('database tests', () => {
let db;
before(async () => { db = await connect(); });
after(async () => { await db.close(); });
beforeEach(async () => { await db.truncate(); });
it.skip('TODO: pagination', () => {});
});
Community & Resources
Communities and official channels
- Mocha Site β Config, usage, and examples
- GitHub mochajs/mocha β Source, issues, and releases
- Stack Overflow #mocha β Async and timeout answers
Learning resources and tools
- Mocha Getting Started β Official quick start
- Chai β The most common assertion library
- Sinon β Test doubles and mocks