-
-
Notifications
You must be signed in to change notification settings - Fork 6.4k
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
babel-jest does not transpile import/export in node_modules when Babel 7 is used #6229
Comments
I don't think Jest should add babel transforms, beyond the ones it needs for its own purposes. That said, Jest should load your babel config itself, so weird that it doesn't work... |
babel-jest doesn't load the confic, it reads it and uses it for caching, but to my knowledge it leaves babelrc handling up to Babel as it should. However Babel no longer uses project config inside node_modules. So the only one able to add config for node_modules at this point is babel-jest. |
Oh, you're correct, I confused it with the merge done by the This seems more like a Babel issue rather than Jest, though. Is there an issue over there about it, and discussing the use case of transpiling |
No, this is a change they explicitly made. They decided project config shouldn't affect node_modules, which is entirely reasonable since project config can contain stage-0, react/jsx, flow and other things that should not be used in packages. So it doesn't make sense for this to be considered a Babel bug or have an issue there. They explicitly decided .babelrc should not affect node_modules. And they already give whoever is calling babel-core to transform files the ability to define plugins/presets that will run on the file even if it is in node_modules. Babel isn't doing anything unreasonable. |
Any updates on this? This is behaviour which was working before and is now not, which seems to be to be a clear regression. Fair enough if the behaviour is to be removed, but lacking any clear documentation indicating that, this just feels like a bug. |
I had the same issue and I found this comment. |
Thanks for the suggestion @mpospelov. Unfortunately, it didn't seem to work for me. |
I have a problem on Mac also. |
this happend to me when i updated a react-native project to 0.56.0. Since then, jest test can't be run anymore. |
Any additional thoughts on this @SimenB? I'm also seeing this issue. |
I've got the same issue as @marcelerz. Ever since updating to React Native 56.0, |
For the record, I was able to fix the issue of my module in the
"transformIgnorePatterns": [
// Change MODULE_NAME_HERE to your module that isn't being compiled
"/node_modules/(?!MODULE_NAME_HERE).+\\.js$"
] |
Thanks @austinkelleher We had something similar but a bit different, your fix didn't work first - but then we realized that we were missing the |
babel-jest requires babel config to be stored in .js file to transform JSX expressions via @babel/preset-react. See jestjs/jest#6229 (comment)
package.json
Versions:
/internals/jest/settings.js
Could you please help? |
Renaming .babelrc to babel.config.js worked for me |
See #6053 (comment), it explains the need for |
SimenB, that solution is not working for vue-cli. We don't have .babelrc, or .babel.config.js. I tried creating a basic one, it didn't work. |
@MasteR-7 , i had similar problem, my advice to u is the followings:
|
This is needed because of jestjs/jest#6229
faced the same issue, followed this steps to resolve
|
I got this problem while trying to make run npm test https://stackoverflow.com/questions/61391878/problem-while-testing-es6-class-with-jest |
I ran into a similar error when trying to import a package from node_modules whose index.js is an ES Module. After a lot of painful debugging, I was able to resolve it by moving my babel configuration out of |
@jtbandes I made the changes you suggest. I rename the file .babelrc by babel.config.js. I also add the jest.config.js file and I removed the jest configuration from package.json. But now when I run npm run test then this error appears: babel.config.js: Error while loading config - Unexpected token ': My babel.config.js has this content: { Pd. Now the npm run production command is not working anymore. Now babel is not capable of transpile the export keyword. Before the suggestion the transpilation of the code running ok, the only problem was just the testing with jest while running npm run test. |
babel.config.js is a JS file, not a JSON file. You need to use |
Man I spent countless hours trying to figure out why Maybe the error that's displayed below when there's an unexpected token should explicitly state that it only works with
|
Reference this issue: jestjs/jest#6229
The error handling for this issue needs to be updated with a direct link to the solution unless a fix is going to be implemented as it's quite absurd how obscure and nonsensical this issue is still ongoing in 2020. On second thought, if you consider it's 2020 it's quite the norm. Good day |
Feel free to send a PR improving the error |
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
I started getting the dreaded
SyntaxError: Unexpected token import
error again forlodash-es
. And I spent hours debugging it, because I already had .babelrc and package.json:jest configured properly ("modules": "commonjs"
, ajest.transform
forbabel-jest
, andtransformIgnorePatterns
set to not ignorenode_modules/lodash-es
).After a lot of debugging and a little searching, it appears that Babel 7 has stopped using the project's
.babelrc
code innode_modules
. This means that all the previous suggestions to just telltransformIgnorePatterns
to transpile certain packages in node_modules and set"modules": "commonjs"
in the test env to make WebPackimport
work with jest no longer work.To Reproduce
Steps to reproduce the behavior:
lodash-es
into your projectbabel-jest
andtransformIgnorePatterns
to not ignore the es-modules using packageExpected behavior
When babel-jest is used to transpile ES-modules using packages like lodash-es it should transpile the imports to CommonJS so they work with Jest.
Practically I believe it should be sufficient to add
transform-modules-commonjs
to the plugins list thatbabel-jest
uses when the filename is anode_modules
file and Babel 7 is used. This would also fix the fact that it's not good practice for the project's.babelrc
(which may also include plugins for non-standard things like JSX and flow) to be used just to transpile ES6 / modules code in packages.Link to repl or repo (highly encouraged)
https://gist.github.com/dantman/820f6232acc6f53bd1e57d21b09e1f89
Run
npx envinfo --preset jest
Paste the results here:
The text was updated successfully, but these errors were encountered: