Jest: Jest seems not working with babel v7

Created on 6 Mar 2018  路  14Comments  路  Source: facebook/jest

Do you want to request a _feature_ or report a _bug_?
_bug_

What is the current behavior?
I get an TypeError: Cannot read property 'loose' of undefined (While processing preset: "/Users/x/Desktop/jest-test/node_modules/babel-preset-react-app/index.js") on using jest v22.4.2 with babel v7

I've searched for this error and found this Bug in Babel, which suggests that when babel-core instead of @babel/core is required by any package, it will break. And it seems like jest-runtime v22.4.2 (currently the newest one) is doing exactly this.

If the current behavior is a bug, please provide the steps to reproduce and
either a repl.it demo through https://repl.it/languages/jest or a minimal
repository on GitHub that we can yarn install and yarn test.

I created this GitHub Repo

What is the expected behavior?
No error and it works with babel v7

Please provide your exact Jest configuration and mention your Jest, node,
yarn/npm version and operating system.

  • Operating System: MacOS 10.13.2 (High Sierra)
  • Nodejs: v9.4.0
  • npm: v5.7.1

(all other dependencies can be found in the aforementioned GitHub Repo)

Most helpful comment

same issue. i have @babel/core and babel-core@^7.0.0-0 installed.
the strange thing is when i try to use .babelrc' it works. but when i usebabel.config.jsit dosent work in that case. here is mybabel.config.js`

  const babelConfig = (api) => {
  // api.cache.never();
  const config = {
    presets: [
      ['@babel/preset-env', { modules: false, useBuiltIns: 'usage' }],
      '@babel/preset-react',
    ],
    plugins: [
      'react-hot-loader/babel',
      '@babel/plugin-proposal-object-rest-spread',
      '@babel/plugin-proposal-class-properties',
      '@babel/plugin-syntax-dynamic-import',
      ['import', { libraryName: 'antd', libraryDirectory: 'es', style: false }],
      [
        'module-resolver',
        {
          root: [
            './src',
          ],
          alias: {
            App: './src/App',
            Common: './src/App/Common',
            Elements: './src/App/Elements',
            Router: './src/App/Router',
            Constants: './src/Constants',
            Services: './src/Services',
            Store: './src/Store',
            Styles: './src/Styles',
            Assets: './src/Assets',
          },
        },
      ],
    ],
  };
  if (api.env('test')) {
    config.presets.splice(0, 1, ['@babel/preset-env', { useBuiltIns: 'usage' }]); // if i remove this nothing changes
    config.plugins.push('@babel/plugin-transform-modules-commonjs');
  }
  return config;
};

module.exports = babelConfig; 

All 14 comments

Ahh forget it, in the Docs its npm install --save-dev babel-jest 'babel-core@^7.0.0-0' @babel/core regenerator-runtime, so both babel-core and @babel-core need to be installed 馃 must have missed that, sorry 鉁岋笍

so both babel-core and @babel-core need to be installed

yes

Why both babel-core and @babel/core, is there a tracking issue for the package that is referring to babel-core?

Just upgraded babel from 7.0.0-beta.44 to 7.0.0-beta.46 and all my tests fail with Unexpected token import. Looks like .babelrc is ignored. Is it known issue?

I see babel changes the way .babelrc files are discovered and it introduces babel.config.js, but I could not find any documentation. See:

https://github.com/babel/babel/releases/tag/v7.0.0-beta.45

and

https://github.com/babel/babel/pull/7784

That's #6053

Thanks - will check it out.

same issue. i have @babel/core and babel-core@^7.0.0-0 installed.
the strange thing is when i try to use .babelrc' it works. but when i usebabel.config.jsit dosent work in that case. here is mybabel.config.js`

  const babelConfig = (api) => {
  // api.cache.never();
  const config = {
    presets: [
      ['@babel/preset-env', { modules: false, useBuiltIns: 'usage' }],
      '@babel/preset-react',
    ],
    plugins: [
      'react-hot-loader/babel',
      '@babel/plugin-proposal-object-rest-spread',
      '@babel/plugin-proposal-class-properties',
      '@babel/plugin-syntax-dynamic-import',
      ['import', { libraryName: 'antd', libraryDirectory: 'es', style: false }],
      [
        'module-resolver',
        {
          root: [
            './src',
          ],
          alias: {
            App: './src/App',
            Common: './src/App/Common',
            Elements: './src/App/Elements',
            Router: './src/App/Router',
            Constants: './src/Constants',
            Services: './src/Services',
            Store: './src/Store',
            Styles: './src/Styles',
            Assets: './src/Assets',
          },
        },
      ],
    ],
  };
  if (api.env('test')) {
    config.presets.splice(0, 1, ['@babel/preset-env', { useBuiltIns: 'usage' }]); // if i remove this nothing changes
    config.plugins.push('@babel/plugin-transform-modules-commonjs');
  }
  return config;
};

module.exports = babelConfig; 

@hannadrehman I just had this issue and it worked in my docker container, I think it's the version of yarn that will cause this. 1.6.0 results in your issue, 1.7.0 seems to work well.

@hannadrehman i have the same issue.

had similar issue - i could not get jest 24 to work with any combination of babel 7 when using latest yarn. i had to switch back to npm install which seemed to properly set up the dependent package hoisting.

Jest 24 does not need the bridge at all

pardon me. the bridge package was likely one of the many package combinations i tried to get jest 24 to work with babel 7. what worked for me was installing both @babel/core": "^7.2.2" and "babel-core": "^7.0.0-0", and switching from yarn to npm. i've edited the above comment to not include bridge package.

@hannadrehman
I have the similar issue - tests failed with

TypeError: require(...) is not a function

It's happens after configuring @babel/polyfill https://babeljs.io/docs/en/babel-polyfill.

...
"@babel/core": "^7.2.2",
"babel-jest": "^24.1.0",
"jest": "^24.1.0",
...

Solved by changing { useBuiltIns: 'usage' } to { useBuiltIns: 'entry' }

Was this page helpful?
0 / 5 - 0 ratings