I'd like to set some defaults for mocha without having to type them each time. Does mocha look for a config file / dotfile anywhere, as jshint looks for .jshintrc
and npm looks for package.json
?

- 22,868
- 20
- 88
- 147
-
1The accepted answer is now deprecated. I suggest accepting the new one (https://stackoverflow.com/a/54804446/1740079) to prevent more people from using the old approach. – ichigolas Jun 11 '20 at 16:58
3 Answers
Yes. You can create a file ./test/mocha.opts
and in the file you can specify --no-colors
.
See mocha.opts on Mocha Doc for more information.

- 528
- 5
- 19

- 53,766
- 29
- 154
- 219
-
12This has been deprecated. See @migg's answer [here](https://stackoverflow.com/a/54804446/645511). – Katie Kilian Jan 13 '20 at 16:19
Mocha recommends mocha --config=.mocharc.json
.
There are new formats too, like yaml. See some examples.
Old answer:
The default is ./test/mocha.opts
. You can pass a custom path with the --opts
parameter :
mocha --opts ./mocha.opts
Useful in case you don’t store your tests in test/
folder, but next to code files, for example.
Any name and extension seems to work, so you can even do mocha --opts .mocharc
if you want it to go well with .jshintrc
, .babelrc
and the like.

- 1,276
- 16
- 20
-
2This is (or at least was), a very obscure function. I had given up hope for such a thing long ago, and thank you for pointing out it's possible now. :) – DBrown Aug 12 '16 at 01:40
-
7THANK YOU! Tests should really be next to files. Locality is important! – Byron Whitlock Sep 07 '16 at 17:47
-
1
-
It doesn’t seem like so. The CLI splits the content by spaces and then parses it with `commander` module. `commander`’s parser expects a string array. `mocha` source: https://github.com/nishigori/mocha/blob/8ba8416981993de3287b14a80b9ee9d11fe647a7/bin/_mocha#L175 - `commander` source: https://github.com/tj/commander.js/blob/d9abf56c43a93e9584e2e18a9f8fb5af552f738c/index.js#L212 – gabssnake Sep 29 '17 at 16:50
-
1You could launch `mocha` in a script and pass your JSON file contents. See: https://github.com/mochajs/mocha/wiki/Using-mocha-programmatically#set-options – gabssnake Sep 29 '17 at 16:54
-
strange, I couldnt get `--config ./mocha.opts` to work as per their official docs but `--opts ./mocha.opts` worked... thanks! – andy mccullough Jan 10 '18 at 12:53
-
This will no longer work. See: https://stackoverflow.com/questions/60283197/mocha-opts-deprecated-how-to-migrate-to-package-json – Martin Capodici Feb 25 '22 at 23:53
-
Thanks @MartinCapodici, updated the answer to reflect the newest mocha behavior – gabssnake Feb 27 '22 at 10:15
In mocha 6+ the mocha.opts
was changed to legacy
and the new place to define your configuration is a .mocharc
file that can have different formats (JSON, YAML, JS) as described in the docs or a JSON config added to the package.json
using mocha
key.
Specifying your own path to mocha config is done using --config <file>
but mocha uses any .mocharc.*
file as default in order described in the docs (JS, YAML, YML, JSON) and also automatically uses mocha
key from package.json
with lower priority than a given config file.

- 5,571
- 2
- 30
- 40
-
2An example .mocharc.js file is here: https://github.com/mochajs/mocha/blob/master/example/config/.mocharc.js (and there are other formats in that directory as well). – Peter W Jul 25 '19 at 02:50