issue with ts jest and node-fetch v3
See original GitHub issueI upgraded node-fetch to v3, and run jest test and I get:
Jest encountered an unexpected token
Jest failed to parse a file. This happens e.g. when your code or its dependencies use non-standard JavaScript syntax, or when Jest is not configured to support such syntax.
Out of the box Jest supports Babel, which will be used to transform your files into valid JS based on your Babel configuration.
By default "node_modules" folder is ignored by transformers.
Here's what you can do:
• If you are trying to use ECMAScript Modules, see https://jestjs.io/docs/ecmascript-modules for how to enable it.
• To have some of your "node_modules" files transformed, you can specify a custom "transformIgnorePatterns" in your config.
• If you need a custom transformation specify a "transform" option in your config.
• If you simply want to mock your non-JS modules (e.g. binary assets) you can stub them out with the "moduleNameMapper" config option.
You'll find more details and examples of these config options in the docs:
https://jestjs.io/docs/configuration
For information about custom transformations, see:
https://jestjs.io/docs/code-transformation
Details:
import http from 'http';
^^^^^^
SyntaxError: Cannot use import statement outside a module
1 | import { Task, DataError } from '@koober/std';
> 2 | import fetch from 'node-fetch';
| ^
3 | import { FacebookResponseError, FacebookTokenInfo, FacebookTokenInfoFormatted, FacebookUserIdentity } from './data';
4 |
5 | export interface FacebookClient
at Runtime.createScriptFromCode (node_modules/jest-runtime/build/index.js:1597:14)
at Object.<anonymous> (src/bundle/facebook/client.ts:2:1)
I followed instructions, but nothing changes, I also tried this jest config
"extensionsToTreatAsEsm": [
".ts"
],
"globals": {
"ts-jest": {
"tsconfig": "tsconfig.json",
"useESM": true
}
},
"preset": "ts-jest/presets/default-esm",
but nothing happens
Issue Analytics
- State:
- Created 2 years ago
- Reactions:70
- Comments:21 (1 by maintainers)
Top Results From Across the Web
How to use node-fetch with Jest and Typescript?
I my case, it looks like there was some issue with the latest node-fetch 3.x packaged and ts-jest . I went back to...
Read more >Bypassing module mocks - Jest
Jest allows you to mock out whole modules in your tests, which can be useful for testing if your code is ... import...
Read more >syntaxerror cannot use import statement outside a module ...
So this jest configuration solved my issue: "jest": { "preset": "ts-jest", "testEnvironment": "node", "transform": { "node_modules/variables/.+\\.(j|t)sx?$
Read more >ts-jest - npm Package Health Analysis - Snyk
The npm package ts-jest was scanned for known vulnerabilities and missing license, and no issues were found. Thus the package was deemed as...
Read more >How to correctly mock node-fetch? - Reddit
I'm having problems mocking node-fetch it is not mocking and makes an actual request to an API. Here is the module I'm trying...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@SashaJson we are currently migrating to axios since there are no responses to these quite challenging issues users are facing
Having the same problem here.