225

If we have a unit test file my-spec.js and running with mocha:

mocha my-spec.js

The default timeout will be 2000 ms. It can be overwritten for partial test with a command line parameter:

mocha my-spec.js --timeout 5000

Is it possible to change the default timeout globally for all tests? i.e. the default timeout value will be different from 2000 ms when you call:

mocha my-spec.js
1

5 Answers 5

392

By default Mocha will read a file named test/mocha.opts that can contain command line arguments. So you could create such a file that contains:

--timeout 5000

Whenever you run Mocha at the command line, it will read this file and set a timeout of 5 seconds by default.

Another way which may be better depending on your situation is to set it like this in a top level describe call in your test file:

describe("something", function () {
    this.timeout(5000); 

    // tests...
});

This would allow you to set a timeout only on a per-file basis.

You could use both methods if you want a global default of 5000 but set something different for some files.


Note that you cannot generally use an arrow function if you are going to call this.timeout (or access any other member of this that Mocha sets for you). For instance, this will usually not work:

describe("something", () => {
    this.timeout(5000); //will not work

    // tests...
});

This is because an arrow function takes this from the scope the function appears in. Mocha will call the function with a good value for this but that value is not passed inside the arrow function. The documentation for Mocha says on this topic:

Passing arrow functions (“lambdas”) to Mocha is discouraged. Due to the lexical binding of this, such functions are unable to access the Mocha context.

8
  • 2
    Thanks for info. But I tried to modify mocha.opts file , but it does not affect.
    – lm.
    May 6, 2014 at 14:38
  • 3
    Did you create it in the right place? Mocha is very specific about where it wants this file. If you run Mocha in /home/me/src/proj/ then Mocha is going to search for this file: /home/me/src/proj/test/mocha.opts
    – Louis
    May 6, 2014 at 16:21
  • 8
    @JeffLowery Are you using an arrow function? Arrow functions do not establish a new this, which usually results in this.timeout failing like you show in your comment.
    – Louis
    Feb 13, 2017 at 19:39
  • 3
    @JeffLowery Use a regular function (). What Mocha passes as this is really internal state. I'm sure if you fiddle around with code you'd be able to write code that sets the timeout you want and yet uses arrow functions but that would be a brittle approach. I've edited my answer to talk about arrow functions.
    – Louis
    Feb 13, 2017 at 19:44
  • 1
    Pretty mad that Mocha actively chooses to use the insane default this-binding of Javascript as a feature. What were they thinking?
    – Timmmm
    Feb 24, 2020 at 10:53
113

Just adding to the correct answer you can set the timeout with the arrow function like this:

it('Some test', () => {

}).timeout(5000)
4
  • 10
    The question is "to change default timeout globally for all tests". Your answer only change one test. describe('suite', () => {...}).timeout(5000) doesn't work.
    – aleung
    Dec 20, 2017 at 11:02
  • As in the previous answer, the solution proposed is currently ineffective. mochajs.org/#timeouts tells to insert explicit setTimeout instructions inside the tests. Dec 16, 2018 at 14:35
  • 1
    this worked for me with mocha@5 (for a specific test) Jan 23, 2020 at 19:19
  • 3
    @MarcoFaustinelli You are misunderstanding the docs. The setTimeouts have nothing to do with Mocha. They are there to demonstrate the effects of timeout settings.
    – oligofren
    Mar 10, 2020 at 14:11
54

Adding this for completeness. If you (like me) use a script in your package.json file, just add the --timeout option to mocha:

"scripts": {
  "test": "mocha 'test/**/*.js' --timeout 10000",
  "test-debug": "mocha --debug 'test/**/*.js' --timeout 10000"
},

Then you can run npm run test to run your test suite with the timeout set to 10,000 milliseconds.

3
  • In case anyone is interested, most IDEs also allow you to inject mocha options for test execution; e.g. for WebStorm, you can enter this (i.e. "--timeout 10000") under Run->Edit Configurations->Extra Mocha Options.
    – Rubicon
    Dec 8, 2016 at 19:22
  • This is the approach to use if you find yourself playing whack-a-mole with the individual this.timeout(...) calls. Aug 1, 2022 at 15:47
  • This works with ts-mocha too. May 31 at 8:33
29

In current versions of Mocha, the timeout can be changed globally like this:

mocha.timeout(5000);

Just add the line above anywhere in your test suite, preferably at the top of your spec or in a separate test helper.


In older versions, and only in a browser, you could change the global configuration using mocha.setup.

mocha.setup({ timeout: 5000 });

The documentation does not cover the global timeout setting, but offers a few examples on how to change the timeout in other common scenarios.

3
  • 6
    This does not work in Node. See stackoverflow.com/a/47915119/893113. It seems the CLI option is the only way. Jun 15, 2018 at 2:04
  • I does not work in the browser either. As of today, the documentation linked in the answer does not mention any timeout parameter. On the contrary, mochajs.org/#timeouts tells to insert explicit setTimeout instructions inside the tests. Dec 16, 2018 at 14:34
  • @MarcoFaustinelli You are misunderstanding the docs. The setTimeouts have nothing to do with Mocha. They are there to demonstrate the effects of timeout settings.
    – oligofren
    Mar 10, 2020 at 14:11
0

Following worked for me in TypeScript:

describe('WriteCSV', () => {
  it('should write the CSV file correctly', async function (this: Mocha.Context) 
  {
    this.timeout(1000);
  });
});

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.