Skip to content

Commit

Permalink
fix: wrong error thrown while loading config files (#4832)
Browse files Browse the repository at this point in the history
  • Loading branch information
juergba authored Feb 17, 2022
1 parent 11c4560 commit 86305cf
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 14 deletions.
4 changes: 2 additions & 2 deletions .github/ISSUE_TEMPLATE/bug_report.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ Place an `x` between the square brackets on the lines below for every satisfied
- [ ] 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)
- [ ] 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.
- [ ] '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
- [ ] 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.
- [ ] 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.

### Description

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

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

- The output of `mocha --version` and `node node_modules/.bin/mocha --version`:
- The output of `mocha --version` and `node_modules/.bin/mocha --version`:
- The output of `node --version`:
- Your operating system
- name and version:
Expand Down
19 changes: 7 additions & 12 deletions lib/cli/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,28 +30,23 @@ exports.CONFIG_FILES = [
'.mocharc.json'
];

const isModuleNotFoundError = err =>
err.code === 'MODULE_NOT_FOUND' ||
err.message.indexOf('Cannot find module') !== -1;

/**
* Parsers for various config filetypes. Each accepts a filepath and
* returns an object (but could throw)
*/
const parsers = (exports.parsers = {
yaml: filepath => require('js-yaml').load(fs.readFileSync(filepath, 'utf8')),
js: filepath => {
const cwdFilepath = path.resolve(filepath);
let cwdFilepath;
try {
debug('parsers: load using cwd-relative path: "%s"', cwdFilepath);
debug('parsers: load cwd-relative path: "%s"', path.resolve(filepath));
cwdFilepath = require.resolve(path.resolve(filepath)); // evtl. throws
return require(cwdFilepath);
} catch (err) {
if (isModuleNotFoundError(err)) {
debug('parsers: retry load as module-relative path: "%s"', filepath);
return require(filepath);
} else {
throw err; // rethrow
}
if (cwdFilepath) throw err;

debug('parsers: retry load as module-relative path: "%s"', filepath);
return require(filepath);
}
},
json: filepath =>
Expand Down
20 changes: 20 additions & 0 deletions test/node-unit/cli/config.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

const sinon = require('sinon');
const rewiremock = require('rewiremock/node');
const {parsers} = require('../../../lib/cli/config');

describe('cli/config', function () {
const phonyConfigObject = {ok: true};
Expand Down Expand Up @@ -155,4 +156,23 @@ describe('cli/config', function () {
});
});
});

describe('parsers()', function () {
it('should print error message for faulty require', function () {
// Fixture exists, but fails loading.
// Prints correct error message without using fallback path.
expect(
() => parsers.js(require.resolve('./fixtures/bad-require.fixture.js')),
'to throw',
{message: /Cannot find module 'fake'/, code: 'MODULE_NOT_FOUND'}
);
});

it('should print error message for non-existing file', function () {
expect(() => parsers.js('not-existing.js'), 'to throw', {
message: /Cannot find module 'not-existing.js'/,
code: 'MODULE_NOT_FOUND'
});
});
});
});
1 change: 1 addition & 0 deletions test/node-unit/cli/fixtures/bad-require.fixture.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
require('fake');

0 comments on commit 86305cf

Please sign in to comment.