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
- Update jest to 24.1.0 To avoid errors with using `async` in the jest tests on v24, this fix was required: https://github.com/facebook/jest/issues/3126#issuecomment-465926747 — committed to WikiEducationFoundation/WikiEduDashboard by ragesoss 5 years ago
- Update for unit testing - https://github.com/facebook/jest/issues/3126 - https://stackoverflow.com/questions/60876624/how-to-test-mock-out-react-hooks — committed to WizelineLabs/gitwize-fe by wize-hieunguyen 4 years ago
- Snackbar (#332) * First commit * Simple version of snackbar * Add support for async/await in tests https://github.com/facebook/jest/issues/3126\#issuecomment-483320742 * Tests for snackbar ... — committed to equinor/design-system by wenche 4 years ago
- Install axe for accessibility testing. Also added babel-polyfill to setup, which is no longer automatically included with jest. See https://github.com/facebook/jest/issues/3126#issuecomment-34594932... — committed to shalin1/frontend-takehome-parcel by shalin1 4 years ago
This worked for me to fix “ReferenceError: regeneratorRuntime is not defined” in Jest:
Then in .babelrc (besides other options):
I was facing the same issue, importing the
babel-polyfill
directly into thejest.init.js
(jest setup file) solved the issue for me.Adding targets node current worked for me to fix “ReferenceError: regeneratorRuntime is not defined” in Jest:
To import and define regenerator-runtime globally, you do:
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 installbabel-polyfill
.Jest used to autoinclude
babel-polyfill
(and still does it in 19.0.2), but since it caused memory leaks, we moved to usingregenerator-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 in19.0.2
and didn’t roll out a decent release since then (onlyjest@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:
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
underenv.test
.@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
jest.transform.js
Sorry for the bump! But I wanted to share 😄 . Adding this in my general plugins works for me:
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:
babel-preset-env
, but the async/await example uses babel-plugin-transform-runtime instead. Which is the recommended dependency?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.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
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
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.