Jest: Error in Async Example: ReferenceError: regeneratorRuntime is not defined

Created on 12 Mar 2017  ·  21Comments  ·  Source: facebook/jest

I'm getting following error when trying to run an example from this directory:
https://github.com/facebook/jest/tree/master/examples/async

 FAIL  __tests__/user-test.js
  ● Test suite failed to run

    ReferenceError: regeneratorRuntime is not defined

      at Object.<anonymous> (__tests__/user-test.js:16:48)
      at process._tickCallback (internal/process/next_tick.js:103:7)

Test Suites: 1 failed, 1 total
Tests:       0 total
Snapshots:   0 total
Time:        1.495s
Ran all test suites.

Same issue can be reproduced using node 6.10 and node 7.7.4:

git clone https://github.com/facebook/jest jest
cd jest/examples/async
npm install
npm run test

Most helpful comment

This worked for me to fix "ReferenceError: regeneratorRuntime is not defined" in Jest:

npm install --save-dev @babel/plugin-transform-runtime

Then in .babelrc (besides other options):

{
  "env": {
    "test": {
      "plugins": ["@babel/plugin-transform-runtime"]
    }
  }
}

All 21 comments

This doesn't repro locally on our CI or locally. Maybe there is something wrong with your version of npm?

@cpojer

Thanks Chris.

This issue can be reproduced from docker container running node:latest from following repository:
https://hub.docker.com/_/node/

Below are the steps which lead to an error:

# from local machine (also tested in vagrant 1.9.2 running ubuntu/trusty64)
docker pull node:latest
docker run --rm -it node bash
# from inside the container (docker version 17.03.0-ce, node version 7.7.2)
git clone https://github.com/facebook/jest /jest
cd /jest
# as per instructions from https://github.com/facebook/jest/tree/master/examples
npm install
cd examples/async
npm install
npm test

Am I correct in the way I execute this example?
Is it possible that I'm missing some global requirements that will make this scenario pass?
Can I offer you any additional info?

Edit:
I found that example works if you replace regeneration-runtime with babel-polyfill in devDependencies.

I'm going to assume that jest in master is actually compatible with the former module, but the example doesn't use master code and instead pulls the latest release which may still require babel-polyfill.

I'm experiencing the same issue with Travis CI: https://travis-ci.org/okonet/lint-staged/jobs/212179781

Tests running okay locally but not on CI

I just bumped into this issue!

@mpontus @okonet you can use jest@next where it's fixed or otherwise you need install babel-polyfill.

Jest used to autoinclude babel-polyfill (and still does it in 19.0.2), but since it caused memory leaks, we moved to using regenerator-runtime only, which ships with our transitive deps (so since Jest v20 if you're using npm >=3 or yarn you don't need to install anything for async/await support).

The problem is that we eradicated babel-polyfill from docs, but forgot to include this diff https://github.com/facebook/jest/pull/2755 in 19.0.2 and didn't roll out a decent release since then (only jest@next tag).
Sorry about the inconvenience!

@thymikee How can I use regenerator-runtime as described in the docs along with a transform?
I'm not using any .babelrc file in my project and load a babel configuration which is shared for both webpack and jest configuration just like below:

jest.config.js

...
'transform': {
    '^.+\\.js$': '<rootDir>/config/jest.transform.js'
}
...

jest.transform.js

var babelConfig = require('./babel.config')

module.exports = require('babel-jest').createTransformer(babelConfig)

Sorry to wake up an old issue: I'm running into the same issue with Jest 20.0.4, and can't really suss out the take-away of these comments. From what I can see:

  • The async/await documentation advises to use babel-preset-env, but the async/await example uses babel-plugin-transform-runtime instead. Which is the recommended dependency?
  • Like okonet, the regeneratorRuntime crops up on CircleCI but not locally. Running my CI test script locally with --runInBand does not indicate that that's the issue.
  • It looks like the root cause is that regenerator-runtime needs to be installed directly if you're using an older package manager, which, on CI, is likely.

I was facing the same issue, importing the babel-polyfill directly into the jest.init.js (jest setup file) solved the issue for me.

  ...
 "setupFiles": [
   "<rootDir>/jest.init.js"
  ],
  ...
/// jest.init.js
import Enzyme from 'enzyme';
import Adapter from 'enzyme-adapter-react-16';
import 'babel-polyfill';

Sorry for the bump! But I wanted to share 😄 . Adding this in my general plugins works for me:

    [
      "transform-runtime",
      {
        "helpers": false,
        "polyfill": false,
        "regenerator": true
      }
    ]

I don't know if this is the best way of handling it, though.

This worked for me to fix "ReferenceError: regeneratorRuntime is not defined" in Jest:

npm install --save-dev @babel/plugin-transform-runtime

Then in .babelrc (besides other options):

{
  "env": {
    "test": {
      "plugins": ["@babel/plugin-transform-runtime"]
    }
  }
}

I was facing the same issue, importing the babel-polyfill directly into the jest.init.js (jest setup file) solved the issue for me.

  ...
 "setupFiles": [
   "<rootDir>/jest.init.js"
  ],
  ...
/// jest.init.js
import Enzyme from 'enzyme';
import Adapter from 'enzyme-adapter-react-16';
import 'babel-polyfill';

I tried importing the polyfill in jest.init.js but it broke my tests weirdly. I didn't have the undefined generator error but I had lots of errors that I didn't have previously in my tests, such as missing component imports or missing props.

My jest.conf.js contained the following line:

setupFiles: ['<rootDir>/test/unit/setup']

Hence I had to import the polyfill in setup.js instead of jest.init.js (which did not exist):

import Vue from 'vue'
import '@babel/polyfill';

Vue.config.productionTip = false
Vue.config.silent = true

_Except for the polyfill import line, the 3 other lines were already in the file when I opened it._

Importing the polyfill in setup.js worked while importing it in jest.init.js didn't work (I changed the setupFiles path).

Adding targets node current worked for me to fix "ReferenceError: regeneratorRuntime is not defined" in Jest:

{
  "presets": [
    [
      "@babel/preset-env",
      {
        "targets": {
          "node": "current"
        }
      }
    ]
  ]
}

Adding targets node current worked for me to fix "ReferenceError: regeneratorRuntime is not defined" in Jest:

{
  "presets": [
    [
      "@babel/preset-env",
      {
        "targets": {
          "node": "current"
        }
      }
    ]
  ]
}

This won't actually help babelyfing, my code won't be compiled with the target node current. It will look exactly the same (ES6). Tests would be running, great, but no compiled code :P

Here's a snippit of my package.json, this works for me now.

"babel": {
    "presets": [
      [
        "@babel/preset-env",
        {
          "targets": {
            "node": "current"
          }
        }
      ],
      "@babel/preset-react"
    ],
    "plugins": [
      "component-identification",
      "@babel/plugin-proposal-class-properties",
      [
        "module-resolver",
        {
          "root": [
            "./src"
          ]
        }
      ]
    ]
  },

For me it was not enough to have preset-env targets specified in common config.
I had to define the targets again explicitly for node under env.test.

{
  'presets': [
    [
      '@babel/preset-env',
      {
        'targets': {
          'chrome': 61,
          'node': 8,
          'electron': '2.0.9'
        },
      }
    ],
    '@babel/typescript',
    '@babel/preset-react'
  ],
  'env': {
    'test': {
      'presets': [
        [
          '@babel/preset-env',
          {
            'targets': {
              'node': 8,  // <- ADDED
            },
          }
        ],
        '@babel/typescript',
      ]
    }
  }
}

The only thing I had to do is to define the target in babel as mentioned by @AoDev. No additional targets elsewhere.

To import and define regenerator-runtime globally, you do:

require('regenerator-runtime/runtime');

For those that just want to add the required thing, and not mess around with adding all babel polyfills for jest.

This was mentioned in the blog post introducing Jest 24, if people missed it: https://jestjs.io/blog/2019/01/25/jest-24-refreshing-polished-typescript-friendly#breaking-changes. You need to configure babel properly, no need to change any code. See this comment from the PR: https://github.com/facebook/jest/pull/7595#issuecomment-454486373

@ghengeveld This is work for me, Thank you

What solved it for me, was to not just add the regenerator, but also the runtime to my config:

  'globals': {
    'vue-jest': {
      'babelConfig': {
        'plugins': [
          '@babel/plugin-transform-regenerator',
          '@babel/plugin-transform-runtime',
        ],
        'presets': [['@babel/preset-env', { 'modules': false }]],
        'env': {
          'test': {
            'presets': [
              ['@babel/preset-env', { 'targets': { 'node': 'current' } }],
            ],
          },
        },
      },
    },
  },

@here none of the attempts here have worked out for me , I still seem to be getting Test suite failed to run

ReferenceError: regeneratorRuntime is not defined

This my my package.json
"devDependencies": {
"@babel/core": "^7.6.2",
"@babel/plugin-transform-regenerator": "^7.8.7",
"@babel/runtime": "^7.9.6",
"@react-native-community/eslint-config": "^0.0.5",
"babel-eslint": "^10.0.1",
"babel-jest": "^24.9.0",
"babel-plugin-module-resolver": "^3.1.3",
"babel-plugin-transform-runtime": "^6.23.0",
"detox": "^16.5.1",

.babelrc
whenever I add something on this file , my app files will not run. I have to delete this file back to have my reactive native app running

Encountered same issue. Fresh jest (26.6.3), plain async test and this..
For a framework that defines itself as "delightful" this really is not.
Why on earth do I need to bother about babel configuration, plugins etc.
Why can't it work out of the box?

upd. In my case, I had babel.config.js already but was missing
targets: { node: 'current' }

Was this page helpful?
0 / 5 - 0 ratings

Related issues

StephanBijzitter picture StephanBijzitter  ·  3Comments

hramos picture hramos  ·  3Comments

samzhang111 picture samzhang111  ·  3Comments

paularmstrong picture paularmstrong  ·  3Comments

gustavjf picture gustavjf  ·  3Comments