Skip to content

armgit / mocha

Public Imported from GitHub
0

☕️ Classic, reliable, trusted test framework for Node.js and the browser

fix: give CLI reporter-option precedence over config file (#6331)

Minouccc571110f0e87a
diff --git a/lib/cli/parse-args.js b/lib/cli/parse-args.js
index bb6aaf89..41fa3198 100644
--- a/lib/cli/parse-args.js
+++ b/lib/cli/parse-args.js
@@ -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]);
}
diff --git a/test/integration/options/reporter-option.spec.cjs b/test/integration/options/reporter-option.spec.cjs
index 8d2fd51f..995c8a74 100644
--- a/test/integration/options/reporter-option.spec.cjs
+++ b/test/integration/options/reporter-option.spec.cjs
@@ -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-"),
diff --git a/test/node-unit/cli/options.spec.js b/test/node-unit/cli/options.spec.js
index 82ddaad7..c3fec348 100644
--- a/test/node-unit/cli/options.spec.js
+++ b/test/node-unit/cli/options.spec.js
@@ -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("{}");
diff --git a/test/node-unit/cli/parse-args.spec.js b/test/node-unit/cli/parse-args.spec.js
index f3c4e9c4..8d13a625 100644
--- a/test/node-unit/cli/parse-args.spec.js
+++ b/test/node-unit/cli/parse-args.spec.js
@@ -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(