-
-
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
Missing file/package in import statement misreported as ESM error #4803
Comments
I believe the checks for
Lines 58 to 65 in 28b4824
I think
|
@giltayar could you have a look at this one, please? |
Oh, well hello @giltayar haha |
@juergba @JakobJingleheimer. I've seen this too! Let me look into it this week (or the week after). |
@JakobJingleheimer I tried reproducing the problem, and couldn't. My setup was Node and mocha versions exactly like yours, $ npm install mocha babel-register-esm # babel-register-esm is a loader of mine that transpiles via babel
$ cat test.mjs
import '@testing-library/jest-dom';
$ npx mocha --loader=babel-register-esm ./test.mjs
(node:3252) ExperimentalWarning: --experimental-loader is an experimental feature. This feature could change at any time
(Use `node --trace-warnings ...` to show where the warning was created)
(node:3252) DeprecationWarning: Obsolete loader hook(s) supplied and will be ignored: getFormat, transformSource
Error [ERR_MODULE_NOT_FOUND]: Cannot find package '@testing-library/jest-dom' imported from /Users/giltayar/code/tmp/mocha-esm-problem/test.mjs
... So it looks like it's working...? What did I miss here? |
Your file extension is Lines 44 to 46 in 28b4824
Ensure your file extension is not
The contents of import 'nonexistent'; If you replace that import with one to something that does exist, all is well. |
@JakobJingleheimer reproduced! The fix is really ugly, as this whole section of code is heuristics in figuring out whether the But it's mostly passing the test of time, and I can't think of another way to fallback to Hmm... maybe we don't really need the (just thoughts. Will be fixed one way or another this weekend) |
🤔 can you not trust Then in the try/catch for I just tried both, and it worked for this bug's scenario: Removing the Updating the check for |
@JakobJingleheimer unfortunately, I can't. The But I fixed the problem and a PR will be coming in a few minutes! (it's a hack, but the whole way of determining whether a file is ESM or CJS is ultimately a hack) |
Prerequisites
faq
labelnode node_modules/.bin/mocha --version
(Local) andmocha --version
(Global). We recommend that you not install Mocha globally.Description
This lead me down the garden path: A bad import statement (where the targeted package doesn't exist) results in mocha claiming the containing file was not interpreted as ESM despite containing an
import
(and mocha swallows the actual, correct error message).Error from Mocha:
Error from Node.js:
The bug originates from https://github.com/mochajs/mocha/blob/v9.1.3/lib/nodejs/esm-utils.js#L51
Steps to Reproduce
test.jsx
Without installing
@testing-library/jest-dom
:NODE_ENV=test NODE_OPTIONS="--loader=./loader.mjs" npx mocha ./test.jsx
(The loader live-transpiles jsx via esbuild and marks the file as esm; it's not especially important for the reproduction but it did help me find the bug in Mocha).
Expected behavior: Mocha reports the correct error (that the targeted package doesn't exist, as node correctly reports).
Actual behavior: Mocha wrongly reports a different, unrelated error.
Reproduces how often: 100%
Versions
mocha --version
andnode node_modules/.bin/mocha --version
: 9.1.3 (also occurs in 8.3.0)node --version
: 17.2.0Additional Information
N/A
The text was updated successfully, but these errors were encountered: