Mocha Exclusive Tests

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!

Questions