Introduction
Mocha is a feature-packed JavaScript test framework that can be used with Angular, Vue, Express, etc. Mocha allows performing asynchronous testing simple.
This blog discusses Exclusive and Inclusive Tests in Mocha JS.
Exclusive Tests
By appending the .only() function, Mocha allows the developer to run only a specified test case or suite of tests. This exclusivity feature enables Mocha to support Exclusive Tests.
Consider the code below.
describe('Array', function () {
describe.only('#indexOf()', function () {
// …
//All the suites that are nested will still be executed.
});
});
This code executes only a particular suite.
Here’s another example that executes an individual test case:
describe('Array', function () {
describe('#indexOf()', function () {
it.only('will return -1 ,unless present', function () {
// ...
});
it('will return the index when present', function () {
// ...
});
});
});
Previously, the .only() function used string matching concept to choose which tests to execute.However,this is no longer the case in the newer version as .only() function can be used many times to define a subset of tests to run.
describe('Array', function () {
describe('#indexOf()', function () {
it.only('will return -1 unless present', function () {
// this test will run
});
it.only('will return the index when present', function () {
// this test will also run
});
it('will return -1 when called with a non-Array context', function () {
// this test will not run
});
});
});
You can also choose multiple suites.
describe('Array', function () {
describe.only('#indexOf()', function () {
it('will return -1 unless present', function () {
// this test will run
});
it('will return the index when present', function () {
// this test will also run
});
});
describe.only('#concat()', function () {
it('will return a new Array', function () {
// this test will also run
});
});
describe.only('#splice()', function () {
it('will return a new Array', function () {
// this test will also run
});
});
describe('#slice()', function () {
it('will return a new Array', function () {
// this test will not be run
});
});
});




