Table of contents
1.
Introduction
2.
Timeouts
2.1.
Suite-level
2.2.
Test-level
2.3.
Hook-level
3.
Change the default timeout 
4.
FAQs
5.
Key Takeaways
Last Updated: Mar 27, 2024
Easy

Test Durations and Timeouts for tests

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

Introduction

Mocha is a powerful JavaScript test framework that runs on Node.js and in the browser, making asynchronous testing easy and enjoyable. Mocha tests execute in sequential order, allowing for flexible and accurate reporting and mapping uncaught exceptions to the appropriate test cases. Tests that are executed have some duration and timeouts. We will learn about it in detail through this article. Let’s dive in.

Timeouts

Any test that takes longer than 2 seconds (2000ms) to complete will be timed out by Mocha.js by default. A timeout causes the test to fail and a timeout error to be thrown.
For a given test suite, hook, or test case, Mocha.js provides a this.timeout() method for specifying the amount of time that should elapse before a timeout occurs. The time in milliseconds is represented by the number passed to this.timeout().
The timeout can be configured in various ways (suite, hook, and test levels). 

Suite-level

Can we use this to apply suite-level timeouts to the entire test? suites? or to disable them. timeout(0). All nested suites and test cases that do not override the value inherit this timeout setting.

describe('a suite of tests', function() {
  this.timeout(500);

  it('This process should take less than 500 milliseconds', function(done) {
    setTimeout(done, 300);
  });

  it('This process should take less than 500 milliseconds as well', function(done) {
    setTimeout(done, 250);
  });
});

Test-level

A test-case can have test-specific timeouts applied to it. You can also use this.timeout(0) to completely disable timeouts:

it('This process has to take less than 500ms', function(done) {
  this.timeout(500);
  setTimeout(done, 300);
});

Hook-level

Hook-level timeouts can also be applied to a test-case, as shown in the examples below:

describe('a suite of tests', function() {
  beforeEach(function(done) {
    this.timeout(3000); // This is a very long environment setup.
    setTimeout(done, 2500);
  });
});
describe('a suite of tests', function() {
    afterEach(function(done) {
    this.timeout(70); // The test duration here is within normal range.
    setTimeout(done, 65);
  });
});
describe('a suite of tests', function() {
  beforeEach(function(done) {
    this.timeout(30); // A very fast environment setup.
    setTimeout(done, 25);
  });
});

This can be used in the same way as the other types of timeout tests. timeout(0) disables the hook's timeout.

Change the default timeout 

For mocha tests, the default timeout is 2000 milliseconds. There are several options for changing this:

Change timeout for a single test case

describe("testing promises", function () {
    this.timeout(5000);
    it('test1', function(){ ... });
});

describe("testing promises", function () {

    it('test1', function(){ 
        this.timeout(5000);
        ...
     });
});

Change timeout for all the test cases

"scripts": {
  "tests": "./node_modules/mocha/bin/mocha 'test/**/*.spec.js' --timeout 5000",
},

Or 

mocha.setup({ timeout: 5000 });

FAQs

  1. Why is it not recommended to use arrow functions to call this.timeout()?
    This is because an arrow function gets this information from the scope in which it is used. Mocha will call the function with a value, but that value will not be passed to the arrow function.
  2. What does diffs show in Mocha?
    The err.actual and err.expected properties of any thrown AssertionErrors from an assertion library are supported by Mocha. Mocha tries to show the difference between what the assertion expected and what it actually saw. 
  3. Which command-line parameter can be used to overwrite for the partial test?
    If we have a unit test file called my-spec.js and we're using Mocha:
    mocha my-spec.js
    The default timeout is 2000 milliseconds. A command-line parameter can be used to overwrite it for partial testing:
    mocha my-spec.js --timeout 5000
  4. How to change the default timeout globally for all tests? 
    Mocha reads a file named test/mocha.opts by default, which can contain command line arguments. As an example, you could make a file that contains:
    --timeout 5000
    Mocha will read this file and set a timeout of 5 seconds by default whenever you run it from the command line.
  5. What does describe keyword do?
    You can only set a timeout on a per-file basis if you use the describe keyword.

Key Takeaways

In this article, we have extensively discussed test durations and timeouts for tests using Mocha and their practical implementation.
We hope that this blog has helped you enhance your knowledge regarding test durations and timeouts and if you would like to learn more, check out our articles on Coding Ninjas Studio. Do upvote our blog to help other ninjas grow. Happy Coding!

Live masterclass