-
-
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 #9395
Comments
I resolved the issue by doing the following: yarn add ts-jest -D then in your module.exports = {
transform: {
'^.+\\.ts?$': 'ts-jest',
},
}; |
Correct, ESM is not supported. You can track #4842. In the meantime, you need to transpile to cjs (using babel, typescript, etc) EDIT: Since this issue is still getting traffic, please see https://jestjs.io/docs/en/ecmascript-modules |
A more complete set of steps is:
After this, I was able to get files with imports working just fine. |
I had the same problem when I started to used babel... But later, I had a solution... I haven't had the problem anymore so far... Currently, Node v12.14.1, "@babel/node": "^7.8.4", I use babel-node and nodemon to execute (node is fine as well..) package.json: launch.json: Of course, with babel-node, you also normally need and edit another file, such as babel.config.js/.babelrc file |
Awsome workaround!! FYI for some people, if you are using a .babelrc the changes didn't work for me. I had to delete my .babelrc it and just use the suggested babel.config.json |
@smpeters solution worked for me, but I want to see if I can explain exactly why it worked, because TBH I have no idea what those steps actually do (but thank you @smpeters for posting them!)
Whew! |
@smpeters I'm getting a Any ideas about this? |
@changyeamoon I think that's because If using Node, {
"presets": [
[
"@babel/preset-env",
{
"targets": {
"node": true
}
}
]
]
} |
I solved the fucking thing by migrating the |
How did you do the migration? |
Well I spent a whole day on this trying to figure out... turns out for me I reused a .js file from another project that was set to compiled everything. The new project did not and that .js file was exporting an ESM. So I changed the name to .ts and bang zoom! There's probably a way to include .js in the compile so it gets converted to ESM but since I'm converting everything to TS anyway I didn't bother. Please feel free to let me know though if you find out. With the current version of ts-jest I didn't need the babel stuff. ts-jest 25.5.1 and typescript 3.9.3. this is my jest.config.js
|
Dear @smpeters |
I solved it for my React Native project. First I initialized a new TypeScript template with
Then I configured |
In my case, there was a problem loading That did the trick. Hope it helps someone else. |
That's really helped, I've shocked too :) |
I've came across this issue and wanted to leave a solution that worked in my case. We are using some internally developed library that we use in our apps. Because of that the libraries are only delivered as ES6 modules, so trying to test them threw an error from the issue title. What I had to do to fix was:
Where |
Without this, if we start using something from @zulip/shared and then try to run Jest, it fails with a SyntaxError. The fix is to tell Jest to have Babel compile the file first. We can do this with the steps described here (on an issue reporting the same error message I saw before making this fix): jestjs/jest#9395 (comment)
My solution works for React JSX files. The other is for Typescript files.
They are similar solutions to similar problems.
…On Sun, Aug 30, 2020 at 6:05 AM Danny Hinshaw ***@***.***> wrote:
I'm just over here trying to figure out why @ziishaned
<https://github.com/ziishaned> 's answer is downvoted to oblivion when it
works for me:
[image: Screen Shot 2020-08-30 at 7 02 40 AM]
<https://user-images.githubusercontent.com/24684771/91657446-f4b54f00-ea8e-11ea-8b10-fbd90c2de9ec.png>
Meanwhile @smpeters <https://github.com/smpeters> solution seems to be
widely accepted and *doesn't* work for me...:
[image: Screen Shot 2020-08-30 at 7 04 51 AM]
<https://user-images.githubusercontent.com/24684771/91657475-1e6e7600-ea8f-11ea-95f1-1e0312b8bdf8.png>
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#9395 (comment)>, or
unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAAJQUTAG2GFPTYSA2ZXJGTSDIW63ANCNFSM4KFPEIEQ>
.
|
For new coming, use this jest.config.js module.exports = {
preset: 'ts-jest/presets/js-with-babel'
}; |
@abrcdf1023 thank you. |
@abrcdf1023 This work's for me! Thanks! |
Shame nothing works for me. This is even on a new create-react-app, so all my babel, jest, etc. configurations are all clean. I can get jest to work with typescript for a typescript file on its own, but the moment I try to import my other typescript module, it just doesn't seem to recognise the "import" keyword, despite the keyword being recognised right above the import statement, when I import another file...
How jest cannot seem to compile or recognise my external library that's in typescript is beyond me |
None of this worked for me. I do not use TypeScript. I just upgraded it from Node v12.10 to v14.5, fixed certain invalid Inside my module.exports = {
"presets": [
[
"@babel/preset-env",
{
targets: {
node: true,
},
}
]
]
}; and inside my module.exports = {
testEnvironment: "node",
bail: 2,
transform: {
"^.+\\.jsx?$": "babel-jest"
},
}; (In fact, even the option |
i spent over 4 hours and i stumbled upon this comment |
For the record, I had to migrate tests that were in js so they can be used with typescript and thanks to @abrcdf1023's comment, I was able to find this solution that works like a charm:
|
What worked for me was changing the {
"presets": [
["@babel/preset-env", { "modules": "auto" }]
]
} |
What worked for me:
|
@yanickrochon any luck for you? I have a similar setup to yours, none of the other above solutions have worked for me. |
My setup looks like this:
Can't get test to run. Does anything look wrong here to anyone? I've read every thread related to this issue |
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. |
When you use ECMAScript Modules in Node (v13.6.0 for me) combined with Jest, the following error occurs:
Here is the test code I am trying to execute.
I suspect the issue is Jest is not configured to run Node code using "type: module" (ECMAScript Modules).
The text was updated successfully, but these errors were encountered: