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
V3 breaks webpack and jest #1265
Comments
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
Hmmm, just for the heck of it I tried something similar to what react-intl suggests and it doesn't work... Maybe something is missing in how |
Update: A minimal
https://github.com/loynoir/example-jest-config-esm/blob/master/jest.config.ts Good luck, bro! 🎉 |
@RedGuy12 If you check the source code of jest, you will find out the core problem here is not
|
Documentation for getting this to work with Jest is here: |
Jest
Node.js
We both want ESM world, but reality is ecosystem is not ready yet. Instead of "just use all the experimental features", which seems like an uncontrolable black box, Can't we just dual-modulize node-fetch? |
this are being discussed here: #1227 (read everything before jumping in to discussions) |
I'm not sure about that, Jest is just one of many test runners. I don't think that just because its support is experimental, the whole ecosystem isn't ready.
I think a better approach is to stick with node-fetch 2.x if you are having troubles |
I know. But here is a specific real-world scenario, just jest, not some abstraction.
Node.js mark as
Emm, seems very frustrating. |
Yes, but I don't think that
Why does this seem frustrating? |
Here is the problem. Please check jest source code. |
Because there is a proper solution for current time point, |
Yes I know that Jest is using vm-modules. What I meant was that the majority of the ecosystem is not using vm-modules, and thus holding back the entire ecosystem just because Jest does it feels a bit strange to me 🤔
Well, I am personally against dual-module, but that is because there are clear drawbacks with it which is discussed in the larger thread. I think it's better if we concentrate specifically on Jest and WebPack compatibility (without going away from ESM-only) in this issue. In fact, it might be better to split it up into two issues: one for Jest, and one for WebPack. I've used ESM-only modules in WebPack before so I'm not entirely sure what the problems are there, but I believe that there are workarounds/fixes available... |
I'm using ESM and Rollup in my project but Jest still had the same problem with loading the package. Here's what worked for me:
Credit goes to: https://stackoverflow.com/a/66480267/8746088 |
@Thomasan1999... That did it for me!!! it was |
False alarm... I forgot to |
@heath-freenome Not false alarm. I saw and tested it! 😄 PS: I use [tempDir] $ pnpm i -D jest ts-jest ts-node typescript node-fetch && jest && tail -n +1 jest.config.ts tsconfig.json esmDeps.spec.ts
==> jest.config.ts <==
export default {
preset: 'ts-jest/presets/js-with-ts-esm',
transformIgnorePatterns: [
'node_modules/\\.pnpm/(?!node-fetch|fetch-blob)',
]
}
==> tsconfig.json <==
{
"compilerOptions": {
"allowJs": true,
"esModuleInterop": true
}
}
==> esmDeps.spec.ts <==
import nf from 'node-fetch'
describe("esmDeps", () => {
it("esmDeps", () => {
console.log(nf)
});
}); |
I'm not using typescript in my project (yet)... And there still isn't a workaround for my webpack issues |
Since @Thomasan1999 got a solution without any experimental features! I currently have no real-world problem with node-fetch. 😄 FYI @jimmywarting @LinusU, not hard core ESM yet, like top-level await will break this solution. (I also tested it) |
@heath-freenome
Maybe you need FYI, ESM-only package node-fetch depends on ESM-only fetch-blob. Good luck! |
For Node JS project that's worked for me, may be you can do the same for webpack: // jest.config.js
export default {
transform: {
'^.+\\.js$': 'babel-jest',
},
transformIgnorePatterns: [
'node_modules/(?!(fetch-blob|node-fetch)/)'
]
} // babel.config.cjs
module.exports = {
presets: [
[
'@babel/preset-env',
{
targets: {
node: 'current'
}
}
]
],
plugins: ['babel-plugin-transform-es2015-modules-commonjs', 'babel-plugin-transform-import-meta']
} |
Thanks @xbreaker that worked great until node-fetch updated to v3.1.0 now.
There are other issues with node-fetch@3.1 which causes problems with jest, specifically the usage of node:prefixes. I have added another workaround details here: #1367 (comment) Hope this helps somebody |
Please document how to get things working with jest and webpack like formatJS does for react-intl: https://formatjs.io/docs/react-intl#jest
Right now my webpack blows up with:
and Jest with:
Expected behavior
I've have always used the
import
syntax as described in the migration guide... but the guide is incomplete since webpack/jest now blow up when I try upgradingYour Environment
The text was updated successfully, but these errors were encountered: