-
Notifications
You must be signed in to change notification settings - Fork 97
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
jest-environment-jsdom 28+ tries to use browser exports instead of default exports #300
Comments
@jaredLunde Friendly ping! ☝️ |
I'm having the same problem with
|
Yep looking into it |
Hello! While I think it's certainly better to provide both CommonJS and ESM modules in browsers, however this problem is only occurring in jest environments because jest doesn't support ESM modules by default. In other words, many other environments like webpack, tsc, etc already supports ESM, and would work quite well without any problem. I've encountered the same problem and looked into it. Can you check the following comment and if it works for you? jestjs/jest#13739 (comment) |
Any progress on this? |
yeah ngl if it doesn't affect me personally it's harder to make time for 😏 would like to get to it but haven't had time |
@jaredLunde With jest 28 now supporting exports. Node and jest might interpret your resize-observer as "commonJS" module. Looking at https://nodejs.org/api/packages.html#type - not adding type: "module" it defaults to commonJS. And therefore maybe the "default" in exports doesn't get used and defaults to be using "import" because the package is deemed to be commonJS? Just speculating of course, need to validate my thoughts. |
Workaround // jest.config.js
module.exports = {
...
moduleNameMapper: { '@react-hook/(.*)': '<rootDir>/node_modules/@react-hook/$1/dist/main' }
} |
We have been able to workaround this issue also with some jest configuration using the resolver option. return options.defaultResolver(path, {
...options,
// Use packageFilter to process parsed `package.json` before
// the resolution (see https://www.npmjs.com/package/resolve#resolveid-opts-cb)
packageFilter: (pkg) => {
// jest-environment-jsdom 28+ tries to use browser exports instead of default exports,
// but @react-hook/resize-observer only offers an ESM browser export and not a CommonJS one. Jest does not yet
// support ESM modules natively, so this causes a Jest error related to trying to parse
// "export" syntax.
//
// This workaround prevents Jest from considering @react-hook/resize-observer module-based exports at all;
// it falls back to CommonJS+node "main" property.
if (typeof pkg.name === "string" && pkg.name.startsWith("@react-hook/")) {
delete pkg["exports"];
delete pkg["module"];
}
return pkg;
},
}); |
Describe the bug
jest-environment-jsdom
v28+ tries to usebrowser
exports instead of default exports which generates the following error when JesttestEnvironment: 'jsdom'
:To Reproduce
Steps to reproduce the behavior:
git clone https://github.com/alex-l-miro/react-hook-bug.git
yarn
yarn test
Expected behavior
Jest should not fail when
testEnvironment
isjsdom
The text was updated successfully, but these errors were encountered: