Cette page est affichée en anglais. Une traduction française est en cours.
ScriptsApr 23, 2026·3 min de lecture

Mocha — Feature-Rich JavaScript Testing Framework

Mocha is a flexible JavaScript test framework for Node.js and the browser, supporting BDD and TDD styles with async testing and rich reporting.

assetLangBanner.body

Introduction

Mocha is a JavaScript test framework that runs on Node.js and in the browser, making asynchronous testing straightforward. It provides a flexible and accurate reporting system while mapping uncaught exceptions to the correct test cases. Mocha has been a foundational testing tool in the JavaScript ecosystem since 2011.

What Mocha Does

  • Runs test suites in serial with full exception tracing for accurate reporting
  • Supports BDD (describe/it), TDD (suite/test), QUnit, and exports interfaces
  • Handles async tests via callbacks, Promises, and async/await natively
  • Generates reports in spec, dot, nyan, JSON, TAP, and custom reporter formats
  • Provides hooks (before, after, beforeEach, afterEach) for setup and teardown

Architecture Overview

Mocha loads test files, parses the describe/it tree into a suite hierarchy, then runs each test sequentially by default. A Runner emits events consumed by a Reporter. Async boundaries are detected automatically: if a test returns a Promise or accepts a done callback, Mocha waits for resolution or timeout before proceeding.

Self-Hosting & Configuration

  • Install via npm install --save-dev mocha and add a test script to package.json
  • Configure via .mocharc.yml, .mocharc.json, or mocha field in package.json
  • Set --timeout to adjust the default 2-second async test timeout
  • Use --recursive to discover test files in nested directories
  • Run in the browser by loading mocha.js and calling mocha.setup("bdd")

Key Features

  • Supports every major assertion library (assert, Chai, should.js, expect.js)
  • File-watch mode re-runs tests on source changes automatically
  • Root-level hooks and global fixtures for cross-suite setup
  • Parallel mode via --parallel flag for large test suites
  • Rich plugin ecosystem with custom reporters and interfaces

Comparison with Similar Tools

  • Jest — batteries-included with built-in assertions and mocking; Mocha is more modular and lets you choose your own assertion library
  • Vitest — Vite-native with ESM-first design; Mocha predates ESM but supports it via loader flags
  • Jasmine — similar BDD interface but bundles its own assertion library; Mocha is more flexible
  • AVA — runs tests concurrently by default; Mocha runs serially for predictability
  • Tape — minimal TAP-producing test harness; Mocha offers richer reporting and lifecycle hooks

FAQ

Q: Can Mocha run tests in parallel? A: Yes, use the --parallel flag. Mocha spawns worker processes and distributes test files across them.

Q: Does Mocha support TypeScript? A: Yes, use --require ts-node/register or configure the require option in .mocharc.yml.

Q: How do I use Mocha with ES modules? A: Run Node with --experimental-specifier-resolution=node or name test files with .mjs extension.

Q: Is Mocha still actively maintained? A: Yes, Mocha continues to receive updates and bug fixes with an active maintainer team.

Sources

Fil de discussion

Connectez-vous pour rejoindre la discussion.
Aucun commentaire pour l'instant. Soyez le premier à partager votre avis.

Actifs similaires