Skip to content

Commit 5b7af5e

Browse files
feat: add MOCHA_OPTIONS env variable (#4835)
Co-authored-by: Josh Goldberg ✨ <[email protected]>
1 parent 472a8be commit 5b7af5e

File tree

3 files changed

+73
-7
lines changed

3 files changed

+73
-7
lines changed

docs/index.md

+11
Original file line numberDiff line numberDiff line change
@@ -2214,11 +2214,22 @@ If no custom path was given, and if there are multiple configuration files in th
22142214
1. `.mocharc.jsonc`
22152215
1. `.mocharc.json`
22162216

2217+
### Environment Variables
2218+
2219+
The `MOCHA_OPTIONS` environment variable may be used to specify command line arguments. These arguments take priority over those found in configuration files.
2220+
2221+
For example, setting the `bail` and `retries` options:
2222+
2223+
```bash
2224+
$ MOCHA_OPTIONS="--bail --retries 3" mocha
2225+
```
2226+
22172227
### Merging
22182228

22192229
Mocha will also _merge_ any options found in `package.json` into its run-time configuration. In case of conflict, the priority is:
22202230

22212231
1. Arguments specified on command-line
2232+
1. Arguments specified in `MOCHA_OPTIONS` environment variable.
22222233
1. Configuration file (`.mocharc.js`, `.mocharc.yml`, etc.)
22232234
1. `mocha` property of `package.json`
22242235

lib/cli/options.js

+13-4
Original file line numberDiff line numberDiff line change
@@ -208,9 +208,10 @@ module.exports.loadPkgRc = loadPkgRc;
208208
* Priority list:
209209
*
210210
* 1. Command-line args
211-
* 2. RC file (`.mocharc.c?js`, `.mocharc.ya?ml`, `mocharc.json`)
212-
* 3. `mocha` prop of `package.json`
213-
* 4. default configuration (`lib/mocharc.json`)
211+
* 2. `MOCHA_OPTIONS` environment variable.
212+
* 3. RC file (`.mocharc.c?js`, `.mocharc.ya?ml`, `mocharc.json`)
213+
* 4. `mocha` prop of `package.json`
214+
* 5. default configuration (`lib/mocharc.json`)
214215
*
215216
* If a {@link module:lib/cli/one-and-dones.ONE_AND_DONE_ARGS "one-and-done" option} is present in the `argv` array, no external config files will be read.
216217
* @summary Parses options read from `.mocharc.*` and `package.json`.
@@ -231,6 +232,7 @@ const loadOptions = (argv = []) => {
231232
return args;
232233
}
233234

235+
const envConfig = parse(process.env.MOCHA_OPTIONS || '');
234236
const rcConfig = loadRc(args);
235237
const pkgConfig = loadPkgRc(args);
236238

@@ -243,7 +245,14 @@ const loadOptions = (argv = []) => {
243245
args._ = args._.concat(pkgConfig._ || []);
244246
}
245247

246-
args = parse(args._, mocharc, args, rcConfig || {}, pkgConfig || {});
248+
args = parse(
249+
args._,
250+
mocharc,
251+
args,
252+
envConfig,
253+
rcConfig || {},
254+
pkgConfig || {}
255+
);
247256

248257
// recombine positional arguments and "spec"
249258
if (args.spec) {

test/node-unit/cli/options.spec.js

+49-3
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,10 @@ describe('options', function () {
4242
/**
4343
* Order of priority:
4444
* 1. Command-line args
45-
* 2. RC file (`.mocharc.js`, `.mocharc.ya?ml`, `mocharc.json`)
46-
* 3. `mocha` prop of `package.json`
47-
* 4. default rc
45+
* 2. `MOCHA_OPTIONS` environment variable
46+
* 3. RC file (`.mocharc.js`, `.mocharc.ya?ml`, `mocharc.json`)
47+
* 4. `mocha` prop of `package.json`
48+
* 5. default rc
4849
*/
4950
describe('loadOptions()', function () {
5051
describe('when no parameter provided', function () {
@@ -408,6 +409,30 @@ describe('options', function () {
408409
});
409410
});
410411

412+
describe('env options', function () {
413+
it('should parse flags from MOCHA_OPTIONS', function () {
414+
readFileSync = sinon.stub().onFirstCall().returns('{}');
415+
findConfig = sinon.stub().returns('/some/.mocharc.json');
416+
loadConfig = sinon.stub().returns({});
417+
findupSync = sinon.stub().returns('/some/package.json');
418+
sinon
419+
.stub(process, 'env')
420+
.value({MOCHA_OPTIONS: '--retries 42 --color'});
421+
422+
loadOptions = proxyLoadOptions({
423+
readFileSync,
424+
findConfig,
425+
loadConfig,
426+
findupSync
427+
});
428+
429+
expect(loadOptions(), 'to satisfy', {
430+
retries: 42,
431+
color: true
432+
});
433+
});
434+
});
435+
411436
describe('config priority', function () {
412437
it('should prioritize package.json over defaults', function () {
413438
readFileSync = sinon.stub();
@@ -474,6 +499,27 @@ describe('options', function () {
474499
'500'
475500
);
476501
});
502+
503+
it('should prioritize env over rc file', function () {
504+
readFileSync = sinon.stub();
505+
readFileSync.onFirstCall().returns('{}');
506+
readFileSync.onSecondCall().returns('');
507+
findConfig = sinon.stub().returns('/some/.mocharc.json');
508+
loadConfig = sinon.stub().returns({retries: 300});
509+
findupSync = sinon.stub().returns('/some/package.json');
510+
sinon
511+
.stub(process, 'env')
512+
.value({MOCHA_OPTIONS: '--retries 800 --color'});
513+
514+
loadOptions = proxyLoadOptions({
515+
readFileSync,
516+
findConfig,
517+
loadConfig,
518+
findupSync
519+
});
520+
521+
expect(loadOptions(), 'to have property', 'retries', 800);
522+
});
477523
});
478524

479525
describe('when called with a one-and-done arg', function () {

0 commit comments

Comments
 (0)