Change default timeout for mocha
Asked Answered
R

6

231

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
Roush answered 6/5, 2014 at 10:23 Comment(1)
Mocha's official website also explains more on this.Dropsy
G
401

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.

Gabler answered 6/5, 2014 at 10:42 Comment(8)
Thanks for info. But I tried to modify mocha.opts file , but it does not affect.Roush
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.optsGabler
this.timeout(10000); // default timeout ^ TypeError: this.timeout is not a function at Suite.<anonymous> (/Users/jeff.l/Documents/workspace/unit-tests/mocha-chai_tests/checkoutTest.js:12:10)Paleontology
@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.Gabler
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.Paleontology
@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.Gabler
Pretty mad that Mocha actively chooses to use the insane default this-binding of Javascript as a feature. What were they thinking?Hawfinch
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.Adrien
R
115

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

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

}).timeout(5000)
Radial answered 20/7, 2017 at 16:18 Comment(4)
The question is "to change default timeout globally for all tests". Your answer only change one test. describe('suite', () => {...}).timeout(5000) doesn't work.Vanadous
As in the previous answer, the solution proposed is currently ineffective. mochajs.org/#timeouts tells to insert explicit setTimeout instructions inside the tests.Liveried
this worked for me with mocha@5 (for a specific test)Karaite
@MarcoFaustinelli You are misunderstanding the docs. The setTimeouts have nothing to do with Mocha. They are there to demonstrate the effects of timeout settings.Educationist
S
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.

Sommerville answered 29/8, 2016 at 22:22 Comment(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.Subadar
This is the approach to use if you find yourself playing whack-a-mole with the individual this.timeout(...) calls.Cestode
This works with ts-mocha too.Quizmaster
P
30

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.

Popper answered 6/7, 2015 at 17:8 Comment(3)
This does not work in Node. See https://mcmap.net/q/119846/-is-there-a-way-to-get-current-mocha-instance-and-edit-options-at-runtime. It seems the CLI option is the only way.Removal
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.Liveried
@MarcoFaustinelli You are misunderstanding the docs. The setTimeouts have nothing to do with Mocha. They are there to demonstrate the effects of timeout settings.Educationist
P
2

It seems like every answer here is outdated. mocha.opts has been deprecated and no longer works. While you can still pass --timeout 5000 to the mocha command, the right way to change the timeout globally for your project is to configure it in package.json, like so:

"mocha": {
  "timeout": 5000
}

Alternatively, you could configure it in .mocharc.json (or .js, or .yml), but I think package.json is preferable.

Prevot answered 1/3 at 13:7 Comment(0)
W
0

Following worked for me in TypeScript:

describe('WriteCSV', () => {
  it('should write the CSV file correctly', async function (this: Mocha.Context) 
  {
    this.timeout(1000);
  });
});
Woodall answered 1/8, 2023 at 7:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.