-
-
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
Unexpected Token Import for ES6 modules #2081
Comments
I have this same issue with React-Native and Jest, then install a module like npm install native-base --save npm test will throw this error: |
I am having the same issue, the link @cpojer provided doesn't seem to help 😕 |
Hi! 'transformIgnorePatterns' does....mmmm...that it must does - ignoring) |
This props helped me :)
UPD1
... and it's working for me :) Best regards, Pavel Tretyakov |
Hi guys, I am still hacing this problem, but I am not using react native. The problem is always fixed using jest --no-cache. Is there a way to make jest work using cache, without throwing this error? |
I cannot get this transform to work with my create-react-app application. Always get "Unexpected token import" on any test files containing ES6 import. I've tried all suggestions I could find. no-cache has no impact. I obviously can't ignore these files from the transform (they need to be transformed!). I give up. Happy to provide any required information. |
I seem to also be experiencing this issue. |
Same here |
1 similar comment
Same here |
You are all likely running into this problem because ES modules are not supported in Node and you deliberately stopped compiling them with your build pipeline (webpack, rollup) when you are shipping them to the browser. You need to enable compilation from ES modules to CommonJS for the "test" env in your babelrc, see https://babeljs.io/docs/usage/babelrc/#env-option and https://babeljs.io/docs/plugins/transform-es2015-modules-commonjs/ {
"env": {
"production": {
"plugins": ["transform-es2015-modules-commonjs"]
}
}
} |
Thanks for your response, cpojer. Yesterday I removed my node env settings and let it ran with default node env, because jest was working globally, but not while using node scripts. It seemed to solve the issue, so I believe that my problem was with node-env settings. But it is still something to monitor. EDIT: This did not work at all. @cpojer's fix worked like a charm. Thanks. |
@cpojer 's fix worked for me, thanks |
Awesome, also worked for me, thanks! |
I am having a similar issue in my fairly simple react native app. When running .babelrc
.package.json
Does the env snippet suggested by @cpojer go in the .package.json? |
@JessicaBarclay it should go in your .babelrc config. Like this:
|
Ahh Thank you @rlage ! Sorted :) |
FWIW, I actually had the my Before, failing with "Unexpected token import":
After, successfully transforming files:
|
Based on the Jest doc, it should not be needed, since Jest sets the NODE_ENV to test by defult.
This .babelrc is what worked for me: {"env": {
"development": {
"plugins": ["transform-es2015-modules-commonjs"]
},
"test": {
"plugins": ["transform-es2015-modules-commonjs"]
}
}} NOTE: it looks odd, but both development and test keys must be present, otherwise it will not work. Then I just call: node_modules/.bin/jest or with package.json: {"scripts": {"test": "jest"}} No need for setting NODE_ENV to test. |
@laszbalo Interesting, the presence of that second |
@jlyman which solution in docs? |
@doudounannan Sorry if that wasn't clear. The solution I ended up going with was what I posted here. Like @laszbalo said, it goes against what the docs say under Using Babel, but either @laszbalo's solution or my direct injection of |
Strangely when I use
I got the idea from #2550 (comment) since nothing else in #770 or here seemed to help... |
Doesn't matter what I do, it's always the same error:
|
I'm having the same issue. I added the following to my jest
and now I get the following error when I run Jest:
|
@calcazar what's in your node_modules/enzyme/.babelrc? I solved it by using babel-jest and ts-jest. In package.json:
Then in .babelrc (which I only use for testing)
hope that helps someone! (This is for an aurelia app so YMMV) |
This is what I see. as an FYI I'm running Enzyme V3.0
|
@thinkOfaNumber thank you for posting your suggestion. I used it to figure out how to get es6 modules from an external library to get compiled in jest. The key is here "jest": {
"modulePaths": [
"<rootDir>/src",
"<rootDir>/node_modules"
],
"transformIgnorePatterns": ["/node_modules/(?!lodash-es)"], // <-- this allows babel to load only the node modules I need (which is lodash-es) and ignore the rest This is the part that I need it. Thank you so much! |
As someone who found this page while searching for a similar issue that is likely irrelevant to the original discussion. My issue ended up being due to using the function-like dynamic imports described by https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import#Dynamic_Imports. This ended up being solved by adding the babel plugin for this feature available at https://github.com/airbnb/babel-plugin-dynamic-import-node and modifying the .babelrc accordingly.
|
i just remove the option :{ module:false }, and then it works "presets": [
["@babel/env", {module: false} ],
"@babel/preset-react",
"@babel/preset-typescript"
] |
adding |
@ngmgit but this means that you transpile all your node_modules, which is not very optimal... |
In my case, hope it helped! |
None of EVERYTHING that is on here works to me. I cant believe that a facebook`s official testing library to test React applications is that level of hard to configure, i am a developer with 8+ years of experience and i spent 5 hours of my day without success going trhough so many issues and questions on stack overflow. i just cant configure my react application to work with jest + react-testing-library this is ONLY because jest is yelling every time to me a new syntax error that he cant proccess. Every plugin i use solve one problem and give another one. There is way to make babel handle everything? I am sincerely done with that i really given up, i am no longer spending more time on that, i will probably cancel adding this library on the project. Seriously, it is very weird to see that there is NO default configuration for ANYONE that wants to test a react application using jest + react-testing-library. |
after hours of trial and error with many a SO answer and github issue, @cpojer's answer worked for me! 🙏 UPDATED with latest and greatest with some additional context that helped me:
|
This issue can also happen when not running jest in the project's root directory... |
If you are using
|
pnpm supportI want to leave a note here for pnpm users.... The following example doesn't work for pnpm users because pnpm paths are different. The above regexes only work with flattened
https://jestjs.io/docs/en/tutorial-react-native#transformignorepatterns-customization FixUse a negative lookahead for the last occurence by adding
|
Shame that even after all these years this is still an issue. CRA + Jest works fine with Typescript, with 0 config, until it doesn't. Everything goes well doing basic work, until you need to import another Typescript library, then your compiler will start to complain and your Jest tests won't work, because its not smart enough to understand Typescript. And then when you google all these solutions/workarounds on stackoverflow, on github, etc. none of these actually do anything. I just wish there was an in-built way for Jest to work with typescript imported modules. Setting @loganpowell |
This code helped me:
where "test" - your enviroment |
been wracking my head over this for many hours now, literally NOTHING in this thread works. why is the solution so inconsistent??? this shouldn't be such a hard problem to solve but i've wasted almost an entire day of work on this, and still nothing works. can't believe this issue has been open for 5 years. |
It was closed 3 days after it was opened, 4 years ago Please see https://jestjs.io/docs/en/ecmascript-modules |
Do you want to request a feature or report a bug?
Report a bug
What is the current behavior?
I am using the same testing setup in several projects right now, all from my boilerplate. This problem has popped up in all of them recently, but I haven't yet been able to track it down. It seems that Jest is missing the babel configuration in my package.json and the test suite is failing with 'Unexpected Token Import'. Please keep in mind that up until a week ago, this configuration was working successfully in several projects, so I am assuming it's a regression and figured I should report it.
Note that when I add a .babelrc file the test suite passes for all but one test, where I get the same Unexpected Token Import, which seems bizarre considering there are a dozen passing tests that all use ES6 imports. Without the .babelrc all 14 tests are failing.
If the current behavior is a bug, please provide the steps to reproduce and if possible a minimal repository on GitHub that we can
npm install
andnpm test
.It can be reproduced from my boilerplate by installing
npm run setup
and runningnpm run test
.What is the expected behavior?
I expect that the Jest configuration that worked previously would still work. It would seem that my preprocessor is no longer running with babel-jest as it was previously.
Run Jest again with
--debug
and provide the full configuration it prints. Please mention your node and npm version and operating system.The text was updated successfully, but these errors were encountered: