Skip to content

Commit 86305cf

Browse files
authored
fix: wrong error thrown while loading config files (#4832)
1 parent 11c4560 commit 86305cf

File tree

4 files changed

+30
-14
lines changed

4 files changed

+30
-14
lines changed

.github/ISSUE_TEMPLATE/bug_report.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ Place an `x` between the square brackets on the lines below for every satisfied
2222
- [ ] Checked that your issue hasn't already been filed by cross-referencing [issues with the `faq` label](https://github.com/mochajs/mocha/issues?utf8=%E2%9C%93&q=is%3Aissue%20label%3Afaq%20)
2323
- [ ] Checked next-gen ES issues and syntax problems by using the same environment and/or transpiler configuration without Mocha to ensure it isn't just a feature that actually isn't supported in the environment in question or a bug in your code.
2424
- [ ] 'Smoke tested' the code to be tested by running it outside the real test suite to get a better sense of whether the problem is in the code under test, your usage of Mocha, or Mocha itself
25-
- [ ] Ensured that there is no discrepancy between the locally and globally installed versions of Mocha. You can find them with: `node node_modules/.bin/mocha --version`(Local) and `mocha --version`(Global). We recommend that you _not_ install Mocha globally.
25+
- [ ] Ensured that there is no discrepancy between the locally and globally installed versions of Mocha. You can find them with: `node_modules/.bin/mocha --version`(Local) and `mocha --version`(Global). We recommend that you _not_ install Mocha globally.
2626

2727
### Description
2828

@@ -51,7 +51,7 @@ Scrub if needed so as not to reveal passwords, etc.
5151

5252
<!-- If applicable, please specify: -->
5353

54-
- The output of `mocha --version` and `node node_modules/.bin/mocha --version`:
54+
- The output of `mocha --version` and `node_modules/.bin/mocha --version`:
5555
- The output of `node --version`:
5656
- Your operating system
5757
- name and version:

lib/cli/config.js

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -30,28 +30,23 @@ exports.CONFIG_FILES = [
3030
'.mocharc.json'
3131
];
3232

33-
const isModuleNotFoundError = err =>
34-
err.code === 'MODULE_NOT_FOUND' ||
35-
err.message.indexOf('Cannot find module') !== -1;
36-
3733
/**
3834
* Parsers for various config filetypes. Each accepts a filepath and
3935
* returns an object (but could throw)
4036
*/
4137
const parsers = (exports.parsers = {
4238
yaml: filepath => require('js-yaml').load(fs.readFileSync(filepath, 'utf8')),
4339
js: filepath => {
44-
const cwdFilepath = path.resolve(filepath);
40+
let cwdFilepath;
4541
try {
46-
debug('parsers: load using cwd-relative path: "%s"', cwdFilepath);
42+
debug('parsers: load cwd-relative path: "%s"', path.resolve(filepath));
43+
cwdFilepath = require.resolve(path.resolve(filepath)); // evtl. throws
4744
return require(cwdFilepath);
4845
} catch (err) {
49-
if (isModuleNotFoundError(err)) {
50-
debug('parsers: retry load as module-relative path: "%s"', filepath);
51-
return require(filepath);
52-
} else {
53-
throw err; // rethrow
54-
}
46+
if (cwdFilepath) throw err;
47+
48+
debug('parsers: retry load as module-relative path: "%s"', filepath);
49+
return require(filepath);
5550
}
5651
},
5752
json: filepath =>

test/node-unit/cli/config.spec.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
const sinon = require('sinon');
44
const rewiremock = require('rewiremock/node');
5+
const {parsers} = require('../../../lib/cli/config');
56

67
describe('cli/config', function () {
78
const phonyConfigObject = {ok: true};
@@ -155,4 +156,23 @@ describe('cli/config', function () {
155156
});
156157
});
157158
});
159+
160+
describe('parsers()', function () {
161+
it('should print error message for faulty require', function () {
162+
// Fixture exists, but fails loading.
163+
// Prints correct error message without using fallback path.
164+
expect(
165+
() => parsers.js(require.resolve('./fixtures/bad-require.fixture.js')),
166+
'to throw',
167+
{message: /Cannot find module 'fake'/, code: 'MODULE_NOT_FOUND'}
168+
);
169+
});
170+
171+
it('should print error message for non-existing file', function () {
172+
expect(() => parsers.js('not-existing.js'), 'to throw', {
173+
message: /Cannot find module 'not-existing.js'/,
174+
code: 'MODULE_NOT_FOUND'
175+
});
176+
});
177+
});
158178
});
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
require('fake');

0 commit comments

Comments
 (0)