-
-
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
Errors with nanoid v3 when using jest and jsdom #462
Comments
What we can do on our side if the other module works with |
similar thread here #439 |
(Honestly, the struggle with Jest was a reason why I decided to go to ESM-only. There was just no way to make it work in every environment.) |
In what way is jest with jsdom working with |
Here is current "exports": {
".": {
"browser": "./index.browser.js",
"require": {
"types": "./index.d.cts",
"default": "./index.cjs"
},
"import": {
"types": "./index.d.ts",
"default": "./index.js"
},
"default": "./index.js"
} What changes do you suggest there to fix Jest? |
jsdom is supposed to provide a browser-like environment. If it's not browser-like enough, that would be a problem with jsdom. OTOH, if a package assumes "browser" means "import", that's not jsdom's fault.
You already appear to have an
Or if you're worried that some browser might supply both "require" and "import", you could do like this to prefer the esm version in that case
Or you could switch it up, put "browser" inside the existing "require" and "import" blocks:
|
But is it a good idea to use browser’s version in Node.js? Does Jest emulate We didn’t use |
I'd say that if someone wants to try it, as indicated by passing the "browser" condition, it's their problem if it doesn't work due to a missing Web Crypto API or whatever. In my case we're running the tests in Node 20 already anyway. But if we were still testing with an earlier version, I'd take it as my responsibility to load an appropriate mock/polyfill in the Jest config to make it work. |
Taking OK, can you send PR to |
any solution for fix this? |
I get the same error, however related to Current Is there any solution to this? I've temporarily solved the issue by downgrading to |
@AdiMarianMutu you have a different issue. You need to update your project to ESM to use 4.0 or 5.0. |
If this is blocking anyone, you can make a simple jest mock to get around it:
|
have removed from our codebase for this reason sadly :( |
I switched to the native 'crypto.randomUUID()' implementation in the end. One dependency less, haven't been happier. |
@ai Any solution for that in mind? We will keep using v3 while this doesn't change. Sadly we can't get the improvements done for the node:18. |
@ppamorim sorry, there is no good solution. Jest is just broken with ESM because it hacked Node.js loading mechanism. All options have a lot of compromises. |
The breakage here has nothing to do with jest "hack[ing] the Node.js loading mechanism", it's that this package assumes that "browser" always means "import" despite the loading request specifically indicating "require" (i.e. commonjs) rather than "import" (i.e. esm). I told you back in January how to fix it in v3, but I can't figure out your custom build system to work out how to fix it for you. Although it seems like ppamorim may be asking about something else, since this issue is specific to v3 so "We will keep using v3 while this doesn't change" makes little sense in relation to this issue. |
Jest is broken because it is using browser version on Node.js environment. We provide CJS package for Node.js, but Jest hacked |
We already went through that argument above. me:
you:
me:
|
Browser version in Node.js environment is still non-standard Node.js behavior and this is why for me, it is a hack. |
if it helps with anything, I couldn't mock the module due to having a However by letting jest transform // jest.config.js
{
transformIgnorePatterns: [
'<rootDir>/node_modules/(?!nanoid)',
],
} |
When running tests using jest and jest-environment-jsdom, version 3.3.7 of this package (which is still supposed to be commonjs-compatible) causes a failure:
The problem is that, with jest-environment-jsdom, the conditions available are "browser", "require" (not "import"), and "default", but nanoid v3's
exports
assumes "browser" always comes with "import".A detailed analysis of a similar problem in another package, along with potential workarounds, may be found at microsoft/accessibility-insights-web#5421 (comment).
Reproduction
npm add jest jest-environment-jsdom nanoid@^3
test.js
:npm exec jest test.js
The text was updated successfully, but these errors were encountered: