Mocha is a highly flexible and feature-rich JavaScript test framework designed for both Node.js environments and browsers. It facilitates writing and running tests with support for various styles, including Behavior Driven Development (BDD) and Test Driven Development (TDD). The current stable version is 11.7.5, with frequent beta releases for version 12 indicating active development. Mocha differentiates itself by being unopinionated about the assertion library, allowing developers to choose their preferred tools (e.g., Chai, Node's built-in `assert`). It provides robust capabilities for asynchronous testing, hooks for setup/teardown, and comprehensive reporting, making it a popular choice for defining test suites and individual test cases.
npm install mochaVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to set up a basic Mocha test file using TypeScript and Chai for assertions. It includes synchronous and asynchronous test examples with `describe` and `it` blocks, illustrating common testing patterns.
Upgrade your Node.js environment to version 20.19.0 or newer, or version 22.12.0 or newer, before upgrading to Mocha v12.
Migrate test files and any test-related utility modules to ES module syntax (`import`/`export`) or ensure they are correctly configured for CommonJS (`.cjs` extension or no `"type": "module"` in `package.json`). For mixed environments, understand Node.js's module resolution rules, possibly using dynamic `import()` for ESM from CJS.
For promise-based async tests, return the Promise. For callback-based async tests, ensure `done()` is called exactly once when the asynchronous operation completes (or with an error). Do not mix these patterns.
Ensure your environment meets the updated Node.js requirements. For older browser testing, use an earlier Mocha version. Update any scripts or tooling that directly invoke `bin/mocha` to `bin/mocha.js`.
For TypeScript projects, run `npm install --save-dev @types/mocha`. Ensure your `tsconfig.json` includes `@types/mocha` in the `types` array or `compilerOptions.typeRoots`.
Run your tests using the `mocha` command (e.g., `mocha your-test-file.js`) instead of `node your-test-file.js`. For TypeScript, ensure `@types/mocha` is installed and properly configured in `tsconfig.json`.
Increase the timeout for the specific test (`it('...', function() { ... }).timeout(5000);`) or the entire suite (`this.timeout(5000);`). Ensure all asynchronous operations correctly complete by calling `done()` or resolving/rejecting the returned Promise.Ensure consistent module types. If using ESM, adopt `import` statements. If using CJS, stick to `require()` or use dynamic `import()` for ESM dependencies. Consider renaming `.js` files that are ESM to `.mjs` or setting `"type": "module"` in `package.json` for ESM projects.
Ensure you are using the correct test interface for your syntax. If you're using `describe`/`it`, use the default BDD interface or explicitly specify `mocha --ui bdd`. Check your Mocha configuration files (`.mocharc.js`, `package.json`) for conflicting `ui` settings.
No dependency data recorded yet.