babel-jest fails on dynamic babel.config.js due to old "babel-core" dependency
See original GitHub issueš Bug Report
Babel 7 added support for dynamic configuration, however, because babel-jest
is dependant on babel-core
, itās using the wrong version of babel for loading options and running transformations. Babel 7 uses @babel/core@^7.0.0
which is an entirely different module.
As such, it doesnāt load the userās @babel/core
but instead loads the one provided by jest-config
transiently which does not pick up the new babel.config.js
format introduced in 7.
To Reproduce
Run jest using babel-jest in a project with:
- Babel 7 (release, which uses scoped packages)
- A dynamic
babel.config.js
(i.e. a module which resolves to a function which returns the babel options). - A tested module using syntax that isnāt supported in NodeJS (like ES6 imports)
It will error.
Test output
FAIL packages/css/src/escapeCssUrl.test.js
ā Test suite failed to run
Jest encountered an unexpected token
This usually means that you are trying to import a file which Jest cannot parse, e.g. it's not plain JavaScript.
By default, if Jest sees a Babel config, it will use that to transform your files, ignoring "node_modules".
Here's what you can do:
ā¢ To have some of your "node_modules" files transformed, you can specify a custom "transformIgnorePatterns" in your config.
ā¢ If you need a custom transformation specify a "transform" option in your config.
ā¢ If you simply want to mock your non-JS modules (e.g. binary assets) you can stub them out with the "moduleNameMapper" config option.
You'll find more details and examples of these config options in the docs:
https://jestjs.io/docs/en/configuration.html
Details:
[redacted]\packages\css\src\escapeCssUrl.test.js:1
({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){import escapeCssUrl from './escapeCssUrl';
^^^^^^
SyntaxError: Unexpected token import
at ScriptTransformer._transformAndBuildScript (node_modules/jest-runtime/build/script_transformer.js:403:17)
Expected behavior
I expected jest to run tests using the dynamic babel configuration without issue.
Link to repl or repo (highly encouraged)
I can try setting one up if needed.
Run npx envinfo --preset jest
Paste the results here:
Environment:
OS: Windows 10
Node: 8.11.1
Yarn: 1.9.4
npm: 5.6.0
Watchman: Not Found
Xcode: N/A
Android Studio: Not Found
Issue Analytics
- State:
- Created 5 years ago
- Comments:5 (1 by maintainers)
Top Results From Across the Web
Configure Babel
Create a file called babel.config.json with the following content at the root of your ... You are allowed to access any Node.js APIs,...
Read more >Ask Question - Stack Overflow
I tried to dellete babel-core but when i want to start project it says that babel-loader needs babel-core. Any help would be nice,...
Read more >eslint: failed to load config "next/babel" to extend from. - You.com
[22:43:02.518] info - Disabled SWC as replacement for Babel because of custom Babel configuration ".babelrc" https://nextjs.org/docs/messages/swc-disabledĀ ...
Read more >babel-jest | Yarn - Package Manager
... compile JavaScript code using Babel. yarn add --dev babel-jest @babel/core ... [jest-config] Do not warn about preset in ProjectConfig #13583Ā ...
Read more >Build errors with images when upgrading from Ubuntu Xenial ...
I looked at Gatsby build fails after updating build image to Ubuntu ... babel-jest@25.1.0" has unmet peer dependency "@babel/core@^7.0.0".
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I have the same issue and found an ugly workaround for now:
Put this into
.babelrc.js
and remove.babelrc
.š on adding support for @babel/core 7
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. Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.