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

fix unfriendly error; add ESM examples for root hook plugins; closes #4310 #4311

Merged
merged 2 commits into from
Jun 5, 2020

Conversation

boneskull
Copy link
Contributor

This PR conflates two things, and I apologize.

I've updated the docs to show how to use ESM with root hook plugins.

I've also added a fix in the case that the middleware fails. It looks like yargs does not handle async middleware rejections properly--it fails to trap any error, and instead prints an unrelated problem. So, if handleRequires or validatePlugin fails, we need to trap that error ourself and print it, then force yargs to exit before the fail handler can take over.

Also contains refactors of test/integration/options/require.spec.js, which was driving me nuts due to the nondeterministic parallel tests. It was also confusing as hell anyway, so hopefully the result is a little easier to stomach (it's still a hack).

- re-enable parallel-mode root hook plugin tests
- refactor root hook plugin tests to avoid flake

Signed-off-by: Christopher Hiller <boneskull@boneskull.com>
@boneskull boneskull added type: bug a defect, confirmed by a maintainer area: documentation anything involving docs or mochajs.org semver-patch implementation requires increase of "patch" version number; "bug fixes" labels Jun 4, 2020
@boneskull boneskull added this to the v8.0.0 milestone Jun 4, 2020
@boneskull boneskull self-assigned this Jun 4, 2020
@boneskull boneskull requested a review from nicojs June 4, 2020 00:25
@coveralls
Copy link

Coverage Status

Coverage increased (+0.009%) to 93.575% when pulling 4fe1ab5 on boneskull/issue/4310-esm-root-hook-docs into 63eb80b on master.

@boneskull
Copy link
Contributor Author

Just so we understand what's happening with the error. This is the current behavior. Given:

// foo.js
export const foo = 1;

Running this

$ mocha --require foo.js

you get

(node:26950) UnhandledPromiseRejectionWarning: TypeError: Cannot convert undefined or null to object
    at Function.keys (<anonymous>)
    at Object.help (/Users/boneskull/projects/mochajs/mocha/node_modules/yargs/lib/usage.js:240:22)
    at Object.self.showHelp (/Users/boneskull/projects/mochajs/mocha/node_modules/yargs/lib/usage.js:432:15)
    at Array.<anonymous> (/Users/boneskull/projects/mochajs/mocha/lib/cli/cli.js:53:13)
    at Object.fail (/Users/boneskull/projects/mochajs/mocha/node_modules/yargs/lib/usage.js:41:17)
    at /Users/boneskull/projects/mochajs/mocha/node_modules/yargs/lib/command.js:246:36
(node:26950) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 2)
(node:26950) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

With my change, you get instead

✖ ERROR: /Users/boneskull/projects/mochajs/mocha/foo.js:1
export const foo = 1;
^^^^^^

SyntaxError: Unexpected token 'export'
    at wrapSafe (internal/modules/cjs/loader.js:1072:16)
    at Module._compile (internal/modules/cjs/loader.js:1122:27)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1178:10)
    at Module.load (internal/modules/cjs/loader.js:1002:32)
    at Function.Module._load (internal/modules/cjs/loader.js:901:14)
    at Module.require (internal/modules/cjs/loader.js:1044:19)
    at require (internal/modules/cjs/helpers.js:77:18)
    at exports.requireOrImport (/Users/boneskull/projects/mochajs/mocha/lib/esm-utils.js:20:12)
    at exports.handleRequires (/Users/boneskull/projects/mochajs/mocha/lib/cli/run-helpers.js:94:34)
    at /Users/boneskull/projects/mochajs/mocha/lib/cli/run.js:342:36
    at /Users/boneskull/projects/mochajs/mocha/node_modules/yargs/lib/middleware.js:57:24
    at Array.reduce (<anonymous>)
    at applyMiddleware (/Users/boneskull/projects/mochajs/mocha/node_modules/yargs/lib/middleware.js:42:6)
    at Object.runCommand (/Users/boneskull/projects/mochajs/mocha/node_modules/yargs/lib/command.js:238:19)
    at Object.parseArgs [as _parseArgs] (/Users/boneskull/projects/mochajs/mocha/node_modules/yargs/yargs.js:1113:24)
    at Object.parse (/Users/boneskull/projects/mochajs/mocha/node_modules/yargs/yargs.js:575:25)
    at Object.exports.main (/Users/boneskull/projects/mochajs/mocha/lib/cli/cli.js:71:6)
    at Object.<anonymous> (/Users/boneskull/projects/mochajs/mocha/bin/mocha:153:29)
    at Module._compile (internal/modules/cjs/loader.js:1158:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1178:10)
    at Module.load (internal/modules/cjs/loader.js:1002:32)
    at Function.Module._load (internal/modules/cjs/loader.js:901:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:74:12)
    at internal/main/run_main_module.js:18:47

which is the root problem. (file must end in .mjs or have type: module in package.json)

@boneskull
Copy link
Contributor Author

I considered trying to make this even friendlier--e.g., "invalid use of ESM, please see node docs". however, just because a file throws a SyntaxError doesn't mean it's a misuse of ESM. in other words, it seems difficult to detect. if we can detect it reliably, I'd like to.

Copy link
Contributor

@nicojs nicojs left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A small test improvement suggestion, but this can land as is as well.

I was thinking, do we maybe want to refer to root hooks as being test "setup" functions as well? That way users that are searching for "setup" also endup at these docs. If you agree I can create a PR for it.

docs/index.md Outdated
@@ -1316,7 +1335,9 @@ Available root hooks and their behavior:
The root hook callbacks run in the usual context, so `this` is available:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
The root hook callbacks run in the usual context, so `this` is available:
The root hook callbacks run in the usual context, so `this` will refer to the current mocha context:

@boneskull
Copy link
Contributor Author

this is a drawback to using ctrl-f for searching. we can’t attach keywords or metadata to a section...

in general I’d like to avoid using multiple terminology for the same idea. “setup” is a “before each” hook in the tdd interface, for example, so it’d conflict

Signed-off-by: Christopher Hiller <boneskull@boneskull.com>
@boneskull boneskull force-pushed the boneskull/issue/4310-esm-root-hook-docs branch from 4fe1ab5 to 66f16d1 Compare June 5, 2020 18:32
@boneskull boneskull merged commit 0a66259 into master Jun 5, 2020
@boneskull boneskull deleted the boneskull/issue/4310-esm-root-hook-docs branch June 5, 2020 21:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: documentation anything involving docs or mochajs.org semver-patch implementation requires increase of "patch" version number; "bug fixes" type: bug a defect, confirmed by a maintainer
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants