question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. ItĀ collects links to all the places you might be looking at while hunting down a tough bug.

And, if youā€™re still stuck at the end, weā€™re happy to hop on a call to see how we can help out.

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.

Dynamic Configuration Docs

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:closed
  • Created 5 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

8reactions
mweibelcommented, Aug 30, 2018

I have the same issue and found an ugly workaround for now:

// FIXME: remove once babel-jest supports babel.config.js
module.exports = require('./babel.config')({
  cache: () => {},
  env: () => process.env.NODE_ENV
})

Put this into .babelrc.js and remove .babelrc.

šŸ‘ on adding support for @babel/core 7

0reactions
github-actions[bot]commented, May 12, 2021

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found