基本的Jest单元测试中的错误-意外的标识符| _Object $ defineProperty | babel运行时

时间:2019-07-26 11:37:03

标签: reactjs typescript jestjs babel-jest

我正在使用酶和Jest编写一个简单的单元测试。 但是我对基本的初始测试有疑问。 正如您在屏幕上看到的那样,

失败的人只是“没有崩溃的回报”测试。

enter image description here

我尝试使用:

  • 排除 ”:tsconfig中的“ / node_modules /”
  • testPathIgnorePatterns | transformIgnorePatterns ”:jest.config中的“ / node_modules /”
  • 反应脚本测试--env = jsdom 就像建议的here

但它不起作用。

请帮助!

我的package.json

{
 "name": "my-app",
 "version": "0.12.0",
 "private": true,
 "dependencies": {
   "@types/jest": "23.3.13",
   "@types/lodash": "^4.14.120",
   "@types/node": "10.12.18",
   "@types/react": "16.7.20",
   "@types/react-dom": "16.0.11",
   "@types/react-helmet": "^5.0.8",
   "@types/react-redux": "^7.0.1",
   "@types/react-slick": "^0.23.3",
   "@types/styled-components": "^4.1.12",
   "axios": "^0.18.0",
   "babel-plugin-transform-runtime": "^6.23.0",
   "clipboard-polyfill": "^2.7.0",
   "core-js": "^2.6.5",
   "fscreen": "^1.0.2",
   "i18next": "^14.0.1",
   "moment": "^2.24.0",
   "normalizr": "^3.3.0",
   "polished": "^3.0.0",
   "react": "^16.7.0",
   "react-app-polyfill": "^0.2.2",
   "react-cookie": "^3.0.8",
   "react-dom": "^16.7.0",
   "react-dropzone": "^8.2.0",
   "react-helmet": "^5.2.0",
   "react-i18next": "^9.0.8",
   "react-media": "^1.9.2",
   "react-pose": "^4.0.8",
   "react-redux": "^6.0.0",
   "react-router-dom": "^4.3.1",
   "react-scripts": "^2.1.8",
   "react-slick": "^0.24.0",
   "redux": "^4.0.1",
   "redux-devtools-extension": "^2.13.7",
   "redux-thunk": "^2.3.0",
   "reselect": "^4.0.0",
   "styled-components": "^4.1.3",
   "tech-vnc": "<url-to->/tech-vnc.git",
   "tech-websocket-renderer": "<url-to->/tech-websocket-renderer.git",
   "testbirds-styleguide": "<url-to->/testbirds-styleguide.git",
   "typescript": "3.2.4",
   "url-search-params-polyfill": "^5.0.0"
 },
 "scripts": {
   "test": "react-scripts test --env=jsdom",
   "start": "react-scripts start",
   "prebuild": "npm run import-translations",
   "build": "react-scripts build",
   "build:nest": "env-cmd .env.nest npm run build",
   "import-translations": "node ./scripts/translations.js",
   "eject": "react-scripts eject"
 },
 "eslintConfig": {
   "extends": "react-app"
 },
 "browserslist": [
   ">0.2%",
   "not dead",
   "not ie <= 11",
   "not op_mini all"
 ],
 "devDependencies": {
   "@types/react-router-dom": "^4.3.1",
   "cross-fetch": "^3.0.0",
   "env-cmd": "^8.0.2",
   "enzyme": "^3.10.0",
   "enzyme-adapter-react-16": "^1.14.0",
   "fs-extra": "^7.0.1",
   "moxios": "^0.4.0"
  },
  "files": [
    "build"
   ]
 }

我的tsconfig.json

enter image description here

1 个答案:

答案 0 :(得分:0)

从Enzyme 3开始,您将需要安装Enzyme以及与您所使用的React版本相对应的Adapter。

还需要在您的global setup file中配置适配器:

        import { configure } from 'enzyme';
        import Adapter from 'enzyme-adapter-react-16';

        configure({ adapter: new Adapter() });

要获取更多帮助,请检查here

并确保已将node_modules添加到排除tsconfig.json中。

在我的情况下,我添加了以下内容,

    "exclude": [
        "node_modules",
        "build",
        "scripts",
        "acceptance-tests",
        "webpack",
        "jest"
    ],

例如检查here

希望这会对您有所帮助。