Skip to content
Open
Show file tree
Hide file tree
Changes from 8 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/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const utils = require("../utils");
exports.CONFIG_FILES = [
".mocharc.cjs",
".mocharc.js",
".mocharc.mjs",
".mocharc.yaml",
".mocharc.yml",
".mocharc.jsonc",
Expand Down Expand Up @@ -72,6 +73,9 @@ exports.loadConfig = (filepath) => {
try {
if (ext === ".yml" || ext === ".yaml") {
config = parsers.yaml(filepath);
} else if (ext === ".js" || ext === ".cjs" || ext === ".mjs") {
const parsedConfig = parsers.js(filepath);
config = parsedConfig.default ?? parsedConfig;
} else if (ext === ".js" || ext === ".cjs") {
config = parsers.js(filepath);
} else {
Expand Down
10 changes: 6 additions & 4 deletions test/integration/config.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@ var loadConfig = require("../../lib/cli/config").loadConfig;
describe("config", function () {
it("should return the same values for all supported config types", function () {
var configDir = path.join(__dirname, "fixtures", "config");
var js = loadConfig(path.join(configDir, "mocharc.js"));
var js = loadConfig(path.join(configDir, "mocharc.js")); // canonical form
var cjs = loadConfig(path.join(configDir, "mocharc.cjs"));
var json = loadConfig(path.join(configDir, "mocharc.json"));
var mjs = loadConfig(path.join(configDir, "mocharc.mjs"));
var yaml = loadConfig(path.join(configDir, "mocharc.yaml"));
expect(js, "to equal", json);
expect(js, "to equal", cjs);
expect(json, "to equal", yaml);
expect(cjs, "to equal", js);
expect(json, "to equal", js);
expect(mjs, "to equal", js);
expect(yaml, "to equal", js);
});

describe('when configuring Mocha via a ".js" file', function () {
Expand Down
7 changes: 7 additions & 0 deletions test/integration/fixtures/config/mocharc.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// a comment
export default {
require: ['foo', 'bar'],
bail: true,
reporter: 'dot',
slow: 60
};
61 changes: 36 additions & 25 deletions test/node-unit/cli/config.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,23 +30,12 @@ describe("cli/config", function () {
sinon.stub(parsers, "js").returns(phonyConfigObject);
});

describe('when supplied a filepath with ".yaml" extension', function () {
const filepath = "foo.yaml";

it("should use the YAML parser", function () {
loadConfig(filepath);
expect(parsers.yaml, "to have calls satisfying", [
{ args: [filepath], returned: phonyConfigObject },
]).and("was called once");
});
});

describe('when supplied a filepath with ".yml" extension', function () {
const filepath = "foo.yml";
describe('when supplied a filepath with ".cjs" extension', function () {
const filepath = "foo.cjs";

it("should use the YAML parser", function () {
it("should use the JS parser", function () {
loadConfig(filepath);
expect(parsers.yaml, "to have calls satisfying", [
expect(parsers.js, "to have calls satisfying", [
{ args: [filepath], returned: phonyConfigObject },
]).and("was called once");
});
Expand All @@ -63,12 +52,12 @@ describe("cli/config", function () {
});
});

describe('when supplied a filepath with ".cjs" extension', function () {
const filepath = "foo.cjs";
describe('when supplied a filepath with ".json" extension', function () {
const filepath = "foo.json";

it("should use the JS parser", function () {
loadConfig(filepath);
expect(parsers.js, "to have calls satisfying", [
it("should use the JSON parser", function () {
loadConfig("foo.json");
expect(parsers.json, "to have calls satisfying", [
{ args: [filepath], returned: phonyConfigObject },
]).and("was called once");
});
Expand All @@ -85,12 +74,34 @@ describe("cli/config", function () {
});
});

describe('when supplied a filepath with ".json" extension', function () {
const filepath = "foo.json";
describe('when supplied a filepath with ".mjs" extension', function () {
const filepath = "foo.mjs";

it("should use the JSON parser", function () {
loadConfig("foo.json");
expect(parsers.json, "to have calls satisfying", [
it("should use the JS parser", function () {
loadConfig(filepath);
expect(parsers.js, "to have calls satisfying", [
{ args: [filepath], returned: phonyConfigObject },
]).and("was called once");
});
});

describe('when supplied a filepath with ".yaml" extension', function () {
const filepath = "foo.yaml";

it("should use the YAML parser", function () {
loadConfig(filepath);
expect(parsers.yaml, "to have calls satisfying", [
{ args: [filepath], returned: phonyConfigObject },
]).and("was called once");
});
});

describe('when supplied a filepath with ".yml" extension', function () {
const filepath = "foo.yml";

it("should use the YAML parser", function () {
loadConfig(filepath);
expect(parsers.yaml, "to have calls satisfying", [
{ args: [filepath], returned: phonyConfigObject },
]).and("was called once");
});
Expand Down