Is there a .mocha file where I can specify defaults such as --no-colors?

node.jsConfigurationConfigmocha.jsDotfiles

node.js Problem Overview


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?

node.js Solutions


Solution 1 - node.js

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.

Solution 2 - node.js

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.

Solution 3 - node.js

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionmcandreView Question on Stackoverflow
Solution 1 - node.jszs2020View Answer on Stackoverflow
Solution 2 - node.jsgabssnakeView Answer on Stackoverflow
Solution 3 - node.jscrackmiggView Answer on Stackoverflow