Simple, flexible, fun JavaScript testing framework for Node.js and browsers
npx @tessl/cli install tessl/npm-mocha@11.7.0Mocha is a feature-rich JavaScript testing framework that runs on Node.js and in browsers. It provides flexible test organization with BDD/TDD interfaces, extensive reporting options, asynchronous testing support, and parallel execution capabilities for improved performance.
npm install mochaconst { Mocha } = require('mocha');
const mocha = require('mocha');For ES modules:
import Mocha from 'mocha';
import { describe, it, before, after, beforeEach, afterEach } from 'mocha';Browser (via script tag):
<script src="node_modules/mocha/mocha.js"></script>
<script>
mocha.setup('bdd');
</script>const { describe, it } = require('mocha');
const assert = require('assert');
describe('Array', function() {
describe('#indexOf()', function() {
it('should return -1 when the value is not present', function() {
assert.equal([1, 2, 3].indexOf(4), -1);
});
it('should return the correct index when value is present', function() {
assert.equal([1, 2, 3].indexOf(2), 1);
});
});
});Mocha is built around several key components:
Mocha supports multiple interfaces for organizing tests, with BDD being the default. Each interface provides different syntax styles for defining test suites and cases.
// BDD Interface (default)
function describe(title, fn);
function it(title, fn);
function before(fn);
function after(fn);
function beforeEach(fn);
function afterEach(fn);
// TDD Interface
function suite(title, fn);
function test(title, fn);
function setup(fn);
function teardown(fn);
function suiteSetup(fn);
function suiteTeardown(fn);Test Organization and Interfaces
Core test execution functionality with lifecycle management, event emission, and parallel execution support.
class Mocha {
constructor(options);
run(callback);
addFile(filepath);
reporter(name, options);
timeout(ms);
slow(ms);
}
class Runner extends EventEmitter {
run(callback);
abort();
grep(pattern);
}Comprehensive reporting system with built-in reporters and support for custom reporters.
class Base {
constructor(runner, options);
done(failures, callback);
epilogue();
}Browser-specific functionality and setup for running tests in browser environments.
// Browser global functions
mocha.setup(options);
mocha.run(callback);
mocha.throwError(error);Command-line interface and configuration options for test execution.
interface MochaOptions {
ui?: string;
reporter?: string;
timeout?: number;
slow?: number;
grep?: string | RegExp;
fgrep?: string;
bail?: boolean;
parallel?: boolean;
jobs?: number;
}interface MochaOptions {
ui?: string;
reporter?: string | Reporter;
timeout?: number;
slow?: number;
grep?: string | RegExp;
fgrep?: string;
bail?: boolean;
parallel?: boolean;
jobs?: number;
asyncOnly?: boolean;
allowUncaught?: boolean;
checkLeaks?: boolean;
color?: boolean;
delay?: boolean;
diff?: boolean;
dryRun?: boolean;
fullTrace?: boolean;
inlineDiffs?: boolean;
invert?: boolean;
retries?: number;
forbidOnly?: boolean;
forbidPending?: boolean;
global?: string[];
recursive?: boolean;
sort?: boolean;
exit?: boolean;
}
interface Suite {
title: string;
parent: Suite | null;
pending: boolean;
timeout(ms?: number): number | Suite;
slow(ms?: number): number | Suite;
bail(bail?: boolean): boolean | Suite;
}
interface Test {
title: string;
fn: Function;
parent: Suite;
pending: boolean;
state: 'failed' | 'passed' | 'pending';
timeout(ms?: number): number | Test;
slow(ms?: number): number | Test;
}
interface Hook {
title: string;
fn: Function;
parent: Suite;
type: 'before' | 'after' | 'beforeEach' | 'afterEach';
}
interface Context {
test?: Test;
currentTest?: Test;
timeout(ms?: number): number | Context;
slow(ms?: number): number | Context;
skip(): never;
retries(count?: number): number | Context;
}
type DoneCB = (error?: any) => void;
type AsyncTestFunction = () => Promise<any>;
type TestFunction = (done?: DoneCB) => void | Promise<any>;
interface Reporter {
new(runner: Runner, options?: any): Reporter;
}