Jest: regenerator 0.13.1 breaks Jest

Created on 4 Jan 2019  Â·  10Comments  Â·  Source: facebook/jest

Tests fail with ReferenceError: regeneratorRuntime is not defined after updating regenerator-runtime to 0.13.1. This is because regeneratorRuntime is no longer a global.

https://github.com/facebook/regenerator/issues/363

Per notes in that issue, we should be using @babel/plugin-transform-runtime now. Jest and its docs need to be updated accordingly. Thanks.

Documentation

Most helpful comment

If users have a recent node version they can use:

// babel.config.js
module.exports = {
    presets: [
        ['@babel/preset-env', { targets: { node: 'current' } }],
    ],
}

Using @babel/preset-env without that target set will result in the tests using async failing with ReferenceError: regeneratorRuntime is not defined.

If you're using a recent Node version I think that's the best setting for Jest. If you need to support other targets you may need to use something like:

// babel.config.js
// NOTE: If you use this your tests aren't running exactly the same code as your production code.
// They may compile faster and be easier to debug though.
module.exports = process.env.NODE_ENV === 'test' ? {
    presets: [
        ['@babel/preset-env', { targets: { node: 'current' } }],
    ],
} : {
    // Configuration suitable for your targets.
    // Possibly including `@babel/plugin-transform-runtime`.
}

All 10 comments

PR most welcome updating the docs.

If there's anything else that needs doing, please follow the issue template

@SimenB I'm a novice here. Can you guide me?

@open-source-explorer sorry, I don't know what needs doing. 😅 @mj1856 @thymikee?

Based on this comment https://github.com/facebook/regenerator/issues/363#issuecomment-440380606:

See #353. With [email protected], the global regeneratorRuntime variable is no longer automatically created (unless you evaluate the runtime code in global scope, rather than a Webpack wrapper), but you can import the runtime and define the variable globally yourself.

This is all handled for you if you use @babel/plugin-transform-runtime, by the way. Any file that needs regeneratorRuntime imports it from @babel/runtime/regenerator.

Maybe we don't need to include regenerator-runtime/runtime in setupFiles, but instead add @babel/plugin-transform-runtime inside babel-jest?

@thymikee I'm not familiar with the jest codebase. It would be great if you could guide me on which files are to be edited 😄

I opened https://github.com/facebook/jest/pull/7595 to track that. We also don't mention regenerator-runtime in master docs (for future release) so I'm not sure what should we change in the documentation – just add a mention that it's advised to use @babel/plugin-transform-runtime along with @babel/runtime to support async iterators with Babel instead of regenerator-runtime if you're on Babel7 and Jest 23? 🤔

If users have a recent node version they can use:

// babel.config.js
module.exports = {
    presets: [
        ['@babel/preset-env', { targets: { node: 'current' } }],
    ],
}

Using @babel/preset-env without that target set will result in the tests using async failing with ReferenceError: regeneratorRuntime is not defined.

If you're using a recent Node version I think that's the best setting for Jest. If you need to support other targets you may need to use something like:

// babel.config.js
// NOTE: If you use this your tests aren't running exactly the same code as your production code.
// They may compile faster and be easier to debug though.
module.exports = process.env.NODE_ENV === 'test' ? {
    presets: [
        ['@babel/preset-env', { targets: { node: 'current' } }],
    ],
} : {
    // Configuration suitable for your targets.
    // Possibly including `@babel/plugin-transform-runtime`.
}

I think we're good here? We've removed special handling of this in Jest 24 (you can test with [email protected])

[...] We've removed special handling of this in Jest 24 (you can test with [email protected])

Hmm Still got this issue using [email protected] 🤔 (Dylan's solution worked fine, though)

Hmm Still got this issue using [email protected] (Dylan's solution worked fine, though)

Same here! (although using jest 24.9.0)

Was this page helpful?
0 / 5 - 0 ratings