Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 11 commits into from Jan 20, 2019

Conversation

@thymikee
Copy link
Collaborator

@thymikee thymikee commented Jan 9, 2019

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
Copy link
Collaborator

@SimenB SimenB commented Jan 10, 2019

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
Copy link
Collaborator

@SimenB SimenB commented Jan 10, 2019

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

@thymikee
Copy link
Collaborator Author

@thymikee thymikee commented Jan 10, 2019

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
Copy link
Collaborator

@SimenB SimenB commented Jan 10, 2019

See #7599

@SimenB
Copy link
Collaborator

@SimenB SimenB commented Jan 10, 2019

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
Collaborator Author

@thymikee thymikee commented Jan 10, 2019

Yup, agreed

@thymikee thymikee force-pushed the thymikee:feat/use-transform-runtime branch from 25ccdfd to f3ca428 Jan 10, 2019
package.json Outdated Show resolved Hide resolved
@thymikee thymikee force-pushed the thymikee:feat/use-transform-runtime branch from 9fd72dd to 6abd74f Jan 10, 2019
examples/async/package.json Outdated Show resolved Hide resolved
@SimenB
Copy link
Collaborator

@SimenB SimenB commented Jan 10, 2019

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 thymikee:feat/use-transform-runtime branch from 6abd74f to 3db5e9f Jan 10, 2019
@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 thymikee:feat/use-transform-runtime branch 2 times, most recently from 00d0931 to 1c72e35 Jan 10, 2019
yarn.lock Outdated Show resolved Hide resolved
@SimenB
Copy link
Collaborator

@SimenB SimenB commented Jan 10, 2019

Woo, green 😀

@SimenB
SimenB approved these changes Jan 10, 2019
@SimenB SimenB requested review from cpojer, rubennorte and rickhanlonii Jan 10, 2019
@cpojer
Copy link
Collaborator

@cpojer cpojer commented Jan 15, 2019

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
Copy link
Collaborator

@SimenB SimenB commented Jan 15, 2019

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
Copy link
Collaborator

@SimenB SimenB commented Jan 15, 2019

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 Jan 15, 2019
…m-runtime

* upstream/master:
  add missing truncate comment to recent blog posts (#7655)
  use raw serializer for e2e output snapshots (#7651)
  chore: use a Set for reserved words list in `jest-mock`
  Fix automock for numeric function names (#7653)
  Update docs re: `moduleFileExtensions` to add ordering note (left-to-right) (#7616)
@thymikee thymikee merged commit 64b3a9b into facebook:master Jan 20, 2019
12 checks passed
12 checks passed
ci/circleci: lint-and-typecheck Your tests passed on CircleCI!
Details
ci/circleci: test-browser Your tests passed on CircleCI!
Details
ci/circleci: test-jest-circus Your tests passed on CircleCI!
Details
ci/circleci: test-node-10 Your tests passed on CircleCI!
Details
ci/circleci: test-node-11 Your tests passed on CircleCI!
Details
ci/circleci: test-node-6 Your tests passed on CircleCI!
Details
ci/circleci: test-node-8 Your tests passed on CircleCI!
Details
ci/circleci: test-or-deploy-website Your tests passed on CircleCI!
Details
continuous-integration/appveyor/pr AppVeyor build succeeded
Details
continuous-integration/travis-ci/pr The Travis CI build passed
Details
deploy/netlify Deploy preview ready!
Details
facebook.jest #20190120.3 succeeded
Details
@thymikee thymikee deleted the thymikee:feat/use-transform-runtime branch Jan 20, 2019
captain-yossarian added 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
Copy link

@hanzlahabib hanzlahabib commented Aug 19, 2019

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

@hanzlahabib hanzlahabib commented Aug 20, 2019

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
Copy link

@khanguslee khanguslee commented Nov 5, 2019

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
            }
        ]
    ]
}
@shinpou
Copy link

@shinpou shinpou commented Nov 27, 2020

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
Copy link
Collaborator

@SimenB SimenB commented Nov 27, 2020

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

@github-actions github-actions bot commented May 11, 2021

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
Linked issues

Successfully merging this pull request may close these issues.

None yet

8 participants