Replies: 14 comments 8 replies
-
+1 same issue with ts/jest compiled with babel // jest.config.js option that might be relevant
transform: {
'^.+\\.[t|j]sx?$': 'babel-jest',
}, |
Beta Was this translation helpful? Give feedback.
-
I think I made it work with "test:watch": "node --experimental-vm-modules node_modules/jest/bin/jest.js --watch", jest.config.json if you are using ES Modules:{
"roots": ["<rootDir>/src"],
"extensionsToTreatAsEsm": [".ts"],
"testEnvironment": "node",
"moduleNameMapper": {
"^(\\.{1,2}/.*)\\.js$": "$1"
},
"transform": {
"\\.[jt]sx?$": [
"babel-jest",
{
"babelrc": false,
"presets": ["@babel/preset-typescript"],
"plugins": ["@babel/plugin-proposal-optional-chaining"]
}
]
}
} Check you my example repository: https://github.com/lynxtaa/node-fetch-jest-example jest.config.json if you are using CommonJS:{
"roots": ["<rootDir>/src"],
"testEnvironment": "node",
"transform": {
"\\.[jt]sx?$": [
"babel-jest",
{
"babelrc": false,
"presets": ["@babel/preset-typescript"],
"plugins": [
"@babel/plugin-proposal-optional-chaining",
"@babel/plugin-transform-modules-commonjs"
]
}
]
}
} And in this case you should use dynamic import: const fetch = (...args) => import('node-fetch').then(({default: fetch}) => fetch(...args)); |
Beta Was this translation helpful? Give feedback.
-
I have a few typescript projects where this happened to me. Add the following to transform: {
"^.+\\.(ts|tsx)$": "ts-jest",
"^.+\\.(js)$": "babel-jest",
},
transformIgnorePatterns: [
], Note the empty node-fetch: ^3.0.0 |
Beta Was this translation helpful? Give feedback.
-
Rolling back to 2.6 worked for me to get serverless-bundle to run jest tests. |
Beta Was this translation helpful? Give feedback.
-
Same here, rolling back to 2.6 worked. Any of the other solutions were not working. |
Beta Was this translation helpful? Give feedback.
-
As typing information is already included in node-fetch 3.0 and up,
the @types/node-fetch package after version 3.0 is just a stub. To get
typing information you need to install a version of the types package
that's inside the 2.x range. In my case, I installed version 2.5.12 and
everything's working fine.
El lun, 10 ene 2022 a las 11:54, Bruno Mello ***@***.***>)
escribió:
… To those who are rolling back, how are you using the type definitions?
***@***.*** doesn't ship with type definitions and installing
@types/node-fetch doesn't seem to solve my problem.
Here is how I am importing the module
[image: image]
<https://user-images.githubusercontent.com/29875060/148814451-763c57a7-3d24-41e5-ad5d-7682db554f48.png>
And in my package.json I have listed both @types/node-fetch on
devDependencies and node-fetch on dependencies
{
...
"devDependencies": {
...
***@***.***/node-fetch": "^3.0.3",
...
},
"dependencies": {
...
"node-fetch": "^2.6.6",
...
}
}
—
Reply to this email directly, view it on GitHub
<#1289 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AMYD4L4RSBSLIK2KKVL424TUVMMNXANCNFSM5D7Q2BZA>
.
You are receiving this because you commented.Message ID:
***@***.***>
|
Beta Was this translation helpful? Give feedback.
-
This is a small example where jest works with node-fetch@3: https://github.com/ravshansbox/jest-node-fetch-es-modules |
Beta Was this translation helpful? Give feedback.
-
Note: edited after original comment this issue should be closed and reference #1279 End note
This is misleading as it works only because you are telling jest to run with support for ESM (--experimental-vm-modules). As far as I can tell, node-fetch v3 only works as an ESM and you cannot make it work in jest without that flag. Our teams are not going to use that flag unfortunately. So unless this becomes supported in Jest, we are going to use node-fetch <3.0.0 Also looks like it's slowing the adoption of node-fetch v3: |
Beta Was this translation helpful? Give feedback.
-
I haven't played with jest lately... |
Beta Was this translation helpful? Give feedback.
-
Using "@types/node-fetch": "2",
"node-fetch": "2", |
Beta Was this translation helpful? Give feedback.
-
It is pretty clear that the NodeJS and DOM ecosystem is not ready for 100% ESM modules. |
Beta Was this translation helpful? Give feedback.
-
steps are next:
|
Beta Was this translation helpful? Give feedback.
-
If you use https://kulshekhar.github.io/ts-jest/docs/guides/esm-support/ And you also need to know one thing, if you use |
Beta Was this translation helpful? Give feedback.
-
@a-wf You can use transformIgnorePatterns: [
"/node_modules/(?!node-fetch)/"
]
"transformIgnorePatterns": [
"/node_modules/(?!node-fetch)/"
] |
Beta Was this translation helpful? Give feedback.
-
I upgraded node-fetch to v3, and run jest test and I get:
I followed instructions, but nothing changes,
I also tried this jest config
but nothing happens
Beta Was this translation helpful? Give feedback.
All reactions