Open anomiex opened 9 months ago
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".
What we can do on our side if the other module works with package.exports
in a wrong way?
similar thread here https://github.com/ai/nanoid/issues/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 package.exports
in a "wrong" way here? It's specifying "browser" and "require" (and not "import"), which is exactly what it wants to receive.
Here is current package.exports
of v3:
"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.
What changes do you suggest there to fix Jest?
You already appear to have an index.browser.cjs
file available in the published package, so something like this would work.
"exports": {
".": {
"browser": {
"require": "./index.browser.cjs",
"default": "./index.browser.js",
},
"require": {
"types": "./index.d.cts",
"default": "./index.cjs"
},
"import": {
"types": "./index.d.ts",
"default": "./index.js"
},
"default": "./index.js"
}
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
"browser": {
"import": "./index.browser.js",
"require": "./index.browser.cjs",
"default": "./index.browser.js",
},
Or you could switch it up, put "browser" inside the existing "require" and "import" blocks:
"exports": {
".": {
"require": {
"types": "./index.d.cts",
"browser": "./index.browser.cjs",
"default": "./index.cjs"
},
"import": {
"types": "./index.d.ts",
"browser": "./index.browser.js",
"default": "./index.js"
},
"browser": "./index.browser.js",
"default": "./index.js"
}
But is it a good idea to use browser’s version in Node.js? Does Jest emulate crypto
for Node.js <20?
We didn’t use .browser.cjs
especially for that reason since it didn’t work in Node.js.
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 require
was just work. We have a special Nose.js version exactly to work without polyfill.
OK, can you send PR to dual-publish
to add browser.require
if it already produces CJS files for browser?
any solution for fix this?
I get the same error, however related to import { webcrypto as crypto } from 'node:crypto'
.
Current nanoid
version: 5.0.7
Is there any solution to this?
I've temporarily solved the issue by downgrading to 3.3.4
@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:
// in jest setup config
jest.mock('nanoid', () => {
return {
nanoid: () => Math.random().toString(),
};
});
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 https://github.com/microsoft/accessibility-insights-web/pull/5421#issuecomment-1109168149.
Reproduction
npm add jest jest-environment-jsdom nanoid@^3
Create the following file named
test.js
:npm exec jest test.js