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.
Backers
Find Mocha helpful? Become a backer and support Mocha with a monthly donation.
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.
Features
- browser support
- simple async support, including promises
- test coverage reporting
- string diff support
- javascript API for running tests
- proper exit status for CI support etc
- auto-detects and disables coloring for non-ttys
- maps uncaught exceptions to the correct test case
- async test timeout support
- test retry support
- test-specific timeouts
- growl notification support
- 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
- mocha.opts file support
- clickable suite titles to filter test execution
- node debugger 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
- and more!
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
Installation
Install with npm globally:
$ npm install --global mocha
or as a development dependency for your project:
$ npm install --save-dev mocha
As of v6.0.0, Mocha requires Node.js v6.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
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
A brief outline on the order Mocha’s components are executed. Worth noting that all hooks, describe
and it
callbacks are run in the order they are defined (i.e. found in the file).
run 'mocha spec.js'
|
spawn child process
|
|--------------> inside child process
process and apply options
|
run spec file/s
|
|--------------> per spec file
suite callbacks (e.g., 'describe')
|
'before' root-level pre-hook
|
'before' pre-hook
|
|--------------> per test
'beforeEach' root-level pre-hook
|
'beforeEach' pre-hook
|
test callbacks (e.g., 'it')
|
'afterEach' post-hook
|
'afterEach' root-level post-hook
|<-------------- per test end
|
'after' post-hook
|
'after' root-level post-hooks
|<-------------- per spec file end
|<-------------- inside child process end
Detects Multiple Calls to done()
If you use callback-based async tests, Mocha will throw an error if done()
is called multiple times. This is handy for catching accidental double callbacks.
it('double done', function(done) {
// Calling `done()` twice is an error
setImmediate(done);
setImmediate(done);
});
Running the above test will give you the below error message:
$ ./node_modules/.bin/mocha mocha.test.js
✓ double done
1) double done
1 passing (6ms)
1 failing
1) double done:
Error: done() called multiple times
at Object.<anonymous> (mocha.test.js:1:63)
at require (internal/module.js:11:18)
at Array.forEach (<anonymous>)
at startup (bootstrap_node.js:187:16)
at bootstrap_node.js:608:3
Assertions
Mocha allows you to use any assertion library you wish. In the above example, we’re using Node.js’ built-in assert module–but generally, if it throws an Error
, it will work! This means you can use libraries such as:
- should.js - BDD style shown throughout these docs
- expect.js -
expect()
style assertions - chai -
expect()
,assert()
andshould
-style assertions - better-assert - C-style self-documenting
assert()
- unexpected - “the extensible BDD assertion toolkit”
Asynchronous Code
Testing asynchronous code with Mocha could not be simpler! Simply invoke the callback when your test is complete. By adding a callback (usually named done
) to it()
, Mocha will know that it should wait for this function to be called to complete the test. This callback accepts both an Error
instance (or subclass thereof) or a falsy value; anything else will cause a failed test.
describe('User', function() {
describe('#save()', function() {
it('should save without error', function(done) {
var user = new User('Luna');
user.save(function(err) {
if (err) done(err);
else done();
});
});
});
});
To make things even easier, the done()
callback also accepts an Error
instance (i.e. new Error()
), so we may use this directly:
describe('User', function() {
describe('#save()', function() {
it('should save without error', function(done) {
var user = new User('Luna');
user.save(done);
});
});
});
Working with Promises
Alternately, instead of using the done()
callback, you may return a Promise. This is useful if the APIs you are testing return promises instead of taking callbacks:
beforeEach(function() {
return db.clear()
.then(function() {
return db.save([tobi, loki, jane]);
});
});
describe('#find()', function() {
it('respond with matching records', function() {
return db.find({ type: 'User' }).should.eventually.have.length(3);
});
});
The latter example uses Chai as Promised for fluent promise assertions.
In Mocha v3.0.0 and newer, returning a Promise
and calling done()
will result in an exception, as this is generally a mistake:
const assert = require('assert');
it('should complete this test', function (done) {
return new Promise(function (resolve) {
assert.ok(true);
resolve();
})
.then(done);
});
The above test will fail with Error: Resolution method is overspecified. Specify a callback *or* return a Promise; not both.
. In versions older than v3.0.0, the call to done()
is effectively ignored.
Using async / await
If your JS environment supports async / await you can also write asynchronous tests like this:
beforeEach(async function() {
await db.clear();
await db.save([tobi, loki, jane]);
});
describe('#find()', function() {
it('responds with matching records', async function() {
const users = await db.find({ type: 'User' });
users.should.have.length(3);
});
});
Synchronous Code
When testing synchronous code, omit the callback and Mocha will automatically continue on to the next test.
describe('Array', function() {
describe('#indexOf()', function() {
it('should return -1 when the value is not present', function() {
[1,2,3].indexOf(5).should.equal(-1);
[1,2,3].indexOf(0).should.equal(-1);
});
});
});
Arrow Functions
Passing arrow functions (“lambdas”) to Mocha is discouraged. Lambdas lexically bind this
and cannot access the Mocha context. For example, the following code will fail:
describe('my suite', () => {
it('my test', () => {
// should set the timeout of this test to 1000 ms; instead will fail
this.timeout(1000);
assert.ok(true);
});
});
If you do not need to use Mocha’s context, lambdas should work. However, the result will be more difficult to refactor if the need eventually arises.
Hooks
With its default “BDD”-style interface, Mocha provides the hooks before()
, after()
, beforeEach()
, and afterEach()
. These should be used to set up preconditions and clean up after your tests.
describe('hooks', function() {
before(function() {
// runs before all tests in this block
});
after(function() {
// runs after all tests in this block
});
beforeEach(function() {
// runs before each test in this block
});
afterEach(function() {
// runs after each test in this block
});
// test cases
});
Tests can appear before, after, or interspersed with your hooks. Hooks will run in the order they are defined, as appropriate; all
before()
hooks run (once), then anybeforeEach()
hooks, tests, anyafterEach()
hooks, and finallyafter()
hooks (once).
Describing Hooks
Any hook can be invoked with an optional description, making it easier to pinpoint errors in your tests. If a hook is given a named function, that name will be used if no description is supplied.
beforeEach(function() {
// beforeEach hook
});
beforeEach(function namedFun() {
// beforeEach:namedFun
});
beforeEach('some description', function() {
// beforeEach:some description
});
Asynchronous Hooks
All hooks (before()
, after()
, beforeEach()
, afterEach()
) may be sync or async as well, behaving much like a regular test-case. For example, you may wish to populate database with dummy content before each test:
describe('Connection', function() {
var db = new Connection,
tobi = new User('tobi'),
loki = new User('loki'),
jane = new User('jane');
beforeEach(function(done) {
db.clear(function(err) {
if (err) return done(err);
db.save([tobi, loki, jane], done);
});
});
describe('#find()', function() {
it('respond with matching records', function(done) {
db.find({type: 'User'}, function(err, res) {
if (err) return done(err);
res.should.have.length(3);
done();
});
});
});
});
Root-Level Hooks
You may also pick any file and add “root”-level hooks. For example, add beforeEach()
outside of all describe()
blocks. This will cause the callback to beforeEach()
to run before any test case, regardless of the file it lives in (this is because Mocha has an implied describe()
block, called the “root suite”).
beforeEach(function() {
console.log('before every test in every file');
});
Delayed Root Suite
If you need to perform asynchronous operations before any of your suites are run, you may delay the root suite. Run mocha
with the --delay
flag. This will attach a special callback function, run()
, to the global context:
setTimeout(function() {
// do some setup
describe('my suite', function() {
// ...
});
run();
}, 5000);
Pending Tests
“Pending”–as in “someone should write these test cases eventually”–test-cases are simply those without a callback:
describe('Array', function() {
describe('#indexOf()', function() {
// pending test below
it('should return -1 when the value is not present');
});
});
Pending tests will be included in the test results, and marked as pending. A pending test is not considered a failed test.
Exclusive Tests
The exclusivity feature allows you to run only the specified suite or test-case by appending .only()
to the function. Here’s an example of executing only a particular suite:
describe('Array', function() {
describe.only('#indexOf()', function() {
// ...
});
});
Note: All nested suites will still be executed.
Here’s an example of executing an individual test case:
describe('Array', function() {
describe('#indexOf()', function() {
it.only('should return -1 unless present', function() {
// ...
});
it('should return the index when present', function() {
// ...
});
});
});
Previous to v3.0.0, .only()
used string matching to decide which tests to execute. As of v3.0.0, this is no longer the case. In v3.0.0 or newer, .only()
can be used multiple times to define a subset of tests to run:
describe('Array', function() {
describe('#indexOf()', function() {
it.only('should return -1 unless present', function() {
// this test will be run
});
it.only('should return the index when present', function() {
// this test will also be run
});
it('should return -1 if called with a non-Array context', function() {
// this test will not be run
});
});
});
You may also choose multiple suites:
describe('Array', function() {
describe.only('#indexOf()', function() {
it('should return -1 unless present', function() {
// this test will be run
});
it('should return the index when present', function() {
// this test will also be run
});
});
describe.only('#concat()', function () {
it('should return a new Array', function () {
// this test will also be run
});
});
describe('#slice()', function () {
it('should return a new Array', function () {
// this test will not be run
});
});
});
But tests will have precedence:
describe('Array', function() {
describe.only('#indexOf()', function() {
it.only('should return -1 unless present', function() {
// this test will be run
});
it('should return the index when present', function() {
// this test will not be run
});
});
});
Note: Hooks, if present, will still be executed.
Be mindful not to commit usages of
.only()
to version control, unless you really mean it! To do so one can run mocha with the option--forbid-only
in the continuous integration test command (or in a git precommit hook).
Inclusive Tests
This feature is the inverse of .only()
. By appending .skip()
, you may tell Mocha to simply ignore these suite(s) and test case(s). Anything skipped will be marked as pending, and reported as such. Here’s an example of skipping an entire suite:
describe('Array', function() {
describe.skip('#indexOf()', function() {
// ...
});
});
Or a specific test-case:
describe('Array', function() {
describe('#indexOf()', function() {
it.skip('should return -1 unless present', function() {
// this test will not be run
});
it('should return the index when present', function() {
// this test will be run
});
});
});
Best practice: Use
.skip()
instead of commenting tests out.
You may also skip at runtime using this.skip()
. If a test needs an environment or configuration which cannot be detected beforehand, a runtime skip is appropriate. For example:
it('should only test in the correct environment', function() {
if (/* check test environment */) {
// make assertions
} else {
this.skip();
}
});
The above test will be reported as pending. It’s also important to note that calling this.skip()
will effectively abort the test.
Best practice: To avoid confusion, do not execute further instructions in a test or hook after calling
this.skip()
.
Contrast the above test with the following code:
it('should only test in the correct environment', function() {
if (/* check test environment */) {
// make assertions
} else {
// do nothing
}
});
Because this test does nothing, it will be reported as passing.
Best practice: Don’t do nothing! A test should make an assertion or use
this.skip()
.
To skip multiple tests in this manner, use this.skip()
in a “before” hook:
before(function() {
if (/* check test environment */) {
// setup code
} else {
this.skip();
}
});
This will skip all it
, beforeEach/afterEach
, and describe
blocks within the suite. before/after
hooks are skipped unless they are defined at the same level as the hook containing this.skip()
.
describe('outer', function () {
before(function () {
this.skip();
});
after(function () {
// will be executed
});
describe('inner', function () {
before(function () {
// will be skipped
});
after(function () {
// will be skipped
});
});
});
Before Mocha v3.0.0,
this.skip()
was not supported in asynchronous tests and hooks.
Retry Tests
You can choose to retry failed tests up to a certain number of times. This feature is designed to handle end-to-end tests (functional tests/Selenium…) where resources cannot be easily mocked/stubbed. It’s not recommended to use this feature for unit tests.
This feature does re-run beforeEach/afterEach
hooks but not before/after
hooks.
NOTE: Example below was written using Selenium webdriver (which overwrites global Mocha hooks for Promise
chain).
describe('retries', function() {
// Retry all tests in this suite up to 4 times
this.retries(4);
beforeEach(function () {
browser.get('http://www.yahoo.com');
});
it('should succeed on the 3rd try', function () {
// Specify this test to only retry up to 2 times
this.retries(2);
expect($('.foo').isDisplayed()).to.eventually.be.true;
});
});
Dynamically Generating Tests
Given Mocha’s use of Function.prototype.call
and function expressions to define suites and test cases, it’s straightforward to generate your tests dynamically. No special syntax is required — plain ol’ JavaScript can be used to achieve functionality similar to “parameterized” tests, which you may have seen in other frameworks.
Take the following example:
var assert = require('chai').assert;
function add() {
return Array.prototype.slice.call(arguments).reduce(function(prev, curr) {
return prev + curr;
}, 0);
}
describe('add()', function() {
var tests = [
{args: [1, 2], expected: 3},
{args: [1, 2, 3], expected: 6},
{args: [1, 2, 3, 4], expected: 10}
];
tests.forEach(function(test) {
it('correctly adds ' + test.args.length + ' args', function() {
var res = add.apply(null, test.args);
assert.equal(res, test.expected);
});
});
});
The above code will produce a suite with three specs:
$ mocha
add()
✓ correctly adds 2 args
✓ correctly adds 3 args
✓ correctly adds 4 args
Test duration
Many reporters will display test duration and flag tests that are slow (default: 75ms), as shown here with the “spec” reporter:
There are three levels of test duration (depicted in the following image):
- FAST: Tests that run within half of the “slow” threshold will show the duration in green (if at all).
- NORMAL: Tests that run exceeding half of the threshold (but still within it) will show the duration in yellow.
- SLOW: Tests that run exceeding the threshold will show the duration in red.
To tweak what’s considered “slow”, you can use the slow()
method:
describe('something slow', function() {
this.slow(10000);
it('should take long enough for me to go make a sandwich', function() {
// ...
});
});
Timeouts
Suite-level
Suite-level timeouts may be applied to entire test “suites”, or disabled via this.timeout(0)
. This will be inherited by all nested suites and test-cases that do not override the value.
describe('a suite of tests', function() {
this.timeout(500);
it('should take less than 500ms', function(done){
setTimeout(done, 300);
});
it('should take less than 500ms as well', function(done){
setTimeout(done, 250);
});
})
Test-level
Test-specific timeouts may also be applied, or the use of this.timeout(0)
to disable timeouts all together:
it('should take less than 500ms', function(done){
this.timeout(500);
setTimeout(done, 300);
});
Hook-level
Hook-level timeouts may also be applied:
describe('a suite of tests', function() {
beforeEach(function(done) {
this.timeout(3000); // A very long environment setup.
setTimeout(done, 2500);
});
});
Again, use this.timeout(0)
to disable the timeout for a hook.
In v3.0.0 or newer, a parameter passed to
this.timeout()
greater than the maximum delay value will cause the timeout to be disabled.
Diffs
Mocha supports the err.expected
and err.actual
properties of any thrown AssertionError
s from an assertion library. Mocha will attempt to display the difference between what was expected, and what the assertion actually saw. Here’s an example of a “string” diff:
Command-Line Usage
mocha [spec..]
Run tests with Mocha
Commands
mocha debug [spec..] Run tests with Mocha [default]
mocha init <path> create a client-side Mocha setup at <path>
Rules & Behavior
--allow-uncaught Allow uncaught errors to propagate [boolean]
--async-only, -A Require all tests to use a callback (async) or
return a Promise [boolean]
--bail, -b Abort ("bail") after first test failure [boolean]
--check-leaks Check for global variable leaks [boolean]
--delay Delay initial execution of root suite
--exit Force Mocha to quit after tests complete [boolean]
--forbid-only Fail if exclusive test(s) encountered [boolean]
--forbid-pending Fail if pending test(s) encountered [boolean]
--global, --globals List of allowed global variables [array]
--retries Retry failed tests this many times [number]
--slow, -s Specify "slow" test threshold (in milliseconds)
[number] [default: 75]
--timeout, -t, --timeouts Specify test timeout threshold (in milliseconds)
[number] [default: 2000]
--ui, -u Specify user interface [string] [default: "bdd"]
Reporting & Output
--color, -c, --colors Force-enable color output [boolean]
--diff Show diff on failure
[boolean] [default: true]
--full-trace Display full stack traces [boolean]
--growl, -G Enable Growl notifications [boolean]
--inline-diffs Display actual/expected differences
inline within each string [boolean]
--reporter, -R Specify reporter to use
[string] [default: "spec"]
--reporter-option, --reporter-options, Reporter-specific options
-O (<k=v,[k1=v1,..]>) [array]
Configuration
--config Path to config file [default: (nearest rc file)]
--opts Path to `mocha.opts` [string] [default: "./test/mocha.opts"]
--package Path to package.json for config [string]
File Handling
--exclude Ignore file(s) or glob pattern(s)
[array] [default: (none)]
--extension, --watch-extensions File extension(s) to load and/or watch
[array] [default: js]
--file Specify file(s) to be loaded prior to root
suite execution [array] [default: (none)]
--recursive Look for tests in subdirectories [boolean]
--require, -r Require module [array] [default: (none)]
--sort, -S Sort test files
--watch, -w Watch files in the current working directory
for changes [boolean]
Test Filters
--fgrep, -f Only run tests containing this string [string]
--grep, -g Only run tests matching this string or regexp [string]
--invert, -i Inverts --grep and --fgrep matches [boolean]
Positional Arguments
spec One or more files, directories, or globs to test
[array] [default: ["test/"]]
Other Options
--help, -h Show usage information & exit [boolean]
--version, -V Show version number & exit [boolean]
--interfaces List built-in user interfaces & exit
--reporters List built-in reporters & exit
Mocha Resources
Chat: [35mhttps://gitter.im/mochajs/mocha[39m
GitHub: [34mhttps://github.com/mochajs/mocha.git[39m
Docs: [33mhttps://mochajs.org/[39m