-
-
Notifications
You must be signed in to change notification settings - Fork 790
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. |
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: