Skip to content
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 fails on dynamic babel.config.js due to old "babel-core" dependency #6916

Closed
jahed opened this issue Aug 29, 2018 · 5 comments
Closed

Comments

@jahed
Copy link

jahed commented Aug 29, 2018

🐛 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
@jahed
Copy link
Author

jahed commented Aug 29, 2018

I'd assume the fix here is to drop babel-core support and move over to @babel/core. I've done this in a fork, but I can't run tests on my machine (well, I can but there are various errors even without my own changes).

The diff for my changes can be seen here for anyone interested. I also changed the caching logic to use loadOptions since resolving the dynamic config is a bit tricky without it. However, it'll likely have performance issues.

https://github.com/jahed/jest/compare/master...jahed:feat%2Fbabel-7-options?diff=unified&name=feat%2Fbabel-7-options

@jasonbellamy
Copy link

I'm having the same issue where babel-jest just started failing with with the babel 7 release.

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

@mweibel
Copy link
Contributor

mweibel commented 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

@SimenB
Copy link
Member

SimenB commented Aug 30, 2018

Closing as duplicate of #6913, but feel free to chime in there with your reproduction case.

@SimenB SimenB closed this as completed Aug 30, 2018
@github-actions
Copy link

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.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 12, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants