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
Bryan
  • 2,870
  • 24
  • 39
  • 44
lm.
  • 4,033
  • 4
  • 25
  • 37

5 Answers5

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.

Nawaz
  • 353,942
  • 115
  • 666
  • 851
Louis
  • 146,715
  • 28
  • 274
  • 320
  • 2
    Thanks for info. But I tried to modify mocha.opts file , but it does not affect. – lm. May 06 '14 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 06 '14 at 16:21
  • this.timeout(10000); // default timeout ^ TypeError: this.timeout is not a function at Suite. (/Users/jeff.l/Documents/workspace/unit-tests/mocha-chai_tests/checkoutTest.js:12:10) – Jeff Lowery Feb 13 '17 at 19:38
  • 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 '17 at 19:39
  • Yes, you are correct, @Louis. It would be nice if I could grab describe's this reference, but it's not obvious how to do that. – Jeff Lowery Feb 13 '17 at 19:42
  • 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 '17 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 '20 at 10:53
  • I generally maintain a global `before` function as part of my tests. This way it is not maintained for a file, but can be maintained for the entire set of testing. This is the kind of thing that will make its way in there. – Jefferey Cave Dec 11 '21 at 19:12
112

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

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

}).timeout(5000)
Denis
  • 2,030
  • 1
  • 12
  • 15
  • 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 '17 at 11:02
  • As in the previous answer, the solution proposed is currently ineffective. https://mochajs.org/#timeouts tells to insert explicit `setTimeout` instructions inside the tests. – Marco Faustinelli Dec 16 '18 at 14:35
  • 1
    this worked for me with mocha@5 (for a specific test) – Fernando Gabrieli Jan 23 '20 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 '20 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.

Freedom_Ben
  • 11,247
  • 10
  • 69
  • 89
  • 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 08 '16 at 19:22
  • This is the approach to use if you find yourself playing whack-a-mole with the individual `this.timeout(...)` calls. – Eric Walker Aug 01 '22 at 15:47
  • This works with ts-mocha too. – Alankar Misra May 31 '23 at 08: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.

GOTO 0
  • 42,323
  • 22
  • 125
  • 158
  • 6
    This does not work in Node. See https://stackoverflow.com/a/47915119/893113. It seems the CLI option is the only way. – paulmelnikow Jun 15 '18 at 02: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, https://mochajs.org/#timeouts tells to insert explicit `setTimeout` instructions inside the tests. – Marco Faustinelli Dec 16 '18 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 '20 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);
  });
});