Mocha: testing JavaScript
Mocha is a test framework for JavaScript. It runs on Node.js and in the browser, and its test files are plain JavaScript: a group of checks is named with describe, one check with it. Mocha runs the checks one after another and prints a single report, so an uncaught exception stays attached to the test that caused it. The project lives on GitHub, and its repository is mochajs/mocha.
Mocha does not bring an assertion library of its own. In Node.js the built-in node:assert is enough, and the browser examples below take assert from chai.
The first test in Node.js
Create a project and install Mocha as a development dependency:
npm init -y
npm install --save-dev mocha
The project holds one module and one folder with the test:
mocha-demo/ ├── package.json ├── sum.js └── test/ └── sum.test.js
sum.js
function sum(a, b) {
return a + b;
}
module.exports = { sum };
test/sum.test.js
const assert = require('node:assert/strict');
const { sum } = require('../sum.js');
describe('sum', function () {
it('adds two numbers', function () {
assert.equal(sum(1, 2), 3);
});
it('adds negative numbers', function () {
assert.equal(sum(-2, 5), 3);
});
});
Mocha finds the files in test/ by itself, so the command is short:
npx mocha
sum
✔ adds two numbers
✔ adds negative numbers
2 passing (15ms)
Each it is one line with its own result, and the last two lines count the checks and give the time of the run. A check that passes in a deeper folder shows its full name in the report, so a failing line can be found in the file at once.
When a check fails
A wrong expected value turns the same file into a failure. Here the second argument of the first check is 5:
const assert = require('node:assert/strict');
const { sum } = require('../sum.js');
describe('sum', function () {
it('adds two numbers', function () {
assert.equal(sum(2, 2), 5);
});
});
npx mocha
sum
1) adds two numbers
0 passing (25ms)
1 failing
1) sum
adds two numbers:
AssertionError [ERR_ASSERTION]: Expected values to be strictly equal:
4 !== 5
+ expected - actual
-4
+5
The pair 4 !== 5 is the actual value and the expected one, and the lines below repeat them with - and +. The report names the file and the line of the check, so the next step is editing that line or the function it calls.
Running Mocha in the browser
In a browser page Mocha needs its stylesheet in <head>:
<link rel="stylesheet" href="https://unpkg.com/mocha/mocha.css" />
The report is drawn into a div with the id mocha:
<div id="mocha"></div>
Then come the scripts: chai, Mocha itself, the setup call, the test file and the start of the run.
<script src="https://unpkg.com/chai/chai.js"></script>
<script src="https://unpkg.com/mocha/mocha.js"></script>
<script>
mocha.setup('bdd');
mocha.checkLeaks();
</script>
<script src="mocha_first_test.js"></script>
<script>
mocha.run();
</script>
The same order is described in the Mocha manual. The page below is a working example: it loads the libraries from unpkg, registers two checks for a function and runs them.
function double(n) {
return n * 2;
}
describe('double', function () {
it('doubles a number', function () {
assert.equal(double(3), 6);
});
it('doubles a negative number', function () {
assert.equal(double(-2), -4);
});
});
The sum example has its own page with a plain HTML test page next to it: Mocha test page: running tests in the browser.
Where to go next
Mocha checks functions and small units of code. A whole browser is driven by Playwright, and the packages that run the test command are installed with npm. Both tools appear in the QA section of this site, next to the JavaScript course.
Article author: Andrei Olegovich