Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mocha doesn't rethrow error from cwd-relative path js config file #4701

Closed
4 tasks done
kirill-golovan opened this issue Jul 24, 2021 · 0 comments · Fixed by #4702
Closed
4 tasks done

Mocha doesn't rethrow error from cwd-relative path js config file #4701

kirill-golovan opened this issue Jul 24, 2021 · 0 comments · Fixed by #4702
Labels
area: node.js command-line-or-Node.js-specific type: bug a defect, confirmed by a maintainer

Comments

@kirill-golovan
Copy link
Contributor

Prerequisites

  • Checked that your issue hasn't already been filed by cross-referencing issues with the faq label
  • 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.

Description

Mocha doesn't rethrow error from cwd-relative path js config file because of the incorrect comparison operator in isModuleNotFoundError function in lib/cli/config.js (should be err.code === 'MODULE_NOT_FOUND'):

mocha/lib/cli/config.js

Lines 33 to 35 in 3722201

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

It will return true for the errors thrown inside config file so instead of rethrowing those errors it will try to load config file with a module-relative path:

mocha/lib/cli/config.js

Lines 48 to 53 in 3722201

} catch (err) {
if (isModuleNotFoundError(err)) {
debug('parsers: retry load as module-relative path: "%s"', filepath);
return require(filepath);
} else {
throw err; // rethrow

Steps to Reproduce

Create .mocharc.js:

throw new Error('foo');

module.exports = {
  spec: 'test/*.js'
};

Run it:
./node_modules/mocha/bin/mocha --config .mocharc.js

Expected behavior:
Error: Unable to read/parse /Users/me/temp_mocha_proj/.mocharc.js: Error: foo

Actual behavior:
Error: Unable to read/parse .mocharc.js: Error: Cannot find module '.mocharc.js'

Reproduces how often:
100%

Versions

  • The output of mocha --version and node node_modules/.bin/mocha --version: 9.0.2
  • The output of node --version: v12.13.0
  • Your operating system
    • name and version: macOS 10.14.6
    • architecture (32 or 64-bit): 64-bit
  • Your shell (e.g., bash, zsh, PowerShell, cmd): bash
@juergba juergba added type: bug a defect, confirmed by a maintainer and removed unconfirmed-bug labels Jul 24, 2021
@juergba juergba added the area: node.js command-line-or-Node.js-specific label Jul 25, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: node.js command-line-or-Node.js-specific type: bug a defect, confirmed by a maintainer
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants