当前位置: 首页 > 知识库问答 >
问题:

`运行Jest测试时未定义regeneratorRuntime`

桓瀚
2023-03-14

这个标题很大程度上解释了我面临的问题。我试图测试一个具有某种状态的React组件,并试图将我的存储提供给该组件以获取它需要的内容。当我使用Jest运行组件的测试时,我得到以下错误:

ReferenceError:未定义regeneratorRuntime

通过一些阅读,我确定这是由babel polyfill再生器运行时未正确应用于Jest造成的。但是,我已经尝试安装这两个软件包并重新运行,结果没有变化。在阅读Jest Github问题页面(删除babel polyfill#2755的自动包含)后,我发现自版本19起,babel polyfill不会被Jest自动包含。我手动安装的那个软件包应该已经解决了这个问题,但它没有。我已经包括了一些我认为相关的文件

.babelrc

{
  "presets": ["es2015", "react", "stage-2"],
  "env": {
    "development": {
      "plugins": [
        ["react-transform", {
          "transforms": [{
            "transform": "react-transform-hmr",
            "imports": ["react"],
            "locals": ["module"]
          }]
        }]
      ]
    }
  }
}

jest.config

{
    "transform": {
        "^.+\\.(js|jsx)$": "<rootDir>/node_modules/webpack-babel-jest",
        ".*\\.(vue)$": "<rootDir>/node_modules/jest-vue-preprocessor",
        ".*": "babel-jest"
    },
    "moduleNameMapper": {
        "\\.(jpg|jpeg|css|scss|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$": "<rootDir>/__tests__/__mocks__/fileMock.js",
        ".*\\.(vue)$": "vue/dist/vue.js"
    },
    "testPathIgnorePatterns": ["type_parser.spec.js", 
                               "<rootDir>/__tests__/__mocks__/",
                               "__tests__/unit/core/util/type_parser.js",
                               "__tests__/GitlabLoader.test.js"
                               ]
}

package.json

{
  "name": "my_project",
  "version": "0.2.0",
  "description": "My Project",
  "scripts": {
    "clean:build": "node ./bin/clean.js createdir",
    "build:html": "node ./bin/buildHtml.js",
    "deployProduction": "node ./bin/deployProduction.js",
    "start": "webpack-dev-server --config ./config/webpack.config.dev.js --hot --inline --progress",
    "serve": "npm run deployProduction&& supervisor --watch ./production-copy src/js/server",
    "prebuild": "npm run clean:build",
    "postbuild": "node ./bin/postBuild.js",
    "rebuild-win": "set BUILD_TYPE=preview& npm run prebuild & npm run build-win & npm run serve",
    "build": "set BUILD_TYPE=final& npm run prebuild & npm run build-win",
    "deploy": "npm run build & npm run serve",
    "build-win": "set NODE_ENV=production & npm run element-build & npm run build-doc & npm run build:html &  webpack -p --config ./config/webpack.config.prod.js --json > webpack.log.json & npm run postbuild",
    "lint": "eslint config src/js/**/*.js",
    "jscs": "jscs src/js/",
    "test": "jest --no-cache --verbose --config=./__tests__/jest.config",
    "test:watch": "npm run test -- --watch",
    "element-init": "node node_modules/element-theme/bin/element-theme -i src/js/core/ui/element-theme.css",
    "element-build": "node node_modules/element-theme/bin/element-theme -c src/js/core/ui/element-theme.css -o src/js/core/ui/element-theme ",
    "build-doc": "node bin/buildDoc.js ",
    "storybook": "start-storybook -p 9001 -c .storybook"
  },
  "repository": {
    "type": "git",
    "url": "my_url"
  },
  "license": "MIT",
  "bugs": {
    "url": "my_url"
  },
  "homepage": "my_homepage",
  "dependencies": {
    "autoprefixer": "^6.3.6",
    "axios": "^0.11.1",
    "babel-runtime": "^6.23.0",
    "babel-standalone": "^6.10.3",
    "bluebird": "^3.4.0",
    "brace": "^0.8.0",
    "browserify": "^13.0.1",
    "chai": "^3.5.0",
    "classnames": "2.2.3",
    "cls-bluebird": "^1.0.1",
    "codemirror": "^5.16.0",
    "connect-history-api-fallback": "^1.3.0",
    "continuation-local-storage": "^3.1.7",
    "dateformat": "^1.0.12",
    "diff": "^3.0.1",
    "element-theme": "^0.4.0",
    "element-ui": "^1.1.5",
    "express-history-api-fallback": "^2.0.0",
    "filedrop": "^2.0.0",
    "fs-extra": "^0.30.0",
    "history": "^2.0.2",
    "humps": "^1.0.0",
    "immutability-helper": "^2.1.1",
    "isomorphic-fetch": "^2.2.1",
    "json-loader": "^0.5.4",
    "jszip": "^3.0.0",
    "jszip-utils": "0.0.2",
    "material-ui": "^0.16.7",
    "materialize-css": "^0.97.6",
    "mocha": "^2.5.3",
    "moment": "^2.17.1",
    "normalizr": "^1.0.0",
    "raven-js": "^3.9.1",
    "react": "^15.0.1",
    "react-ace": "^3.5.0",
    "react-addons-update": "^15.4.2",
    "react-dom": "^15.0.1",
    "react-redux": "^4.4.5",
    "react-router": "^2.3.0",
    "react-router-redux": "^4.0.2",
    "redux": "^3.4.0",
    "redux-logger": "^2.6.1",
    "redux-saga": "^0.9.5",
    "request": "^2.72.0",
    "request-promise": "^3.0.0",
    "reselect": "^2.5.4",
    "save-as": "^0.1.7",
    "showdown": "^1.4.2",
    "three": "^0.79.0",
    "url-pattern": "^1.0.3",
    "vue": "^2.0.5",
    "vue-easy-slider": "^1.4.0",
    "vue-loader": "^9.8.1",
    "vue-router": "^2.0.1",
    "vue-slider-component": "^2.0.4",
    "walk": "^2.3.9"
  },
  "devDependencies": {
    "@kadira/storybook": "^2.35.3",
    "babel-core": "^6.7.6",
    "babel-eslint": "^6.1.0",
    "babel-jest": "^18.0.0",
    "babel-loader": "^6.0.2",
    "babel-plugin-react-transform": "^2.0.2",
    "babel-polyfill": "^6.23.0",
    "babel-preset-es2015": "^6.22.0",
    "babel-preset-es2016": "^6.22.0",
    "babel-preset-react": "^6.23.0",
    "babel-preset-stage-2": "^6.5.0",
    "babel-register": "^6.7.2",
    "chai": "3.5.0",
    "chai-jquery": "2.0.0",
    "cheerio": "0.20.0",
    "colors": "1.1.2",
    "concurrently": "^2.0.0",
    "copy-webpack-plugin": "2.1.1",
    "css-loader": "0.23.1",
    "element-theme-default": "^1.1.5",
    "enzyme": "^2.7.1",
    "eslint": "^2.13.1",
    "eslint-config-airbnb": "^9.0.1",
    "eslint-plugin-import": "^2.2.0",
    "eslint-plugin-jsx-a11y": "^4.0.0",
    "eslint-plugin-react": "^5.2.2",
    "express": "^4.13.4",
    "extract-text-webpack-plugin": "1.0.1",
    "file-loader": "0.8.5",
    "identity-obj-proxy": "^3.0.0",
    "jest": "^19.0.2",
    "jest-cli": "^18.1.0",
    "jest-css-modules": "^1.1.0",
    "jest-enzyme": "^2.1.2",
    "jest-vue-preprocessor": "^0.1.2",
    "jquery": "2.2.3",
    "jscs": "3.0.3",
    "jsdoc-to-markdown": "^2.0.0",
    "jsdom": "8.4.0",
    "json-loader": "^0.5.4",
    "mocha": "^2.4.5",
    "ncp": "^2.0.0",
    "node-sass": "3.7.0",
    "postcss-loader": "0.8.2",
    "react-addons-test-utils": "^15.4.2",
    "react-hot-loader": "1.3.0",
    "react-test-renderer": "^15.4.2",
    "react-transform-hmr": "^1.0.4",
    "redux-devtools": "^3.3.1",
    "redux-devtools-dock-monitor": "^1.1.1",
    "redux-devtools-log-monitor": "^1.0.11",
    "regenerator-runtime": "^0.10.3",
    "remotedev": "^0.1.2",
    "rimraf": "^2.5.2",
    "sass-loader": "3.2.0",
    "storybook-addon-material-ui": "^0.7.6",
    "style-loader": "0.13.1",
    "url-loader": "0.5.7",
    "vueify": "^9.4.0",
    "webpack": "^1.13.0",
    "webpack-babel-jest": "^1.0.4",
    "webpack-dev-middleware": "^1.6.1",
    "webpack-dev-server": "^1.16.3",
    "webpack-hot-middleware": "^2.10.0"
  }
}

共有3个答案

柯建修
2023-03-14

我使用了导入“babel polyfill”解决了我的问题。

2020年更新:此答案已过时,babel polyfill已被弃用。查看文档和Jero的答案。

酆英达
2023-03-14

在将babel polyfill导入到每个已工作的测试中时,更新的jest文档(v24)建议在babel配置中设置此选项。如果执行此操作,则不再需要导入babel polyfill。(就我个人而言,我的预设环境配置中缺少了目标选项)。

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

一句至理名言:指定目标将覆盖您定义的任何浏览器列表配置(否则巴贝尔会使用)。因此,在web项目中,您可能需要创建一个动态的babel.config.js,该动态的babel.config.js具有“jest-aware”特性,以便在测试环境中只添加目标。请参阅jest文档中标题为“让您的Babel配置了解jest”的部分。

宋航
2023-03-14

如果您使用的是setupTests.js文件,则可以从该文件导入再生器运行时:

// setupTests.js

import 'regenerator-runtime/runtime'
import Enzyme from 'enzyme'
import EnzymeAdapter from 'enzyme-adapter-react-16'

Enzyme.configure({
  adapter: new EnzymeAdapter()
})

// package.json

{
  ...
  "dependencies": {
    ...
  },
  "devDependencies": {
    ...
  },
  "jest": {
    "setupFilesAfterEnv": ["./pathToYour/setupTests.js"] 
  }
}

$ yarn add regenerator-runtime --dev
 类似资料:
  • 问题内容: 标题几乎可以解释我所面临的问题。我正在尝试测试具有某种状态的组件,并且试图为该组件提供存储以获取所需的组件。当我使用Jest运行组件的测试时,出现以下错误: 我已经通过了一些读数确定,这是造成或者没有被正确适用于玩笑。但是,我尝试安装这两个软件包并重新运行,但结果没有变化。阅读Jest Github问题页面(删除babel- polyfill的自动包含#2755 )后,我发现Jest从

  • 我得到以下错误时运行我的测试用例摩卡 有人知道为什么会这样吗? 包裹json physicalperson.spec.js 我所尝试的 我在SO中看到了一些线程,我也尝试了下面的代码,但出现了相同的错误。

  • 我正在通过运行Jest测试。默认情况下,Jest并行运行测试。有没有办法让测试按顺序运行? 我有一些测试调用依赖于更改当前工作目录的第三方代码。

  • 我想用Jest做一个测试。 我使用

  • 问题内容: 我试图了解如何开发独立的Javascript代码。我想用命令行运行测试和模块编写Javscript代码。所以,我已经安装,并伴随着图书馆,和。 我的目录结构如下所示: 我在用下面的代码编写的一个小模块在哪里: 并且是测试: 然后,我尝试从顶级目录运行(因此看到该目录): 所以我的问题是: 这是构造代码的正确方法吗? 为什么我的测试无法运行? 学习这种东西的最好方法是什么?我很难在Goo

  • 问题内容: 我在Eclipse中创建了一个新的maven项目,该项目通过使用HtmlUnitDriver和FirefoxDriver来调出一个网页并检查页面标题来运行简单测试。当我运行maven命令mvn test时,这就是我得到的一切: 当我在Eclipse中运行相同的Maven命令时,它显示以下内容: 这是我的pom.xml,供那些想要检查是否可能是问题的人使用: 问题答案: 您是说要添加为P