Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions lib/cli/options.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,16 @@ export const loadPkgRc = (args = {}) => {
* @alias module:lib/cli.loadOptions
* @returns {external:yargsParser.Arguments} Parsed args from everything
*/
// Reads `reporter-option` from a config source under any of its known
// aliases. Keeps `loadOptions` agnostic of how each source was authored.
const readReporterOption = (source) =>
source &&
(source["reporter-option"] ??
source["reporter-options"] ??
source.reporterOption ??
source.reporterOptions ??
source.O);

export const loadOptions = (argv = []) => {
let args = parse(argv);
// short-circuit: look for a flag that would abort loading of options
Expand All @@ -299,6 +309,15 @@ export const loadOptions = (argv = []) => {
const rcConfig = loadRc(args);
const pkgConfig = loadPkgRc(args);

// Capture per-source `reporter-option` before yargs-parser's `combine-arrays`
// flattens them and loses provenance. See the re-merge step below for why.
const reporterOptionSources = {
pkg: readReporterOption(pkgConfig),
rc: readReporterOption(rcConfig),
env: readReporterOption(envConfig),
cli: readReporterOption(args),
};

if (rcConfig) {
args.config = false;
args._ = args._.concat(rcConfig._ || []);
Expand All @@ -317,6 +336,20 @@ export const loadOptions = (argv = []) => {
pkgConfig || {},
);

// `combine-arrays` concatenates `reporter-option` from every source in
// high→low priority order. The downstream coerce in `lib/cli/run.js` reduces
// it left-to-right with last-writer-wins, which inverts the intended
// precedence. Re-emit lowest→highest priority so CLI overrides config.
const orderedReporterOption = [
reporterOptionSources.pkg,
reporterOptionSources.rc,
reporterOptionSources.env,
reporterOptionSources.cli,
].reduce((acc, src) => (src ? acc.concat(list(src)) : acc), []);
if (orderedReporterOption.length) {
args["reporter-option"] = orderedReporterOption;
}

// recombine positional arguments and "spec"
if (args.spec) {
args._ = args._.concat(args.spec);
Expand Down
3 changes: 3 additions & 0 deletions test/integration/fixtures/options/reporter-option-mocharc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
reporter-options:
- foo=fromConfig
- extra=keepMe
42 changes: 42 additions & 0 deletions test/integration/options/reporter-option.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,46 @@ describe("--reporter-option", function () {
);
});
});

describe("when given options on the CLI and in a config file", function () {
var reporterFixture = path.join(
__dirname,
"..",
"fixtures",
"options",
"reporter-with-options.fixture.js",
);
var configFixture = path.join(
__dirname,
"..",
"fixtures",
"options",
"reporter-option-mocharc.yml",
);

it("should allow the CLI to override clashing keys from the config", function (done) {
runMocha(
"passing.fixture.js",
[
"--config",
configFixture,
"--reporter",
reporterFixture,
"--reporter-option",
"foo=fromCli",
],
function (err, res) {
if (err) {
return done(err);
}
expect(res, "to have passed").and(
"to contain output",
/{"foo":"fromCli","extra":"keepMe"}/,
);
done();
},
"pipe",
);
});
});
});
Loading