Skip to content

Commit

Permalink
Support a config file with a CJS extension and use it preferentially.
Browse files Browse the repository at this point in the history
  • Loading branch information
alexjeffburke committed Jul 10, 2021
1 parent 261c46e commit 330f651
Show file tree
Hide file tree
Showing 10 changed files with 55 additions and 21 deletions.
14 changes: 7 additions & 7 deletions lib/configLoaders.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
const fs = require("fs");
const path = require("path");

exports.js = configModule => rootDir => {
try {
exports.js = configModules => rootDir => {
for (const configModule of configModules) {
const file = path.join(rootDir, configModule);
const data = require(file);

return data;
} catch (e) {
// ignore
try {
return require(file);
} catch (e) {
// ignore
}
}

return null;
Expand Down
4 changes: 2 additions & 2 deletions lib/resolveConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ const isExt = ext => /^[.][a-z]+/.test(ext);
const optsToConfigLoaders = opts => {
const loaders = [];

if (opts.configModule) {
loaders.push(configLoaders.js(opts.configModule));
if (opts.configModules) {
loaders.push(configLoaders.js(opts.configModules));
}

if (opts.configFile) {
Expand Down
2 changes: 1 addition & 1 deletion register.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const resolveConfig = require("./lib/resolveConfig");
const config = resolveConfig({
cwd: process.cwd(),
configFile: "package.json",
configModule: "mocha-dominate.config.js"
configModules: ["mocha-dominate.config.cjs", "mocha-dominate.config.js"]
});

const hookEach = () => {
Expand Down
33 changes: 22 additions & 11 deletions test/integration/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,20 +105,31 @@ describe("integration", () => {
});
});

it("should work for exampleconfig", () => {
const cwd = path.join(TESTDATA, "exampleconfig");
describe("with a separate config file", () => {
it("should load config options from mocha.config.js", () => {
const cwd = path.join(TESTDATA, "exampleconfig");

return spawnMochaInDir(cwd, ["-r", "../../register"]);
});
return spawnMochaInDir(cwd, ["-r", "../../register"]);
});

it("should bail on invalid extension", () => {
const cwd = path.join(TESTDATA, "exampleconfig-bad");
it("should prefer config options from mocha.config.cjs", () => {
// if .cjs is not loaded first the test will fail as
// the fallback config file is missing the CSS transform

return expect(
() => spawnMochaInDir(cwd, ["-r", "../../register"]),
"to be rejected"
).then(err => {
expect(err.stderr, "to contain", `invalid extension "less"`);
const cwd = path.join(TESTDATA, "exampleconfig-cjs");

return spawnMochaInDir(cwd, ["-r", "../../register"]);
});

it("should bail on invalid extension", () => {
const cwd = path.join(TESTDATA, "exampleconfig-bad");

return expect(
() => spawnMochaInDir(cwd, ["-r", "../../register"]),
"to be rejected"
).then(err => {
expect(err.stderr, "to contain", `invalid extension "less"`);
});
});
});

Expand Down
1 change: 1 addition & 0 deletions testdata/exampleconfig-cjs/example.less
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
@fontColor: #333;
5 changes: 5 additions & 0 deletions testdata/exampleconfig-cjs/mocha-dominate.config.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = {
transform: {
".less": "<rootDir>/transforms/cssTransform.js"
}
};
1 change: 1 addition & 0 deletions testdata/exampleconfig-cjs/mocha-dominate.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = {};
4 changes: 4 additions & 0 deletions testdata/exampleconfig-cjs/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "exampleconfig",
"version": "0.0.0"
}
7 changes: 7 additions & 0 deletions testdata/exampleconfig-cjs/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const assert = require("assert");

describe("exampleconfig", () => {
it("should ignore less", () => {
assert.deepEqual(require("./example.less"), { cssModule: true });
});
});
5 changes: 5 additions & 0 deletions testdata/exampleconfig-cjs/transforms/cssTransform.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = {
process() {
return "module.exports = { cssModule: true };";
}
};

0 comments on commit 330f651

Please sign in to comment.