-
-
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
SyntaxError: Cannot use import statement outside a module, with TypeScript and ES Modules #9860
Comments
This is due to this check: https://github.com/facebook/jest/blob/6a0f070e0210eb11408a7c8ebb003ff73d62e420/packages/jest-resolve/src/shouldLoadAsEsm.ts#L51-L55 Not sure how to detect if it's an actual module or should use synthetic modules (like JSON). All current code only expects JS (we feed it to |
@askirmas: doesn't that config transform imports, hence making moot the point of Jest natively supporting ES Modules? |
Yup. It's quite simple to fix, but I don't think we want to special case Right now the logic is
What I'm thinking makes sense is to make that last one behave like |
Agree. I haven't seen TypeScript code yet that wasn't using import/export.
|
Sure, but after transplantation it has to still use |
'transplantation', nice autocorrect 😅 but with Babel, TS is a completely different thing than CJS, if you use plugin-typescript but not plugin-transform-modules-commonjs, types get stripped but it remains ESM |
Hah, yeah. Which is why I think we might need an option instead of forcing or otherwise trying to infer it |
Hi, I stumbled across this issue and thought I'd say hi. I'm the maintainer of CoffeeScript and I've been dealing with this same issue. In my case, there are plenty of legacy |
Thanks for chiming in! I think that makes sense for us as well - it's what we already do for CJS - we require you to transform whatever you import to CJS before Jest will load it. And if you've opted into ESM for |
cc @lmiller1990
@SimenB Is it safe to use this approach for Maybe only the case when transforming files from |
Getting the same issue |
I had the same problem. I've fixed that by including js files in ts-node:
|
Thanks Mahdi jan. Worked for me as well. |
My current thinking is that a transformer should return the format of the code it has transpiled. Right now a transformer returns: https://github.com/facebook/jest/blob/1535af7659e0392b3f7c6124fa58d230907ee38d/packages/jest-types/src/Transform.ts#L9-L14 I'm thinking in addition it can return a A problem with this approach is that it might not be possible for a transformer to know - e.g. with the |
No, we won't be passing the result of one transformer into another. What would happen is that a user specifies |
I see - I did not realize Jest could execute ESM (I have always been compiling to cjs). Is this via babel-jest? This would be great, then we can target ESM and not bother with the cjs compilation step (vue-jest already supports both ESM and CJS). |
Yeah, I'm currently working (on and off) on adding native ESM support, see #9430. This issue is a small, but important part of that work 🙂 |
I tried your suggestion, but unfortunately no luck for me yet. Project consists of .mjs, and commonJS. I added the transform in the package.json file and I receive the error on this line of code: I have to remove above line and add this part of code in the jest.config.file before my tests finally pass:
As soon as I remove above transform and use your suggestion, it will throw me the same syntax error. |
Yeah, that's suggested syntax, it's not implemented yet. Will be included in Jest 27 which will come sometime before Christmas (hopefully earlier, but I don't wanna make any promises I'm not able to keep) |
OK, I've changed my mind (again) - I'm introducing a new top-level option called Other behavior will remain as is - |
What is the advantage over the previous approach ? Is this top level option a part of global jest config or is it a part of |
part of the Jest project config. This is passed to transformers, so you can use it if you want. I'd recommend relying on the passed |
I've published |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
solved by adding Basically, import modules are available on commonjs |
This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
🐛 Bug Report
Filing a separate issue at @SimenB's suggestion.
After following the steps to get native ESM support, I'm running into the following error in a project that transpiles TypeScript files using
@babel/preset-typescript
:To Reproduce
git clone https://github.com/dandv/jest-typescript-es-modules.git
cd jest-typescript-es-modules
npm install
npm test
Expected behavior
Test passes.
Link to repl or repo (highly encouraged)
https://github.com/dandv/jest-typescript-es-modules
envinfo
The text was updated successfully, but these errors were encountered: