Skip to content
This repository has been archived by the owner on Sep 30, 2020. It is now read-only.

Support async/await in jest tests #105

Closed
thektan opened this issue Jun 4, 2019 · 3 comments
Closed

Support async/await in jest tests #105

thektan opened this issue Jun 4, 2019 · 3 comments

Comments

@thektan
Copy link
Member

thektan commented Jun 4, 2019

When using async/await in tests the following error is thrown:

ReferenceError: regeneratorRuntime is not defined

Possible Solutions

Option 1

According to Jest docs:

To enable async/await in your project, install @babel/preset-env and enable the feature in your babel.config.js file.

In .babelrc

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

(Sources: jestjs/jest#3126 (comment), babel/babel#7601 (comment))

Option 2

Installing @babel/plugin-transform-runtime and adding the following configuration to .babelrc

{
  "env": {
    "test": {
      "plugins": ["@babel/plugin-transform-runtime"]
    }
  }
}

(Sources: jestjs/jest#3126 (comment), babel/babel#8829 (comment))

Option 3

Importing babel-polyfill to the jest setup.js file. Which would mean adding babel-polyfill as a new devDependency.

import 'babel-polyfill';

(Source: babel/babel#5085 (comment))

Final Comments

I'm currently using Option 2 since it scopes the plugin to just the tests (where the issue is only occurring), but I'd like to hear everyone else's opinion since I haven't noticed any differences with any of these fixes besides Option 3 throwing a new error Warning: Can't perform a React state update on an unmounted component in the tests (but that's probably something I'd need to fix in the component/tests itself).

@wincent
Copy link
Contributor

wincent commented Jun 12, 2019

Given that we currently use Node v10.15.1 in liferay-portal (which has native async/await support), it would be nice to see how far we can get with "Option 1", because it means Babel will skip some of the heavier-weight transforms that it does (including relying on the regenerator runtime) — I think we should be able to apply this for the test environment only.

@thektan
Copy link
Member Author

thektan commented Jun 12, 2019

@wincent It looks like we could do something like this example from the jest docs:

Jest will set process.env.NODE_ENV to 'test' if it's not set to something else. You can use that in your configuration to conditionally setup only the compilation needed for Jest, e.g.

// babel.config.js
module.exports = api => {
  const isTest = api.env('test');
  // You can use isTest to determine what presets and plugins to use.

  return {
    // ...
  };
};

We also might need to add jest-babel and @babel/core? The docs also say:

To use Babel, install required dependencies via yarn:

yarn add --dev babel-jest @babel/core @babel/preset-env

thektan added a commit to thektan/liferay-npm-tools that referenced this issue Jun 27, 2019
thektan added a commit to thektan/liferay-npm-tools that referenced this issue Jun 27, 2019
thektan added a commit to thektan/liferay-npm-tools that referenced this issue Jun 27, 2019
thektan added a commit to thektan/liferay-npm-tools that referenced this issue Jul 1, 2019
thektan added a commit to thektan/liferay-npm-tools that referenced this issue Jul 1, 2019
wincent pushed a commit to thektan/liferay-npm-tools that referenced this issue Jul 2, 2019
wincent added a commit that referenced this issue Jul 2, 2019
feat: support async/await in jest tests (#105)
@wincent
Copy link
Contributor

wincent commented Jul 3, 2019

Closing this because I am about to cut a release, so this will go to liferay-portal master today (at least, I'll submit the PR today).

@wincent wincent closed this as completed Jul 3, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants