-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Jest with TypeScript and ES modules can't import "named imports" from cjs modules #11563
Comments
In hopes that it speeds things along and removes any question as to ownership, I forked the repository provided by @themaskedavenger and removed all typescript related stuff to show it's a jest issue. https://github.com/k2snowman69/tsjestcjerepro Hopefully this removes any doubt that this is a ts or a ts-jest issue Tested with node 14.17.1 and node v16.1.0 |
Okay so dug into this a bit and hopefully my investigation helps whoever ends up fixing this on jest's side. The first and important thing to note is that jest does not use nodejs to resolve files. This tripped me up a lot until I figured this out. Both jest and nodejs use globGlob's export code looks like enzymeEnzyme's export code looks like
however it seems that tslibtslib actually supports cjs, es6 through the When running the following code in node in either cjs or esm there are no errors (as expected)
However when running the following test in Jest ESM:
You'll get the following error:
Which means that jest's resolver isn't resolving the same file that node js is resolving that eventually gets sent to SummaryHopefully that gives some guidance to someone who investigates this and maybe we can at least fix this for tslib. To fix this for glob however, you'll need to fix it in |
This happens to me even with react. import React from 'react'
const {useState} = React And |
Just ran into this myself...incredibly frustrating bug. |
+1 I am seeing the same issue with a very similar setup (jest with ESM, relying on named exports from a CJS module). |
Is there no fix for this yet? |
+1 also wondering if there is a solution for this? |
@k2snowman69 Thanks for your detailed analysis of the root cause of the problem. Like @yqrashawn i am also facing Jest not able to do perform named export even in react package. Current Temp solution is to import the whole package default and extract individual exports
But its a pain staking process, and i would like to automate somehow, any suggestion. |
This issue is stale because it has been open for 1 year with no activity. Remove stale label or comment or this will be closed in 30 days. |
not stale |
Can anyone provide an example of where node is able to import using native ESM, but Jest fails? E.g. React does not support native ESM: facebook/react#11503 |
I can confirm the issue still exists for us. I updated the repo linked by the issue author to the latest version of jest/ts-jest and you can see the issue still persists: https://github.com/SebastienGllmt/tsjestcjerepro |
Had this issue with another package and such approach solved it for me. |
🐛 Bug Report
Using TS, exports from CJS modules can be imported with syntax as if they were named ES module exports, e.g.:
However, with Jest and ES modules, when this style of import is in a test file or in a dependency of a test file, it says
SyntaxError: The requested module 'glob' does not provide an export named 'sync'
Going through all one by one and changing them to
import glob from 'glob';
and then callingglob.sync()
seems to work, however when migrating some legacy stuff from another test runner to Jest this may not be an option, because there are a lot of those such imports in the codebase.Is there a way around this, so that
import { whatever } from 'whatever';
will work for CJS modules?To Reproduce
Steps to reproduce the behavior:
Running jest with:
node --experimental-vm-modules node_modules/jest/bin/jest.js
(as described in https://jestjs.io/docs/ecmascript-modules), and using Jest config:Expected behavior
import { sync } from 'glob'
and similar imports from CJS modules work.Link to repl or repo (highly encouraged)
https://github.com/themaskedavenger/tsjestcjerepro
envinfo
The text was updated successfully, but these errors were encountered: