question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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:closed
  • Created 2 years ago
  • Comments:14 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
SimenBcommented, Sep 13, 2021

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. tweaking transformIgnorePatterns). The fact canvas cannot be found has nothing to do with Jest but rather konva not declaring it as a dependency (https://www.runpkg.com/?konva@8.1.3/lib/index-node.js#2).

1reaction
basickarlcommented, Sep 3, 2021

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 with ts-jest.

Working configuration:

jest.config.json:

{
  "transformIgnorePatterns": ["node_modules/(?!(node-fetch)/)"],
  "transform": {
    "^.+\\.[t|j]sx?$": "ts-jest"
  }
}

tsconfig.json:

{
  "compilerOptions": {
    "allowJs": true
  }
}

Non-working configution:

jest.config.json:

{
  "transformIgnorePatterns": ["node_modules/(?!(node-fetch)/)"],
  "transform": {
    "^.+\\.[t|j]sx?$": "babel-jest"
  }
}

.babelrc:

{
  "presets": [
    [
      "@babel/preset-env",
      {
        "targets": {
          "node": "current"
        }
      }
    ]
  ]
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

babel-jest does not transpile import/export in node_modules ...
Write a Jest test that imports the file you import the es-modules using package; Setup a recent version of Babel 7; Configure .babelrc...
Read more >
babel-jest doesn't handle ES6 within modules - Stack Overflow
Jest doesn't seem to have a problem using ES6 imports however as soon as it hits on an import statement within one of...
Read more >
Developers - babel-jest does not transpile import/export in ...
Write a Jest test that imports the file you import the es-modules using package; Setup a recent version of Babel 7; Configure .babelrc...
Read more >
Code Transformation - Jest
While babel-jest by default will transpile TypeScript files, Babel will not verify the types. If you want that you can use ts-jest ....
Read more >
babel/plugin-transform-modules-commonjs
js does since Babel allows accessing any property of module.exports as a named export, while Node.js only allows importing statically analyzable properties of ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found