Mocha Test Framework
Mocha is a feature-rich JavaScript test framework for Node.js and browsers, designed to make asynchronous testing simple and enjoyable. It offers a flexible base for writing tests and integrates well with various assertion libraries. The current stable version is 11.7.5, with version 12 actively in beta, demonstrating continuous development and frequent updates.
Common errors
-
Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" is called or a Promise is returned.
cause An asynchronous test or hook executed longer than the default 2-second timeout.fixIncrease the timeout for the test/suite using `this.timeout(5000)` or globally with `mocha --timeout 5000`. Ensure asynchronous operations return a Promise or call `done()`. -
Error: No tests found
cause Mocha could not find any test files matching the configured patterns or paths.fixDouble-check the test file paths and glob patterns specified in your `mocha` command or configuration file. Ensure files exist and are correctly named. -
ReferenceError: describe is not defined
cause The test file is being run outside the Mocha test runner, or Mocha's global test functions are not exposed.fixExecute your test file using the `mocha` command (e.g., `npx mocha my-test.js`). Ensure Mocha is installed and correctly configured to expose globals. -
TypeError: Cannot read properties of undefined (reading 'timeout')
cause Attempted to use `this.timeout()` or other Mocha context methods within an arrow function, which does not correctly bind `this`.fixChange the arrow function to a traditional `function` declaration: `it('...', function() { this.timeout(5000); /* ... */ });`. -
Mocha exited with code 1
cause A general failure occurred, often due to a test failure, uncaught exception, unhandled promise rejection, or a critical configuration error.fixReview the console output immediately preceding the exit code for more specific error messages. Use `mocha --full-trace` for detailed stack traces to pinpoint the issue.
Warnings
- gotcha Mocha tests have a default timeout of 2000ms. Asynchronous tests or complex operations exceeding this can lead to 'Timeout of 2000ms exceeded' errors, especially in CI environments or slower machines.
- gotcha Using arrow functions for Mocha hooks (`before`, `afterEach`, `it`, etc.) prevents access to the Mocha context (`this`), which is necessary for methods like `this.timeout()`, `this.retries()`, or `this.skip()`. Arrow functions do not bind their own `this` context.
- breaking Mocha v12 (currently in beta) introduces robust ESM support, including direct `.mjs` exports and ESM configuration files. Projects relying on CommonJS modules might require configuration adjustments and code changes when upgrading to v12.
- gotcha If Mocha reports 'No tests found' and exits with an error, it often indicates an issue with the specified test file paths or glob patterns in the CLI command or configuration. It can also happen if test files are not accessible or contain syntax errors preventing their loading.
Install
-
npm install mocha -
yarn add mocha -
pnpm add mocha
Imports
- describe
import { describe } from 'mocha';
Quickstart
import { describe, it } from 'mocha';
import { expect } from 'chai';
describe('Array', () => {
it('should return -1 when the value is not present', () => {
expect([1, 2, 3].indexOf(4)).to.equal(-1);
});
it('should return the correct index when present', () => {
expect([1, 2, 3].indexOf(2)).to.equal(1);
});
});