mocha
Mocha is a feature-rich JavaScript test framework that runs on Node.js and in the browser, providing a flexible foundation for writing and organizing tests. Unl…
Installation
npm install -D mocha
yarn add -D mocha
pnpm add -D mocha
Import
// mocha tests use globals: describe, it
Quick Example
import assert from 'assert';
describe('Array', () => {
describe('#indexOf()', () => {
it('should return -1 when not found', () => {
assert.strictEqual([1, 2, 3].indexOf(4), -1);
});
});
});About mocha
Mocha is a feature-rich JavaScript test framework that runs on Node.js and in the browser, providing a flexible foundation for writing and organizing tests. Unlike Jest's all-in-one approach, Mocha focuses on being an excellent test runner while allowing developers to choose their preferred assertion library (Chai, Should.js, expect.js, or Node's built-in assert), mocking framework (Sinon, testdouble), and coverage tool (Istanbul/nyc). Mocha provides BDD (describe/it) and TDD (suite/test) interfaces, configurable timeouts, before/after hooks at suite and test levels, grep filtering for running specific tests, and support for parallel test execution. The framework handles asynchronous testing through callbacks, Promises, and async/await with automatic detection. Mocha's reporter system includes built-in spec, dot, nyan, and JSON reporters, with numerous third-party reporters available. Mocha's flexibility and maturity make it popular in enterprises and projects with specific testing stack requirements. The framework supports test retries, root-level hooks, pending tests, and exclusive/inclusive test running via .only() and .skip().
Quick Facts
| Package | mocha |
| Category | Testing |
| Weekly Downloads | 8M+ |
| License | MIT |
| Install | npm install -D mocha |
Related Packages
Chai is a BDD/TDD assertion library for Node.js and the browser that pairs with any JavaScript testi…
Sinon.js provides standalone test spies, stubs, and mocks for JavaScript that work with any test fra…
Jest is a delightful JavaScript testing framework with a focus on simplicity, created by Facebook. I…
nyc is the command-line interface for Istanbul code coverage, providing an easy way to add coverage …
SuperTest is a high-level HTTP assertion library built on SuperAgent that makes it easy to test Node…
Browse npm Packages by Category
Explore our reference of 200 popular npm packages with install commands, examples, and quick-start guides.