How to increase timeout for a single test case in mocha

Javascriptmocha.js

Javascript Problem Overview


I'm submitting a network request in a test case, but this sometimes takes longer than 2 seconds (the default timeout).

How do I increase the timeout for a single test case?

Javascript Solutions


Solution 1 - Javascript

Here you go: http://mochajs.org/#test-level

it('accesses the network', function(done){
  this.timeout(500);
  [Put network code here, with done() in the callback]
})

For arrow function use as follows:

it('accesses the network', (done) => {
  [Put network code here, with done() in the callback]
}).timeout(500);

Solution 2 - Javascript

If you wish to use es6 arrow functions you can add a .timeout(ms) to the end of your it definition:

it('should not timeout', (done) => {
    doLongThing().then(() => {
        done();
    });
}).timeout(5000);

At least this works in Typescript.

Solution 3 - Javascript

(since I ran into this today)

Be careful when using ES2015 fat arrow syntax:

This will fail :

it('accesses the network', done => {

  this.timeout(500); // will not work
  
  // *this* binding refers to parent function scope in fat arrow functions!
  // i.e. the *this* object of the describe function

  done();
});

EDIT: Why it fails:

As @atoth mentions in the comments, fat arrow functions do not have their own this binding. Therefore, it's not possible for the it function to bind to this of the callback and provide a timeout function.

Bottom line: Don't use arrow functions for functions that need an increased timeout.

Solution 4 - Javascript

If you are using in NodeJS then you can set timeout in package.json

"test": "mocha --timeout 10000"

then you can run using npm like:

npm test

Solution 5 - Javascript

From command line:

mocha -t 100000 test.js

Solution 6 - Javascript

You might also think about taking a different approach, and replacing the call to the network resource with a stub or mock object. Using Sinon, you can decouple the app from the network service, focusing your development efforts.

Solution 7 - Javascript

For test navigation on Express:

const request = require('supertest');
const server = require('../bin/www');

describe('navigation', () => {
    it('login page', function(done) {
        this.timeout(4000);
        const timeOut = setTimeout(done, 3500);

        request(server)
            .get('/login')
            .expect(200)
            .then(res => {
                res.text.should.include('Login');
                clearTimeout(timeOut);
                done();
            })
            .catch(err => {
                console.log(this.test.fullTitle(), err);
                clearTimeout(timeOut);
                done(err);
            });
    });
});

In the example the test time is 4000 (4s).

Note: setTimeout(done, 3500) is minor for than done is called within the time of the test but clearTimeout(timeOut) it avoid than used all these time.

Solution 8 - Javascript

This worked for me! Couldn't find anything to make it work with before()

describe("When in a long running test", () => {
  it("Should not time out with 2000ms", async () => {
    let service = new SomeService();
    let result = await service.callToLongRunningProcess();
    expect(result).to.be.true;
  }).timeout(10000); // Custom Timeout 
});

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMahendra SView Question on Stackoverflow
Solution 1 - JavascriptDan KohnView Answer on Stackoverflow
Solution 2 - JavascriptChris SparrowView Answer on Stackoverflow
Solution 3 - JavascriptchriskellyView Answer on Stackoverflow
Solution 4 - Javascriptuser2513697View Answer on Stackoverflow
Solution 5 - JavascriptandreyView Answer on Stackoverflow
Solution 6 - JavascriptDavid SoutherView Answer on Stackoverflow
Solution 7 - JavascriptalditisView Answer on Stackoverflow
Solution 8 - JavascriptOtpidusView Answer on Stackoverflow