How to change the default timeout for mocha tests?

The default timeout for mocha tests is 2000 ms. There are multiple ways to change 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);
        ...
     });

});

NOTE: Arrow function will not work. As arrow functions binds this in the lexical scope. In the above example, this refers to the Mocha instance.
But you can use the following syntax if you use the arrow function.

describe("testing promises", () => {

    it('test1', () => { 
        ...
     }).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 });

You may also like...