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

Regression in output of an error throw by a -r module throws (within yargs) #4316

Closed
3 tasks done
alexjeffburke opened this issue Jun 6, 2020 · 5 comments
Closed
3 tasks done

Comments

@alexjeffburke
Copy link

alexjeffburke commented Jun 6, 2020

Prerequisites

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

When a module loaded with -r throws in the current version of mocha 7, the call made to yargs.showHelp() after the error is printed (https://github.com/mochajs/mocha/blob/v7.2.0/lib/cli/cli.js#L53) seems to throw with the following stack trace:

(node:10708) UnhandledPromiseRejectionWarning: TypeError: Cannot convert undefined or null to object
    at Function.keys (<anonymous>)
    at Object.help (/path/to/mocha-dominate/node_modules/mocha/node_modules/yargs/lib/usage.js:242:22)
    at Object.self.showHelp (/path/to/mocha-dominate/node_modules/mocha/node_modules/yargs/lib/usage.js:434:15)
    at Array.yargs.scriptName.command.command.updateStrings.fail (/path/to/mocha-dominate/node_modules/mocha/lib/cli/cli.js:54:13)
    at Object.fail (/path/to/mocha-dominate/node_modules/mocha/node_modules/yargs/lib/usage.js:41:17)
    at handlerResult.catch.error (/path/to/mocha-dominate/node_modules/mocha/node_modules/yargs/lib/command.js:246:36)
    at process._tickCallback (internal/process/next_tick.js:68:7)
    at Function.Module.runMain (internal/modules/cjs/loader.js:834:11)
    at findNodeScript.then.existing (/Users/alex/.nvm/versions/node/v10.19.0/lib/node_modules/npm/node_modules/libnpx/index.js:268:14)

Having looked into the yargs code at line 240 in yargs/lib/usage.js mentioned above, the problem seems to be that at this point yargs.parsed is false, which means the dereference of newAliases returns undefined and then Object.keys() wrapped around it throws a null or undefined error.

Steps to Reproduce

This error can be reproducued by saving the following in a file called "thrower.js":

throw new Error('some error');

and mocha executed as follows:

npx mocha -r ./thrower.js

Expected behavior:
The error thrown by the require hook is printed to the console.

Actual behavior:
Before the error can be thrown an internal error is created from within yargs.
Reproduces how often: [What percentage of the time does it reproduce?]
This is consistently reproducible.

Versions

Mocha version 7.2.0

@boneskull
Copy link
Member

sorry got confused. #4311

@boneskull
Copy link
Member

duplicate of #4310

@boneskull
Copy link
Member

a fix will land in v8.0.0

@alexjeffburke
Copy link
Author

@boneskull thanks for looking at this - did look around for other issues but didn't quite make the connection this would be solved by the one you linked to.

Btw, this came up when I was trying to port a mocha plugin to the new exports.mochaHooks = {} mechanism. It does some validation and throws when things are out of whack which is how I stumbled on this. But this aside, hooks worked really well and looks like it will be very useful :)

@boneskull
Copy link
Member

@alexjeffburke Thanks for the kind words. Yes, this was kind of buried (the fix landed as part of something else), so I don't fault you for not being able to find it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants