Mocha
In one line
Mocha is a lightweight test runner that lets you choose your own assertion library (Chai) and mocking library (Sinon).
In simple words
Mocha is a minimalist test runner. Unlike Jest which includes everything, Mocha only runs tests — you bring your own assertion library (usually Chai) and mocking library (usually Sinon).
This 'bring your own tools' approach gives you flexibility. You can mix and match the libraries you prefer. But it also means more setup: you install and configure Mocha, Chai, Sinon, and a coverage tool separately.
Mocha is popular in the Node.js backend world and for teams that want fine-grained control over their testing stack. For beginners, Jest's all-in-one approach is usually simpler.
Example
// math.test.js — Mocha + Chai
const { expect } = require('chai');
const { add } = require('./math');
describe('add', () => {
it('returns the sum of two numbers', () => {
expect(add(2, 3)).to.equal(5);
});
it('handles negative numbers', () => {
expect(add(-1, -4)).to.equal(-5);
});
});
// Run: npx mocha math.test.js
// add
// ✓ returns the sum of two numbers
// ✓ handles negative numbers
// 2 passingMocha uses describe/it blocks to structure tests and relies on an assertion library like Chai (expect). Run with npx mocha to execute and see the pass/fail report.
Related terms
Related Resources
Related Lessons
- Mocha Basics
Getting started with Mocha.
Decode error messages
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.