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

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

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Reactions: 1
  • Comments: 22 (5 by maintainers)

Commits related to this issue

Most upvoted comments

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';

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

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

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.

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!

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' } }],
            ],
          },
        },
      },
    },
  },

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',
      ]
    }
  }
}

@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 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.

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.

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' }

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

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 😛

@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

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"
          ]
        }
      ]
    ]
  },