Skip to content

Commit

Permalink
feat: add MOCHA_OPTIONS env variable
Browse files Browse the repository at this point in the history
  • Loading branch information
icholy committed Feb 25, 2022
1 parent 547ffd7 commit d294942
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 3 deletions.
13 changes: 11 additions & 2 deletions lib/cli/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,8 @@ module.exports.loadPkgRc = loadPkgRc;
* 1. Command-line args
* 2. RC file (`.mocharc.c?js`, `.mocharc.ya?ml`, `mocharc.json`)
* 3. `mocha` prop of `package.json`
* 4. default configuration (`lib/mocharc.json`)
* 4. `MOCHA_OPTIONS` environment variable.
* 5. default configuration (`lib/mocharc.json`)
*
* 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.
* @summary Parses options read from `.mocharc.*` and `package.json`.
Expand All @@ -233,6 +234,7 @@ const loadOptions = (argv = []) => {

const rcConfig = loadRc(args);
const pkgConfig = loadPkgRc(args);
const envConfig = parse(process.env.MOCHA_OPTIONS || '');

if (rcConfig) {
args.config = false;
Expand All @@ -243,7 +245,14 @@ const loadOptions = (argv = []) => {
args._ = args._.concat(pkgConfig._ || []);
}

args = parse(args._, mocharc, args, rcConfig || {}, pkgConfig || {});
args = parse(
args._,
mocharc,
args,
rcConfig || {},
pkgConfig || {},
envConfig
);

// recombine positional arguments and "spec"
if (args.spec) {
Expand Down
48 changes: 47 additions & 1 deletion test/node-unit/cli/options.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ describe('options', function () {
* 1. Command-line args
* 2. RC file (`.mocharc.js`, `.mocharc.ya?ml`, `mocharc.json`)
* 3. `mocha` prop of `package.json`
* 4. default rc
* 4. `MOCHA_OPTIONS` environment variable
* 5. default rc
*/
describe('loadOptions()', function () {
describe('when no parameter provided', function () {
Expand Down Expand Up @@ -408,6 +409,30 @@ describe('options', function () {
});
});

describe('env options', function () {
it('should parse flags from MOCHA_OPTIONS', function () {
readFileSync = sinon.stub().onFirstCall().returns('{}');
findConfig = sinon.stub().returns('/some/.mocharc.json');
loadConfig = sinon.stub().returns({});
findupSync = sinon.stub().returns('/some/package.json');
sinon
.stub(process, 'env')
.value({MOCHA_OPTIONS: '--retries 42 --color'});

loadOptions = proxyLoadOptions({
readFileSync,
findConfig,
loadConfig,
findupSync
});

expect(loadOptions(), 'to satisfy', {
retries: 42,
color: true
});
});
});

describe('config priority', function () {
it('should prioritize package.json over defaults', function () {
readFileSync = sinon.stub();
Expand Down Expand Up @@ -474,6 +499,27 @@ describe('options', function () {
'500'
);
});

it('should prioritize rc over env file', function () {
readFileSync = sinon.stub();
readFileSync.onFirstCall().returns('{}');
readFileSync.onSecondCall().returns('');
findConfig = sinon.stub().returns('/some/.mocharc.json');
loadConfig = sinon.stub().returns({retries: 300});
findupSync = sinon.stub().returns('/some/package.json');
sinon
.stub(process, 'env')
.value({MOCHA_OPTIONS: '--retries 800 --color'});

loadOptions = proxyLoadOptions({
readFileSync,
findConfig,
loadConfig,
findupSync
});

expect(loadOptions(), 'to have property', 'retries', 300);
});
});

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

0 comments on commit d294942

Please sign in to comment.