How to change the default timeout for mocha tests?

The default time­out for mocha tests is 2000 ms. There are mul­ti­ple ways to change this:


Change time­out for a sin­gle test case

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

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

});

NOTE: Arrow func­tion will not work. As arrow func­tions binds this in the lex­i­cal scope. In the above exam­ple, this refers to the Mocha instance.
But you can use the fol­low­ing syn­tax if you use the arrow function.

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

    it('test1', () => { 
        ...
     }).timeout(5000);

});

Change time­out 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...