Skip Tests in Mocha

Dynamically skip JavaScript tests at runtime with Mocha

Daniel Korn
BigPanda Engineering
2 min readJul 12, 2019

--

Mocha is a popular JavaScript test framework for Node.js & the browser.
Unlike other test frameworks I’ve used before, it is really simple and quick to start with.

Mochajs logo
https://mochajs.org/

One of the most common actions when writing or debugging tests, regardless of the framework used, is being able to ignore some tests.
That is especially true if you practice TDD, or tend to first list the placeholders for the tests you plan to write.

This inclusive ability is available in Mocha by appending .skip() to the suite or to specific test cases. The skipped tests will be marked as "pending" in the test results.

Usage

Another quick option is prepending x to the it or the describe:

Skipping tests programmatically at runtime

In some situations, you’ll need to skip tests dynamically.
Example of such might be when the test depends on the running environment.

In this case use this.skip()

Do not execute any action after calling this.skip(), as the call will abort the test.

You can skip all tests in a describe block — including all it, beforeEach/ afterEach, and describe blocks within the suite - by using this.skip() in a before hook

Skip Tests Meme

Tip — Exclusive Tests

You can run just a specific suite or test-case by appending .only() to the function.

Mocha Installation

Originally published at https://danielkorn.io on July 12, 2019.

--

--