Skip to content

chore(breaking): remove regenerator-runtime injection#7595

Merged
thymikee merged 11 commits into
jestjs:masterfrom
thymikee:feat/use-transform-runtime
Jan 20, 2019
Merged

chore(breaking): remove regenerator-runtime injection#7595
thymikee merged 11 commits into
jestjs:masterfrom
thymikee:feat/use-transform-runtime

Conversation

@thymikee

@thymikee thymikee commented Jan 9, 2019

Copy link
Copy Markdown
Contributor

Summary

An attempt to get rid of regenerator-runtime addition in setupFiles. It's currently not working, but wanted @SimenB to see what can be wrong.

Test plan

Fix automock test failures

@SimenB

SimenB commented Jan 10, 2019

Copy link
Copy Markdown
Member

Why do we need the regenerator runtime at all? If it's our own code, shouldn't the babel transform add whatever it needs to transpile async? If it's for users, why can't they setup transpilation of async-await themselves?

@SimenB

SimenB commented Jan 10, 2019

Copy link
Copy Markdown
Member

As for the test failures, the changed stack trace looks awesome! Not sure about the automock ones, I'll take a look

Comment thread packages/babel-jest/src/index.js Outdated
@thymikee

Copy link
Copy Markdown
Contributor Author

I think it's there for historic reasons, to make Jest "just work" with Babel's generators but I'd need @cpojer to back this up.

With Babel 6 it required globally available regenerator-runtime, but since Babel 7 it looks like regenerator-runtime is hidden inside @babel/runtime and accessible through the plugin and users should use that? Why it's not included in the @babal/preset-env though?

@SimenB

SimenB commented Jan 10, 2019

Copy link
Copy Markdown
Member

See #7599

@SimenB

SimenB commented Jan 10, 2019

Copy link
Copy Markdown
Member

to make Jest "just work" with Babel's generators

Now that node 8+ supports async-await out of the box, I'm not sure this makes sense. Seems like people should configure Babel properly instead if they want node 6?

@thymikee

Copy link
Copy Markdown
Contributor Author

Yup, agreed

@thymikee thymikee force-pushed the feat/use-transform-runtime branch from 25ccdfd to f3ca428 Compare January 10, 2019 10:25
Comment thread package.json Outdated
Comment thread packages/babel-jest/package.json Outdated
@thymikee thymikee force-pushed the feat/use-transform-runtime branch from 9fd72dd to 6abd74f Compare January 10, 2019 10:42
Comment thread examples/async/.babelrc.js
Comment thread examples/async/package.json
@SimenB

SimenB commented Jan 10, 2019

Copy link
Copy Markdown
Member

Should update the title of this PR, as well as update the changelog (marking it as breaking, I guess)

@SimenB SimenB added this to the Jest 24 milestone Jan 10, 2019
@thymikee thymikee force-pushed the feat/use-transform-runtime branch from 6abd74f to 3db5e9f Compare January 10, 2019 10:49
@thymikee thymikee changed the title [WIP] feat: use @babel/plugin-transform-runtime instead of regenerator-runtime feat(breaking): remove regenerator-runtime injection Jan 10, 2019
@thymikee thymikee changed the title feat(breaking): remove regenerator-runtime injection chore(breaking): remove regenerator-runtime injection Jan 10, 2019
@thymikee thymikee force-pushed the feat/use-transform-runtime branch 2 times, most recently from 00d0931 to 1c72e35 Compare January 10, 2019 11:17
Comment thread yarn.lock Outdated
Comment thread e2e/native-async-mock/babel.config.js Outdated
@codecov-io

codecov-io commented Jan 10, 2019

Copy link
Copy Markdown

Codecov Report

Merging #7595 into master will decrease coverage by 0.07%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #7595      +/-   ##
==========================================
- Coverage   68.24%   68.16%   -0.08%     
==========================================
  Files         251      251              
  Lines        9639     9634       -5     
  Branches        5        5              
==========================================
- Hits         6578     6567      -11     
- Misses       3059     3065       +6     
  Partials        2        2
Impacted Files Coverage Δ
packages/jest-config/src/normalize.js 82.3% <100%> (-0.72%) ⬇️
packages/jest-runtime/src/index.js 75.51% <0%> (-1.48%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 5fc391f...df4350f. Read the comment docs.

@SimenB

SimenB commented Jan 10, 2019

Copy link
Copy Markdown
Member

Woo, green 😀

@cpojer

cpojer commented Jan 15, 2019

Copy link
Copy Markdown
Member

I'm ok with this change. @thymikee is indeed right, this was for making Babel and async/await work out of the box in the past. If this works fine with babel 7, I'm ok with it. Just make sure that if people compile async/await down to ES5 + regenerator runtime in their setup, this change won't break them (alternatively ask them to just drop the transform in the test environment, but it may require setup).

@SimenB

SimenB commented Jan 15, 2019

Copy link
Copy Markdown
Member

Users who just use @babel/plugin-transform-regenerator will get this error:
image

But it's not Jest's fault that they do not include it - they'd get the same error when using webpack or whatever and opening their JS in chrome.

They should use @babel/preset-env instead, IMO, and I think it's fine to break them. Just me? 😅

@SimenB

SimenB commented Jan 15, 2019

Copy link
Copy Markdown
Member

Adding @babel/plugin-transform-runtime works, which is mentioned in the docs: https://babeljs.io/docs/en/babel-plugin-transform-runtime#regenerator-aliasing

(just make sure to set sourceType to either script or unambiguous, otherwise the injected helper uses import)

module.exports = {
  plugins: [
    '@babel/plugin-transform-regenerator',
    '@babel/plugin-transform-runtime',
  ],
  sourceType: 'script',
};

An alternative to setting sourceType is to use @babel/plugin-transform-modules-commonjs


I'll push a test with that as an integration test

SimenB and others added 3 commits January 15, 2019 18:51
…m-runtime

* upstream/master:
  add missing truncate comment to recent blog posts (jestjs#7655)
  use raw serializer for e2e output snapshots (jestjs#7651)
  chore: use a Set for reserved words list in `jest-mock`
  Fix automock for numeric function names (jestjs#7653)
  Update docs re: `moduleFileExtensions` to add ordering note (left-to-right) (jestjs#7616)
@thymikee thymikee merged commit 64b3a9b into jestjs:master Jan 20, 2019
@thymikee thymikee deleted the feat/use-transform-runtime branch January 20, 2019 10:44
captain-yossarian pushed a commit to captain-yossarian/jest that referenced this pull request Jul 18, 2019
* feat: use @babel/plugin-transform-runtime instead of regenerator-runtime

* fix async example

* add changelog

* add babel/core and babel-jest to all examples

* fix native async test

* chore: dedupe lockfile

* remove babel from native transform test

* add test for async regenerator
@hanzlahabib

hanzlahabib commented Aug 19, 2019

Copy link
Copy Markdown

i am kind of stuck on this error, i have searched through many stackoverflow issues, and other github repos issues, couldn't find issue to resolve regeneratorRuntime is not defined
i even tried @SimenB solution but that throws new list of errors,
almost spent half day around this issue couldn't get to the solution

here is what i am trying to do,
this is how my babel.config.js looks like

const isTest = String(process.env.NODE_ENV) === 'test'
module.exports = {
presets: [
'@babel/preset-env',
{ modules: isTest ? 'commonjs' : false },
'@babel/preset-react',
],
env: {
test: {
presets: [
'@babel/preset-env',
'@babel/preset-react',
],
plugins: [
'@babel/plugin-proposal-class-properties',
'@babel/plugin-transform-modules-commonjs',
'babel-plugin-dynamic-import-node',
'syntax-dynamic-import',
'@babel/plugin-proposal-object-rest-spread',
'transform-class-properties',
],
},
},
}

and here is my jest test component file

import React from 'react';
import ReactDOM from 'react-dom'
import commonPage from '../../pages/common/commonPage';
import { mount, shallow, render } from '../../Enzyme';

test('test common page test', () =>{
const container = document.createElement('div');
ReactDOM(, container);
console.log(container)
})

after running npm test it throw
ReferenceError: regeneratorRuntime is not defined

tried many solutions but couldn't reach to solution
any body who wants to see package.json dependencies
here they are

{"name": "primer-react",
"version": "0.1.0",
"private": true,
"devDependencies": {
"@babel/core": "^7.1.6",
"@babel/plugin-proposal-object-rest-spread": "^7.0.0",
"@babel/plugin-transform-modules-commonjs": "^7.5.0",
"@babel/plugin-transform-runtime": "^7.5.5",
"@babel/preset-env": "^7.1.6",
"@babel/preset-react": "^7.0.0",
"@babel/runtime": "^7.0.0-beta.55",
"babel-plugin-syntax-dynamic-import": "^6.18.0",
"babel-plugin-transform-class-properties": "^6.24.1",
"concurrently": "3.6.0",
"enzyme": "^3.9.0",
"enzyme-adapter-react-16": "^1.13.1",
"eslint-config-airbnb": ### "^17.1.1",
"eslint-plugin-import": "^2.18.2",
"eslint-plugin-jsx-a11y": "^6.2.3",
"eslint-plugin-react": "^7.14.3",
"husky": "^3.0.2",
"identity-obj-proxy": "^3.0.0",
"jest-enzyme": "^7.0.2",
"lint-staged": "^9.2.1",
"moxios": "^0.4.0",
"prettier": "^1.18.2",
"pretty-quick": "^1.11.1",
"react-test-renderer": "^16.8.6",
"redux-immutable-state-invariant": "^2.1.0",
"redux-logger": "^3.0.6",
"regenerator-runtime": "^0.13.3"
},
"dependencies": {
"@babel/polyfill": "^7.4.4",
},
}

Sorry for bad formatting i am new i couldn't make it better then this

@hanzlahabib

Copy link
Copy Markdown

Thanks Allah my issue is fixed,
here's what worked for me

  • installed @babel/transform-runtime as dev dependency

and added this in babel.config.js plugins object

["@babel/transform-runtime", {"regenerator": true }] ]

plugins: [
["@babel/transform-runtime", {"regenerator": true }] ],
]

@khanguslee

khanguslee commented Nov 5, 2019

Copy link
Copy Markdown

Thanks Allah my issue is fixed,
here's what worked for me

  • installed @babel/transform-runtime as dev dependency

and added this in babel.config.js plugins object

["@babel/transform-runtime", {"regenerator": true }] ]

plugins: [
["@babel/transform-runtime", {"regenerator": true }] ],
]

@hanzlahabib Think you may have accidentally left out the word plugin in the library. Following this for steps:
Install @babel/plugin-transform-runtime as a dev dependancy

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

Install @babel/runtime as a dependancy

npm install @babel/runtime

Then chuck this into your babel configuration:

{
    "plugins": [
        [
            "@babel/plugin-transform-runtime",
            {
                "regenerator": true
            }
        ]
    ]
}

@shinkathe

Copy link
Copy Markdown

I don't understand in what circumstance "@babel/plugin-transform-runtime" needs to be included? This error occurs for us when using react-table's useAsyncDebounce inside a jest test, but I don't know if the correct way to address this issue is to install transform-runtime, or to make some alterations to which node we are targeting - what exactly is the set of tooling/configuration that we should have where this issue does not occur?

@SimenB

SimenB commented Nov 27, 2020

Copy link
Copy Markdown
Member

I don't understand in what circumstance "@babel/plugin-transform-runtime" needs to be included?

When you transpile async-await syntax.

Either leave async-await alone, or make sure to include the runtime.


Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions

Copy link
Copy Markdown

This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions Bot locked as resolved and limited conversation to collaborators May 11, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants