Jest: 异步示例中的错误:ReferenceError: regeneratorRuntime 未定义

创建于 2017-03-12  ·  21评论  ·  资料来源: facebook/jest

尝试从此目录运行示例时出现以下错误:
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.

使用节点 6.10 和节点 7.7.4 可以重现相同的问题:

git clone https://github.com/facebook/jest jest
cd jest/examples/async
npm install
npm run test

最有用的评论

这对我来说可以修复 Jest 中的“ReferenceError:regeneratorRuntime 未定义”:

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

然后在 .babelrc 中(除了其他选项):

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

所有21条评论

这不会在我们的 CI 或本地复制。 也许你的 npm 版本有问题?

@cpojer

谢谢克里斯。

这个问题可以从 docker container running node:latest从以下存储库中重现:
https://hub.docker.com/_/node/

以下是导致错误的步骤:

# from local machine (also tested in vagrant 1.9.2 running ubuntu/trusty64)
docker pull node:latest
docker run --rm -it node bash
# from inside the container (docker version 17.03.0-ce, node version 7.7.2)
git clone https://github.com/facebook/jest /jest
cd /jest
# as per instructions from https://github.com/facebook/jest/tree/master/examples
npm install
cd examples/async
npm install
npm test

我执行此示例的方式是否正确?
我是否有可能遗漏了一些使这种情况通过的全局要求?
我可以为您提供任何其他信息吗?

编辑:
我发现如果你在 devDependencies 中用 babel-polyfill 替换再生运行时,这个例子就可以工作。

我将假设 jest in master 实际上与前一个模块兼容,但该示例不使用主代码,而是提取可能仍需要 babel-polyfill 的最新版本。

我在使用 Travis CI 时遇到了同样的问题: https :

测试在本地运行正常,但在 CI 上运行不正常

我刚碰到这个问题!

@mpontus @okonet您可以在已修复的地方使用jest@next ,否则您需要安装babel-polyfill

Jest 曾经自动包含babel-polyfill (并且在 19.0.2 中仍然这样做),但由于它导致内存泄漏,我们转向仅使用regenerator-runtime ,它与我们的传递 deps 一起提供(所以自从 Jest v20 如果你使用 npm >=3 或 yarn,你不需要安装任何东西来支持 async/await)。

问题是我们从文档中删除了babel-polyfill ,但忘记在19.0.2包含这个差异https://github.com/facebook/jest/pull/2755并且没有推出从那时起发布不错的版本(只有jest@next标签)。
给您带来的不便深表歉意!

@thymikee如何使用文档中描述的regenerator-runtime以及转换?
我没有在我的项目中使用任何.babelrc文件并加载一个为 webpack 和 jest 配置共享的 babel 配置,如下所示:

jest.config.js

...
'transform': {
    '^.+\\.js$': '<rootDir>/config/jest.transform.js'
}
...

jest.transform.js

var babelConfig = require('./babel.config')

module.exports = require('babel-jest').createTransformer(babelConfig)

很抱歉唤醒了一个老问题:我在 Jest 20.0.4 中遇到了同样的问题,并且无法真正理解这些评论的内容。 据我所知:

  • async/await 文档建议使用babel-preset-env ,但async/await 示例使用 babel-plugin-transform-runtime代替。 推荐的依赖是哪个?
  • okonet ,regeneratorRuntime 出现在CircleCI 上,但不在本地。 使用--runInBand在本地运行我的 CI 测试脚本并不表示这是问题所在。
  • 看起来根本原因是如果您使用的是较旧的包管理器,则需要直接安装regenerator-runtime ,这在 CI 上很可能。

我遇到了同样的问题,将babel-polyfill直接导入jest.init.js (开玩笑的设置文件)为我解决了这个问题。

  ...
 "setupFiles": [
   "<rootDir>/jest.init.js"
  ],
  ...
/// jest.init.js
import Enzyme from 'enzyme';
import Adapter from 'enzyme-adapter-react-16';
import 'babel-polyfill';

对不起撞了! 但我想分享😄。 在我的通用插件中添加它对我有用:

    [
      "transform-runtime",
      {
        "helpers": false,
        "polyfill": false,
        "regenerator": true
      }
    ]

不过,我不知道这是否是最好的处理方式。

这对我来说可以修复 Jest 中的“ReferenceError:regeneratorRuntime 未定义”:

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

然后在 .babelrc 中(除了其他选项):

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

我遇到了同样的问题,将babel-polyfill直接导入jest.init.js (开玩笑的设置文件)为我解决了这个问题。

  ...
 "setupFiles": [
   "<rootDir>/jest.init.js"
  ],
  ...
/// jest.init.js
import Enzyme from 'enzyme';
import Adapter from 'enzyme-adapter-react-16';
import 'babel-polyfill';

我尝试在jest.init.js导入 polyfill,但它奇怪地破坏了我的测试。 我没有未定义的生成器错误,但我有很多以前在测试中没有的错误,例如缺少组件导入或缺少道具。

我的jest.conf.js包含以下行:

setupFiles: ['<rootDir>/test/unit/setup']

因此我不得不在setup.js而不是jest.init.js (它不存在)中导入 polyfill:

import Vue from 'vue'
import '@babel/polyfill';

Vue.config.productionTip = false
Vue.config.silent = true

_除了 polyfill 导入行,当我打开文件时,其他 3 行已经在文件中了。_

setup.js导入 polyfill 起作用,而在jest.init.js导入它不起作用(我更改了 setupFiles 路径)。

添加目标节点当前对我有用,可以修复 Jest 中的“ReferenceError:regeneratorRuntime 未定义”:

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

添加目标节点当前对我有用,可以修复 Jest 中的“ReferenceError:regeneratorRuntime 未定义”:

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

这实际上不会帮助婴儿,我的代码不会用当前的目标节点编译。 它看起来完全一样(ES6)。 测试将运行,很好,但没有编译代码:P

这是我的 package.json 的片段,现在对我有用。

"babel": {
    "presets": [
      [
        "@babel/preset-env",
        {
          "targets": {
            "node": "current"
          }
        }
      ],
      "@babel/preset-react"
    ],
    "plugins": [
      "component-identification",
      "@babel/plugin-proposal-class-properties",
      [
        "module-resolver",
        {
          "root": [
            "./src"
          ]
        }
      ]
    ]
  },

对我来说,在公共配置中指定预设环境目标是不够的。
我必须在env.test下为node再次明确定义目标。

{
  'presets': [
    [
      '@babel/preset-env',
      {
        'targets': {
          'chrome': 61,
          'node': 8,
          'electron': '2.0.9'
        },
      }
    ],
    '@babel/typescript',
    '@babel/preset-react'
  ],
  'env': {
    'test': {
      'presets': [
        [
          '@babel/preset-env',
          {
            'targets': {
              'node': 8,  // <- ADDED
            },
          }
        ],
        '@babel/typescript',
      ]
    }
  }
}

我唯一要做的就是在@AoDev 提到的 babel 中定义目标。 其他地方没有其他目标。

要全局导入和定义 regenerator-runtime,您可以:

require('regenerator-runtime/runtime');

对于那些只想添加所需的东西,而不是为了开玩笑而添加所有 babel polyfill 的人。

这在介绍 Jest 24 的博客文章中提到,如果人们错过了它: https: //jestjs.io/blog/2019/01/25/jest-24-refreshing-poliished-typescript-friendly#break -changes。您需要正确配置 babel,无需更改任何代码。请参阅 PR 的评论: https :

@ghengeveld这对我

为我解决了这个问题,不仅要添加再生器,还要将运行时添加到我的配置中:

  'globals': {
    'vue-jest': {
      'babelConfig': {
        'plugins': [
          '@babel/plugin-transform-regenerator',
          '@babel/plugin-transform-runtime',
        ],
        'presets': [['@babel/preset-env', { 'modules': false }]],
        'env': {
          'test': {
            'presets': [
              ['@babel/preset-env', { 'targets': { 'node': 'current' } }],
            ],
          },
        },
      },
    },
  },

@here这里的任何尝试都没有为我解决,我似乎仍然得到 Test suite failed to run

ReferenceError: regeneratorRuntime is not defined

这是我的 package.json
“开发依赖”:{
"@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",
“排毒”:“^16.5.1”,

.babelrc
每当我在这个文件上添加一些东西时,我的应用程序文件都不会运行。 我必须删除这个文件才能让我的反应式本机应用程序运行

遇到同样的问题。 新鲜的jest (26.6.3),普通的异步测试和这个..
对于一个将自己定义为“令人愉快”的框架来说,这确实不是。
为什么我需要关心 babel 配置、插件等。
为什么它不能开箱即用?

更新。 在我的情况下,我已经有 babel.config.js 但丢失了
targets: { node: 'current' }

此页面是否有帮助?
0 / 5 - 0 等级

相关问题

kentor picture kentor  ·  3评论

samzhang111 picture samzhang111  ·  3评论

Antho2407 picture Antho2407  ·  3评论

Secretmapper picture Secretmapper  ·  3评论

stephenlautier picture stephenlautier  ·  3评论