Jest: Jest fails to work with Babel7

Created on 15 Sep 2018  ยท  6Comments  ยท  Source: facebook/jest

๐Ÿ› Bug Report

A clear and concise description of what the bug is.
I have a simple project where I'm using Babel 7.0.0 instead of 6. Jest fails to work with it, saying that it can't find the "env" preset, even thought it is installed and indicated in the package-based config.

To Reproduce

Steps to reproduce the behavior:
My package.json:

  "scripts": {
    "test": "jest --coverage --no-cache"
  },
  "babel": {
    "presets": [
      "env"
    ]
  },
  "devDependencies": {
    "@babel/core": "^7.0.1",
    "@babel/preset-env": "^7.0.0",
    "babel-jest": "^23.6.0",
    "jest": "^23.6.0",
    "regenerator-runtime": "^0.12.1"
  }

Expected behavior

Expect the test to run. When I down grade to Babel 6.x it works as expected. When I try to use Babel 7, it fails.

A clear and concise description of what you expected to happen.
When I run npm t I get the following error:

โ— Test suite failed to run

    Couldn't find preset "env" relative to directory "/Users/rbiggs/Desktop/jest-test"

      at node_modules/babel-core/lib/transformation/file/options/option-manager.js:293:19
          at Array.map (<anonymous>)

Link to repl or repo (highly encouraged)

https://github.com/rbiggs/jest-test

Please provide either a repl.it demo or a minimal repository on GitHub.

Issues without a reproduction link are likely to stall.

Run npx envinfo --preset jest

Paste the results here:

 System:
    OS: macOS 10.14
    CPU: x64 Intel(R) Core(TM) M-5Y71 CPU @ 1.20GHz
  Binaries:
    Node: 8.11.2 - /usr/local/bin/node
    npm: 5.6.0 - /usr/local/bin/npm
  npmPackages:
    jest: ^23.6.0 => 23.6.0 

Output from VSCode:

screen shot 2018-09-15 at 12 09 48 am

Most helpful comment

Actually, this is the package setup that got this working for me:

  "devDependencies": {
    "@babel/core": "^7.0.1",
    "@babel/preset-env": "^7.0.0",
    "babel-core": "^7.0.0-bridge.0",
    "babel-jest": "^23.6.0"
}

All 6 comments

You have to use babel-core bridge to support Babel 7. https://github.com/facebook/jest/tree/master/packages/babel-jest#usage

@milesj is correct ๐Ÿ™‚

Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@milesj ok for bridge.. but why?

PS: @SimenB Google is redirecting here.. so maybe we can add some explanation.. this is frustrating for many people

@yvele Because Jest uses babel-core, which is still v6. To use v7, you need to use the bridge which pipes babel-core (v6) to @babel/core (v7) internally.

@milesj Thanks that makes sense. Reading https://github.com/facebook/jest/pull/6949 I understand that jest will soon be upgraded fully with Babel 7 ๐Ÿ˜‰

Actually, this is the package setup that got this working for me:

  "devDependencies": {
    "@babel/core": "^7.0.1",
    "@babel/preset-env": "^7.0.0",
    "babel-core": "^7.0.0-bridge.0",
    "babel-jest": "^23.6.0"
}
Was this page helpful?
0 / 5 - 0 ratings