-
-
Notifications
You must be signed in to change notification settings - Fork 3k
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
Fixes #4803: wrong error thrown if loader is used #4807
Conversation
Fixes #4803 |
Thank you! |
@JakobJingleheimer You're doing important work in Node.js on the loaders. So thank you. 🙏 |
Sorry, not sure if you were waiting for me to verify the fix. Just in case: ✅ I replaced my local
|
.../fixtures/esm/loader-with-module-not-found/test-that-imports-non-existing-module.fixture.mjs
Show resolved
Hide resolved
…orrect resolved file
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@giltayar thank you
Requirements
Description of the Change
When loading test files, there is a heuristic that tries to determine whether to
import
orrequire
them. This heuristic failed in the presence of a loader that recognizes filenames that are not.js/.mjs
. In this case, whenrequire
-ing the file, Node.js does not throwERR_REQUIRE_ESM
(because of the different extension) but rather aSyntaxError
saying that a CJSJ cannot useimport
.I added some more code to this heuristic to deal with the problem by checking if the error is the aforementioned syntax error, and if so, throw the original error.
Alternate Designs
Couldn't think of any.
Why should this be in core?
Well... this is the code of the loading mechanism in Mocha.
Benefits
Devs that use ESM with loaders will get the correct error and not a weird error that they can't use to understand what the problem is.
Possible Drawbacks
Heuristics, as they go, should be tampered with lightly, given that they may succeed in all our tests, but fail in the wild world out there.
I have been careful to add tests to all weird cases I've seen, so I'm pretty confident of the change, but as I said, this may be problematic.
Applicable issues
Not a breaking change.