lm. lm. - 1 month ago 14
Javascript Question

Change default timeout for mocha

I have following question
if we have unit test file my-spec.js and running with mocha :

mocha my-spec.js

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

mocha my-spec.js --timeout 5000

The question is:
Is there possible to change default timeout globally for all tests ?
I.e when you call

mocha my-spec.js

the default timeout value to be different from 2000 ms
Thanks in advance


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 () {

    // 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.