Table of contents
1.
Introduction
2.
Exclusive Tests
3.
Inclusive Tests
4.
FAQs
5.
Key Takeaways
Last Updated: Aug 13, 2025
Easy

Exclusive Tests and Inclusive Tests

Author Parth Jain
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

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
    });
  });
});

Inclusive Tests

Opposite to Exclusive tests, Inclusive tests allow the developer to ignore a test case or a bunch of test cases. The cases that are skipped are marked as pending and are later reported.
Below is an example of how a case is skipped.

describe('Array', function () {
  describe('#indexOf()', function () {
    it.skip('will return -1 unless present', function () {
      // this test will be skipped.Hence,it will not run
    });

    it('should return the index when present', function () {
      // this test won’t be skipped and will run
    });
  });
});

The .skip() function can be placed on an entire suite as well. This means appending .skip() to all tests inside the suite. Hooks in the suite are also skipped.

describe('Array', function () {
  describe.skip('#indexOf()', function () {
    it('will return -1 unless present', function () {
      // this test will not run
    });
  });
});

The .skip() function can also enable skip at runtime.Consider the code below.

it('should only test in the correct environment', function() {
  if (/* check test environment */) {
  } else {
    this.skip();
  }
});

FAQs

  1. What is Mocha JS used for?
    Mocha is a feature-packed JavaScript testing framework that runs on Node.js, making asynchronous testing simple and efficient. 
  2. What is Mocha NodeJS?
    Mocha is a testing library for Node. Js, created to be simple, extensible, and rapid. It's used for unit and integration testing.
  3. What are Exclusive Tests?
    Exclusive Tests allow the developer to run only a specified test case or suite of tests with the help of the .only() function.
  4. What are Inclusive Tests?
    Inclusive tests allow the developer to ignore a test case or many test cases. The cases that are skipped are marked as pending and are later reported.
  5. How to install Mocha JS?
    Mocha's installation process is straightforward. All we need to do is type 
    $ npm install --global Mocha to install Mocha globally inside the system.

Key Takeaways

This article taught us about the Exclusive and Inclusive Test in Mocha.js and how they help when testing an application. However, this isn't enough, as there is always much more to explore and learn about this vast field of Web Development. To know more about Mocha and its intricacies, check out the articles on Mocha or enroll in our highly curated Web Development course. .If you are Preparing for interview and don't know where to start, we have got you covered, check out our expert curated courses on our website, You can also check out Coding Ninjas Studio to practice frequently asked interview problems. We hope that this blog has helped you enhance your knowledge regarding Retrying Tests and if you would like to learn more, check out our articles. Do upvote our blog to help other ninjas grow. Happy Coding!”  

Live masterclass