Skip to content
Merged
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
4 changes: 4 additions & 0 deletions lib/cli/parse-args.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,10 @@ const mergeConfigObjects = (defaultValues, configObjects) => {
} else if (defaultArrayOptions.has(name)) {
defaultArrayOptions.delete(name);
merged[name] = value;
} else if (name === "reporter-option") {
// last-writer-wins downstream, so lower-priority sources come first
// https://github.com/mochajs/mocha/issues/5532
merged[name] = mergeValue(merged[name], value);
} else {
merged[name] = mergeValue(value, merged[name]);
}
Expand Down
33 changes: 33 additions & 0 deletions test/integration/options/reporter-option.spec.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,39 @@ describe("--reporter-option", function () {
);
});

it("should allow the CLI to override clashing keys from the config file", function (done) {
var tmpdir = fs.mkdtempSync(
path.join(os.tmpdir(), "mocha-reporter-option-"),
);
fs.writeFileSync(
path.join(tmpdir, ".mocharc.yml"),
[
`reporter: ${customReporter}`,
"reporter-option:",
" - foo=fromConfig",
" - extra=keepMe",
`spec: ${path.join(__dirname, "..", "fixtures", "passing.fixture.cjs")}`,
"",
].join("\n"),
);

invokeMocha(
["--reporter-option", "foo=fromCli"],
function (err, res) {
rimraf.sync(tmpdir);
if (err) {
return done(err);
}
expect(res, "to have passed").and(
"to contain output",
/{"foo":"fromCli","extra":"keepMe"}/,
);
done();
},
{ cwd: tmpdir, stdio: "pipe" },
);
});

it("should preserve package config reporter options after respawning with a Node option", function (done) {
var tmpdir = fs.mkdtempSync(
path.join(os.tmpdir(), "mocha-reporter-option-"),
Expand Down
31 changes: 31 additions & 0 deletions test/node-unit/cli/options.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,37 @@ describe("options", function () {
});
});

describe("reporter-option", function () {
let result;

beforeEach(function () {
readFileSync = sinon.stub();
readFileSync.onFirstCall().returns("{}");
findConfig = sinon.stub().returns("/some/.mocharc.json");
loadConfig = sinon.stub().returns({
"reporter-option": ["output=config.xml", "extra=keepMe"],
});
findupSync = sinon.stub().returns("/some/package.json");

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

result = loadOptions(["--reporter-option", "output=cli.xml"]);
});

it("should merge rc reporter options before CLI reporter options", function () {
expect(result, "to have property", "reporter-option", [
"output=config.xml",
"extra=keepMe",
"output=cli.xml",
]);
});
});

describe("env options", function () {
it("should parse flags from MOCHA_OPTIONS", function () {
readFileSync = sinon.stub().onFirstCall().returns("{}");
Expand Down
23 changes: 23 additions & 0 deletions test/node-unit/cli/parse-args.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,29 @@ describe("parse-args", function () {
);
});

it("orders reporter-option values from lowest to highest priority source", function () {
expect(
parseMochaArgs(
[],
defaults,
{ "reporter-option": ["output=cli.xml"] },
{ "reporter-option": ["output=env.xml"] },
{ "reporter-option": ["output=config.xml", "extra=keepMe"] },
{ "reporter-option": ["output=pkg.xml"] },
),
"to satisfy",
{
"reporter-option": [
"output=pkg.xml",
"output=config.xml",
"extra=keepMe",
"output=env.xml",
"output=cli.xml",
],
},
);
});

it("preserves boolean negation including long alias negation", function () {
expect(
parseMochaArgs(
Expand Down
Loading