From 86305cfed39de6fdfe0cead10759b19dce037370 Mon Sep 17 00:00:00 2001 From: Juerg B <44573692+juergba@users.noreply.github.com> Date: Thu, 17 Feb 2022 17:43:06 +0100 Subject: [PATCH] fix: wrong error thrown while loading config files (#4832) --- .github/ISSUE_TEMPLATE/bug_report.md | 4 ++-- lib/cli/config.js | 19 +++++++----------- test/node-unit/cli/config.spec.js | 20 +++++++++++++++++++ .../cli/fixtures/bad-require.fixture.js | 1 + 4 files changed, 30 insertions(+), 14 deletions(-) create mode 100644 test/node-unit/cli/fixtures/bad-require.fixture.js diff --git a/.github/ISSUE_TEMPLATE/bug_report.md b/.github/ISSUE_TEMPLATE/bug_report.md index 7491593eb1..046f15e854 100644 --- a/.github/ISSUE_TEMPLATE/bug_report.md +++ b/.github/ISSUE_TEMPLATE/bug_report.md @@ -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 @@ -51,7 +51,7 @@ Scrub if needed so as not to reveal passwords, etc. -- 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: diff --git a/lib/cli/config.js b/lib/cli/config.js index 10f16d8aa3..ac719833cf 100644 --- a/lib/cli/config.js +++ b/lib/cli/config.js @@ -30,10 +30,6 @@ 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) @@ -41,17 +37,16 @@ const isModuleNotFoundError = err => 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 => diff --git a/test/node-unit/cli/config.spec.js b/test/node-unit/cli/config.spec.js index edd182c373..1dba0fe16a 100644 --- a/test/node-unit/cli/config.spec.js +++ b/test/node-unit/cli/config.spec.js @@ -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}; @@ -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' + }); + }); + }); }); diff --git a/test/node-unit/cli/fixtures/bad-require.fixture.js b/test/node-unit/cli/fixtures/bad-require.fixture.js new file mode 100644 index 0000000000..ad8ca090ed --- /dev/null +++ b/test/node-unit/cli/fixtures/bad-require.fixture.js @@ -0,0 +1 @@ +require('fake');