☕️ Classic, reliable, trusted test framework for Node.js and the browser
Soft deprecate configuration via "mocha.opts" (#3968)
Juerg Bf15e9884eba8
diff --git a/docs/index.md b/docs/index.mdindex 3e00e346..be1511ef 100644--- a/docs/index.md+++ b/docs/index.md@@ -1065,7 +1065,7 @@ By default, Mocha will search for a config file if `--config` is not specified;### `--opts <path>`-> _Updated in v6.0.0; added `--no-opts`._+> _Deprecated._Specify a path to [`mocha.opts`](#mochaopts).@@ -1636,14 +1636,14 @@ tests as shown below:> _New in v6.0.0_-In addition to supporting the legacy [`mocha.opts`](#mochaopts) run-control format, Mocha now supports configuration files, typical of modern command-line tools, in several formats:+In addition to supporting the deprecated [`mocha.opts`](#mochaopts) run-control format, Mocha now supports configuration files, typical of modern command-line tools, in several formats:- **JavaScript**: Create a `.mocharc.js` in your project's root directory, and export an object (`module.exports = {/* ... */}`) containing your configuration.- **YAML**: Create a `.mocharc.yaml` (or `.mocharc.yml`) in your project's root directory.- **JSON**: Create a `.mocharc.json` (or `.mocharc.jsonc`) in your project's root directory. Comments — while not valid JSON — are allowed in this file, and will be ignored by Mocha.- **package.json**: Create a `mocha` property in your project's `package.json`.-Mocha suggests using one of the above strategies for configuration instead of the legacy `mocha.opts` format.+Mocha suggests using one of the above strategies for configuration instead of the deprecated `mocha.opts` format.### Custom Locations@@ -1693,7 +1693,7 @@ Configurations can inherit from other modules using the `extends` keyword. See [## `mocha.opts`-> _Updated in v6.0.0; `mocha.opts` is now considered "legacy" — though not yet deprecated — and we recommend using a configuration file instead._+> _`mocha.opts` file support is DEPRECATED and will be removed from a future version of Mocha. We recommend using a configuration file instead._Mocha will attempt to load `"./test/mocha.opts"` as a run-control file of sorts.diff --git a/example/config/.mocharc.js b/example/config/.mocharc.jsindex 5d9c7fa7..9b58a28d 100644--- a/example/config/.mocharc.js+++ b/example/config/.mocharc.js@@ -7,7 +7,7 @@module.exports = {diff: true,extension: ['js'],- opts: './test/mocha.opts',+ opts: false,package: './package.json',reporter: 'spec',slow: 75,diff --git a/example/config/.mocharc.json b/example/config/.mocharc.jsonindex 07f954da..5fa8f762 100644--- a/example/config/.mocharc.json+++ b/example/config/.mocharc.json@@ -5,7 +5,7 @@{"diff": true,"extension": ["js"],- "opts": "./test/mocha.opts",+ "opts": false,"package": "./package.json","reporter": "spec","slow": 75,diff --git a/example/config/.mocharc.jsonc b/example/config/.mocharc.jsoncindex d86055f8..49152a18 100644--- a/example/config/.mocharc.jsonc+++ b/example/config/.mocharc.jsonc@@ -5,7 +5,7 @@{"diff": true,"extension": ["js"],- "opts": "./test/mocha.opts",+ "opts": false,"package": /* 📦 */ "./package.json","reporter": /* 📋 */ "spec","slow": 75,diff --git a/example/config/.mocharc.yml b/example/config/.mocharc.ymlindex 466b4956..4004d477 100644--- a/example/config/.mocharc.yml+++ b/example/config/.mocharc.yml@@ -28,7 +28,7 @@ ignore:inline-diffs: false# needs to be used with grep or fgrep# invert: false-opts: './test/mocha.opts'+opts: falserecursive: falsereporter: specreporter-option:diff --git a/lib/cli/options.js b/lib/cli/options.jsindex ffbca063..c87552c5 100644--- a/lib/cli/options.js+++ b/lib/cli/options.js@@ -15,6 +15,7 @@ const mocharc = require('../mocharc.json');const {list} = require('./run-helpers');const {loadConfig, findConfig} = require('./config');const findUp = require('find-up');+const {deprecate} = require('../utils');const debug = require('debug')('mocha:cli:options');const {isNodeFlag} = require('./node-flags');@@ -196,6 +197,9 @@ const loadMochaOpts = (args = {}) => {// if there's an exception to catch here, I'm not sure what it is.// by attaching the `no-opts` arg, we avoid re-parsing of `mocha.opts`.if (mochaOpts) {+ deprecate(+ 'Configuration via mocha.opts is DEPRECATED and will be removed from a future version of Mocha. Use RC files or package.json instead.'+ );result = parse(parseMochaOpts(mochaOpts));debug(`${filepath} parsed succesfully`);}diff --git a/lib/cli/run.js b/lib/cli/run.jsindex dc94c101..b0a16e6b 100644--- a/lib/cli/run.js+++ b/lib/cli/run.js@@ -166,7 +166,7 @@ exports.builder = yargs =>},opts: {default: defaults.opts,- description: 'Path to `mocha.opts`',+ description: 'Path to `mocha.opts` (DEPRECATED)',group: GROUPS.CONFIG,normalize: true,requiresArg: truediff --git a/test/integration/options/opts.spec.js b/test/integration/options/opts.spec.jsindex 51f49fec..e3d98581 100644--- a/test/integration/options/opts.spec.js+++ b/test/integration/options/opts.spec.js@@ -10,6 +10,24 @@ describe('--opts', function() {var args = [];var fixture = path.join('options', 'opts');+ it('should print a deprecation warning', function(done) {+ var mochaOpts = path.join('test', 'opts', 'mocha.opts');+ args = [resolvePath(fixture), '--opts', mochaOpts];+ invokeMocha(+ args,+ function(err, res) {+ if (err) {+ return done(err);+ }++ expect(res, 'to have passed');+ expect(res.output, 'to contain', 'mocha.opts is DEPRECATED');+ done();+ },+ 'pipe'+ );+ });+it('should work despite nonexistent default options file', function(done) {args = [];runMochaJSON(fixture, args, function(err, res) {