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
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
2 changes: 2 additions & 0 deletions test/integration/config.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@ describe("config", function () {
var configDir = path.join(__dirname, "fixtures", "config");
var js = loadConfig(path.join(configDir, "mocharc.js"));
var cjs = loadConfig(path.join(configDir, "mocharc.cjs"));
var mjs = loadConfig(path.join(configDir, "mocharc.mjs"));
var json = loadConfig(path.join(configDir, "mocharc.json"));
var yaml = loadConfig(path.join(configDir, "mocharc.yaml"));
expect(js, "to equal", json);
expect(js, "to equal", cjs);
expect(mjs, "to equal", js);
expect(json, "to equal", yaml);
});

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
};
22 changes: 22 additions & 0 deletions test/node-unit/cli/config.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,28 @@ describe("cli/config", function () {
});
});

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

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 an ESM ".js" extension', function () {
const filepath = 'foo.js';

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 ".jsonc" extension', function () {
const filepath = "foo.jsonc";

Expand Down
Loading