https://mochajs.org
Mocha - the fun, simple, flexible JavaScript test framework
Mocha - the fun, simple, flexible JavaScript test framework simple, flexible, fun Mocha is a feature-rich JavaScript test framework running on Node.js and in the browser, making asynchronous testing simple and fun. Mocha tests run serially, allowing for flexible and accurate reporting, while mapping uncaught exceptions to the correct test cases. Hosted on GitHub. # Sponsors Use Mocha at Work? Ask your manager or marketing team if they’d help support our project. Your company’s logo will also be displayed on npmjs.com and our GitHub repository. # Backers Find Mocha helpful? Become a backer and support Mocha with a monthly donation. # Features browser support simple async support, including promises run Node.js tests in parallel test coverage reporting string diff support JavaScript API for running tests auto-detects and disables coloring for non-TTYs async test timeout support test retry support test-specific timeouts reports test durations highlights slow tests file watcher support global variable leak detection optionally run tests that match a regexp auto-exit to prevent “hanging” with an active loop easily meta-generate suites & test-cases config file support node debugger support node native ES modules support source-map support detects multiple calls to done() use any assertion library you want extensible reporting, bundled with 9+ reporters extensible test DSLs or “interfaces” before, after, before each, after each hooks arbitrary transpiler support (coffee-script etc) TextMate bundle # Table of Contents Installation Getting Started Run Cycle Overview Detects Multiple Calls to done() Assertions Asynchronous Code Synchronous Code Arrow Functions Hooks Pending Tests Exclusive Tests Inclusive Tests Retry Tests Dynamically Generating Tests Timeouts Diffs Command-Line Usage Parallel Tests Root Hook Plugins Global Fixtures Test Fixture Decision-Tree Wizard Thing Interfaces Reporters Node.JS native ESM support Running Mocha in the Browser Configuring Mocha (Node.js) The test/ Directory Error Codes Editor Plugins Examples Testing Mocha More Information # Installation Install with npm globally: $ npm install --global mocha or as a development dependency for your project: $ npm install --save-dev mocha As of v10.0.0, Mocha requires Node.js v14.0.0 or newer. # Getting Started $ npm install mocha $ mkdir test $ $EDITOR test/test.js # or open with your favorite editor In your editor: var 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); }); }); }); Back in the terminal: $ ./node_modules/mocha/bin/mocha.js Array #indexOf() ✓ should return -1 when the value is not present 1 passing (9ms) Set up a test script in package.json: "scripts": { "test": "mocha" } Then run tests with: $ npm test # Run Cycle Overview Updated for v8.0.0. The following is a mid-level outline of Mocha’s “flow of execution” when run in Node.js; the “less important” details have been omitted. In a browser, test files are loaded by