【问题标题】:create react app error - Cannot find name 'describe'创建反应应用程序错误 - 找不到名称“描述”
【发布时间】:2020-02-24 14:28:46
【问题描述】:

我在 Jest 中使用 create react app,但出现以下错误:

Cannot find name 'describe'.
Do you need to install type definitions for a test runner?
Try `npm i @types/jest`
or `npm i @types/mocha`
and then add `jest` or `mocha` to the types field in your tsconfig.

我看到了this question,但我无法按照建议删除 types 属性,所以它对我没有帮助。

还尝试将所有内容放在依赖项中...如果我运行 npm test,测试正在运行,但我的 IDE 中出现很多错误。

我的tsconfig.json

{
  "compilerOptions": {
    "experimentalDecorators": true,
    "target": "es5",
    "lib": [
      "dom",
      "dom.iterable",
      "esnext"
    ],
    "allowJs": true,
    "types": ["reflect-metadata","node"],
    "skipLibCheck": true,
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true,
    "strict": true,
    "forceConsistentCasingInFileNames": true,
    "module": "esnext",
    "moduleResolution": "node",
    "resolveJsonModule": true,
    "strictPropertyInitialization": false,
    "isolatedModules": true,
    "noEmit": true,
    "jsx": "react",
    "emitDecoratorMetadata": true   
  },
  "include": [
    "src",
    "node_modules/@types/jest"
  ]
}

我的package.json

{
  "name": "my-app",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "@material-ui/core": "^4.9.2",
    "@testing-library/jest-dom": "^4.2.4",
    "@testing-library/react": "^9.3.2",
    "@testing-library/user-event": "^7.1.2",
    "@types/node": "^12.12.27",
    "@types/react": "^16.9.19",
    "@types/react-dom": "^16.9.0",
    "@types/react-redux": "^7.1.7",
    "@types/react-router": "^5.1.4",
    "@types/redux-logger": "^3.0.7",
    "add": "^2.0.6",
    "axios": "^0.19.2",
    "class-transformer": "^0.2.3",
    "class-validator": "^0.11.0",
    "connected-react-router": "^6.7.0",
    "history": "^4.10.1",
    "immer": "^5.3.4",
    "inversify": "^5.0.1",
    "inversify-react": "^0.4.3",
    "log4js": "^6.1.2",
    "loglevel": "^1.6.7",
    "loglevel-plugin-prefix": "^0.8.4",
    "react": "^16.12.0",
    "react-dom": "^16.12.0",
    "react-intl": "^3.12.0",
    "react-intl-redux": "^2.2.0",
    "react-redux": "^7.1.3",
    "react-router": "^5.1.2",
    "react-scripts": "3.3.1",
    "redux": "^4.0.5",
    "redux-devtools-extension": "^2.13.8",
    "redux-logger": "^3.0.6",
    "redux-thunk": "^2.3.0",
    "reflect-metadata": "^0.1.13",
    "typescript": "~3.7.5"
  },
  "devDependencies": {
    "@craco/craco": "^5.6.3",
    "@types/jest": "^24.0.0",
    "@types/react-intl-redux": "^0.1.14",
    "@types/redux-mock-store": "^1.0.2",
    "@typescript-eslint/eslint-plugin": "^2.19.0",
    "@typescript-eslint/parser": "^2.19.0",
    "cross-env": "^7.0.0",
    "eslint": "^6.8.0",
    "eslint-config-airbnb": "^18.0.1",
    "eslint-config-prettier": "^6.10.0",
    "eslint-plugin-import": "^2.20.1",
    "eslint-plugin-jest": "^23.7.0",
    "eslint-plugin-jsx-a11y": "^6.2.3",
    "eslint-plugin-prettier": "^3.1.2",
    "eslint-plugin-react": "^7.18.3",
    "eslint-plugin-react-hooks": "^2.3.0",
    "husky": "^4.2.1",
    "lint-staged": "^10.0.7",
    "prettier": "^1.19.1",
    "redux-mock-store": "^1.5.4"
  },

【问题讨论】:

  • 运行 npm i --save-dev ts-jest 应该可以解决您的问题
  • 还是知道了,还尝试将 evreyting 放在依赖项中...如果我运行 npm test 测试正在运行,但我在 ide 中有很多错误

标签: reactjs typescript tsconfig react-testing-library


【解决方案1】:
then add `jest` or `mocha` to the types field in your tsconfig.

您在运行测试时收到的错误消息建议将 jest 添加到您的 types 字段中,但您只有以下内容:

"types": ["reflect-metadata","node"],

改成有

"types": ["reflect-metadata","node", "jest"],

【讨论】:

    猜你喜欢
    • 2020-08-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-11
    • 1970-01-01
    • 2021-08-20
    相关资源
    最近更新 更多