Is there a .mocha file where I can specify defaults such as --no-colors?
Solution 1
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
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
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.
Comments
-
mcandre almost 2 years
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 forpackage.json
?-
ichigolas about 4 yearsThe accepted answer is now deprecated. I suggest accepting the new one (stackoverflow.com/a/54804446/1740079) to prevent more people from using the old approach.
-
-
DBrown almost 8 yearsThis 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. :)
-
Byron Whitlock almost 8 yearsTHANK YOU! Tests should really be next to files. Locality is important!
-
Jared Dykstra almost 7 yearsIs it possible for the contents of the file to be JSON?
-
gabssnake over 6 yearsIt 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: github.com/nishigori/mocha/blob/… -commander
source: github.com/tj/commander.js/blob/… -
gabssnake over 6 yearsYou could launch
mocha
in a script and pass your JSON file contents. See: github.com/mochajs/mocha/wiki/… -
andy mccullough over 6 yearsstrange, I couldnt get
--config ./mocha.opts
to work as per their official docs but--opts ./mocha.opts
worked... thanks! -
Peter W almost 5 yearsAn example .mocharc.js file is here: github.com/mochajs/mocha/blob/master/example/config/.mocharc.js (and there are other formats in that directory as well).
-
Katie Kilian over 4 yearsThis has been deprecated. See @migg's answer here.
-
Martin Capodici over 2 yearsThis will no longer work. See: stackoverflow.com/questions/60283197/…
-
gabssnake over 2 yearsThanks @MartinCapodici, updated the answer to reflect the newest mocha behavior