I am writing a jest test to test a nodejs component. That component used a module called "node:http" from node_modules, and when I run npm test
, it shows the following error:
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.
• If you are trying to use TypeScript, see https://jestjs.io/docs/getting-started#using-typescript
• 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:
..../Server/node_modules/node-fetch/src/index.js:9
import http from 'node:http';
^^^^^^
SyntaxError: Cannot use import statement outside a module
1 | 'use strict'
2 |
> 3 | import fetch from "node-fetch";
I have already set up my environment via following Jest - SyntaxError: Cannot use import statement outside a module. However, it seems the setting is only for use import in jest, not the case that modules in node_modules.
I also have added the following settings for transformIgnorePatterns, but it does not work.
"jest": {
"transform": {
"^.+\\.[t|j]sx?$": "babel-jest"
},
"transformIgnorePatterns": [
"/node_modules/(?!node:http)"
]
},
Could anyone please inform me how to resolve the issue? Thank you very much in advance.