babel-jest does not transpile import/export in node_modules
See original GitHub issue🐛 Bug Report
This is a copy from https://github.com/facebook/jest/issues/6229 which is closed and not fixed still Jest throws error “SyntaxError: Cannot use import statement outside a module” for libraries imported from node_modules.
To Reproduce
Steps to reproduce the behavior:
npm install konva
If import Konva from "konva";
exist in the file from which you are going to import function you want to test by jest it will throw this error.
If i set "transformIgnorePatterns": ["node_modules/"]
- it would not help
If i set "transformIgnorePatterns": ["node_modules/(?!(konva)/)"]
- it would throw another error Cannot find module 'canvas' from 'node_modules/konva/lib/index-node.js'
By the way handling those packajes by hand it is really bad idea.
If you install canvas by npm install canvas
and run npm run test
again it will throw
Cannot find module '../build/Release/canvas.node'
Require stack:
- /Users/mad/projects/game of life/node_modules/canvas/lib/bindings.js
- /Users/mad/projects/game of life/node_modules/canvas/lib/canvas.js
- /Users/mad/projects/game of life/node_modules/canvas/index.js
- /Users/mad/projects/game of life/node_modules/jsdom/lib/jsdom/utils.js
- /Users/mad/projects/game of life/node_modules/jsdom/lib/jsdom/living/events/MouseEvent-impl.js
- /Users/mad/projects/game of life/node_modules/jsdom/lib/jsdom/living/generated/MouseEvent.js
- /Users/mad/projects/game of life/node_modules/jsdom/lib/jsdom/living/events/EventTarget-impl.js
- /Users/mad/projects/game of life/node_modules/jsdom/lib/jsdom/living/generated/EventTarget.js
- /Users/mad/projects/game of life/node_modules/jsdom/lib/jsdom/living/interfaces.js
- /Users/mad/projects/game of life/node_modules/jsdom/lib/jsdom/browser/Window.js
- /Users/mad/projects/game of life/node_modules/jsdom/lib/api.js
- /Users/mad/projects/game of life/node_modules/jest-environment-jsdom/build/index.js
- /Users/mad/projects/game of life/node_modules/jest-util/build/requireOrImportModule.js
- /Users/mad/projects/game of life/node_modules/jest-util/build/index.js
- /Users/mad/projects/game of life/node_modules/@jest/core/build/cli/index.js
- /Users/mad/projects/game of life/node_modules/@jest/core/build/jest.js
- /Users/mad/projects/game of life/node_modules/jest-cli/build/cli/index.js
- /Users/mad/projects/game of life/node_modules/jest-cli/bin/jest.js
- /Users/mad/projects/game of life/node_modules/jest/bin/jest.js
Expected behavior
Well, you should not get any errors. Also, I think it is overhead, it is unnecessary for Jest to go and check all of the node modules libraries and their dependencies and check them. Right now it works only if you isolate each function to a separate file without any dependencies from node modules.
Link to repl or repo (highly encouraged)
https://github.com/maliyshock/game-of-life
npm install
npm run test
Non of provided solutions from https://github.com/facebook/jest/issues/6229 do not work.
Issue Analytics
- State:
- Created 2 years ago
- Comments:14 (5 by maintainers)
Top GitHub Comments
This is going in circles.
As has been stated, Jest doesn’t touch
node_modules
except for loading code from there. If the code is ESM and not CJS you have two options - migrate to using ESM in your code base (https://jestjs.io/docs/ecmascript-modules) or tell Jest to transform the code (i.e. tweakingtransformIgnorePatterns
). The factcanvas
cannot be found has nothing to do with Jest but ratherkonva
not declaring it as a dependency (https://www.runpkg.com/?konva@8.1.3/lib/index-node.js#2).I would like to chip in and say that I have the same issue as maily. However with the module
node-fetch
.I’ve nailed it down to
babel-jest
being the issue as I got it working withts-jest
.Working configuration:
jest.config.json
:tsconfig.json
:Non-working configution:
jest.config.json
:.babelrc
: