-
-
Notifications
You must be signed in to change notification settings - Fork 799
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
SyntaxError about import when using in jsdom testEnvironment #363
Comments
The problem is with Jest config (ESM support is very tricky with Jest). Sorry, you need to ask Jest community. |
Adding nanoid to transformIgnorePatterns and moduleNameMapper to the jest.config.ts helped in my case with the same error:
|
@ai I believe both My understanding is that some changes are required to make it compliant? |
Yeah I think the issue is that Jest is now looking for package.json exports fields: https://jestjs.io/docs/upgrading-to-jest28#packagejson-exports I dont know enough about how they work, but could be an issue with this? Lines 24 to 35 in fd80ce3
Some similar discussion happening over on the uuid issue tracker: uuidjs/uuid#616 I got it to work doing something similar to the custom jest resolver solution recommended here |
The problem is that Jest use You need a |
Ahh that makes sense, thanks for the fast reply! The custom jest resolver worked for me for now. |
I added a note about Jest c9df0ab |
I also created an issue in Jest jestjs/jest#12947 |
@ai I'm not sure this is only a polyfill error, because it's not really complaining about crypto, but about Tried all of the ideas in this issue, none of them worked for me. Would anyone be so kind and share their No matter what I do, I'm getting this: Thanks! |
I have the following in
and finally in
|
@jacquesg thanks! I finally found a working combo this very minute, sharing it here if anyone runs into this issue: The solution has 2 parts:
|
Another way is to switch Jest to ESM mode by Jest docs |
It is unfortunate to have these type of issues where there are multiple solutions to the same problem that IMHO should not exist. Imagine not knowing how to debug or even google this. It requires a good understanding of interactions between different systems, ESM vs CJS, browser vs node, the list goes on... (I for one do not understand all of it or necessarily "care" to understand all of it). A zeroconf solution is really the only way this doesn't continue biting developers. |
It is mostly the problem of Jest. I can recommend Also there is |
@ai I'll check out |
…nanoid issues later
jest.mock("nanoid", () => { return {
nanoid : ()=>{}
} }); use mock in setupTests.js of jest |
This really worked for me. |
|
Thanks for the inspiration! I have faker on a project, so made
|
When
testEnvironment: 'jsdom'
is set it tries to loadnode_modules/nanoid/index.browser.js
and shows an error about the import syntrax.note: in
testEnvironment: 'node'
import has no problemjest config:
tsconfig
error:
The text was updated successfully, but these errors were encountered: