In the last tutorial, you were treated "desktop notifications", in this tutorial we will walk you through the process of configuring Mocha in node.js.
The ability to configure mocha in node.js came with the version 6.0.0 of Mocha. Added to supporting the legacy mocha.opts run-control format, mocha currently supports configuration files, this is typical of modern command-line tools, in several formats as shown below:
- YAML: You should create a .mocharc.yaml (or .mocharc.yml) in your project's root directory.
- JSON: You should create a .mocharc.json (or .mocharc.jsonc) in your project's root directory. Comments - though they are not valid JSON - are allowed in this file, and are ignored by Mocha.
- package.json: You should not create a mocha property in your project's package.json.
mocha recommends that you use one of the above strategies for configuration rather than the legacy mocha.opts format.
It is possible to specify a custom location for your configuration file; this is achieved using the ?config <path> option. Mocha uses the files extension to determine how to parse the file, and will assume JSON when the file extension is unknown.
You can also specify a custom package.json location, by using the ?package
Ignoring config files
If you want to skip the search for config files, you should use -no-config. Similarly, you can use ?no-package to stop Mocha form looking for configuration in a package.json.
When there is no custom path was given and there are multiple configuration files in the same directory, Mocha searches for - and will use - only one. The priority will be:
Mocha also merges any options found in package.json and mocha.opts into mocha's run-time configuration. However, when there is a conflict, the priority will be:
- The arguments specified on command-line
- The configuration file (.mocharc.js, .mocharc.yml, etc.)
- The mocha property of package.json
Options which can safely be repeated (e.g., --require) are concatenated, with higher-priorty configuration sources appearing earlier in the list. For instance, a .mocharc.json that contains "require": "bar", added to the execution of mocha --require foo, would make Mocha to require foo, then bar, in that order.
Configurations can inherit from other modules by using the extends keyword.
- Any "boolean" flag (which does not require a parameter, such as --bail), can be specified by using a boolean value, e.g.: "bail": true.
- Any "array"-type option (run mocha --help for a list) can be a single string value.
- For options that contains a dash (-), you can specify the option name using camelCase.
- Aliases are also valid names, e.g., R rather rthan reporter.
- You can specify Test files using spec, e.g., "spec": "test/**/*.spec.js".
- Flags to node are supported in configuration files as well, such as in mocha.opts. you should use caution, as these may vary between versions of Node.js!
- New Content published on w3resource:
- Scala Programming Exercises, Practice, Solution
- Python Itertools exercises
- Python Numpy exercises
- Python GeoPy Package exercises
- Python Pandas exercises
- Python nltk exercises
- Python BeautifulSoup exercises
- Form Template
- Composer - PHP Package Manager
- PHPUnit - PHP Testing
- Laravel - PHP Framework