如何将酶添加到打字稿项目

How to add enzyme to typescript project

我下载了一个像这样的 typescript React 应用程序项目

yarn create react-app react-app-ts --template typescript

我正在尝试像这样向其中添加酶

yarn add enzyme enzyme-adapter-react-16 --dev

之后 package.json 文件看起来像这样

  "name": "react-app-ts",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "@testing-library/jest-dom": "^5.11.4",
    "@testing-library/react": "^11.1.0",
    "@testing-library/user-event": "^12.1.10",
    "@types/jest": "^26.0.15",
    "@types/node": "^12.0.0",
    "@types/react": "^17.0.0",
    "@types/react-dom": "^17.0.0",
    "react": "^17.0.2",
    "react-dom": "^17.0.2",
    "react-scripts": "4.0.3",
    "typescript": "^4.1.2",
    "web-vitals": "^1.0.1"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },
  "eslintConfig": {
    "extends": [
      "react-app",
      "react-app/jest"
    ]
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  },
  "devDependencies": {
    "enzyme": "^3.11.0",
    "enzyme-adapter-react-16": "^1.15.6"
  }
}

但出于某种原因我无法将其导入测试文件。我收到此消息 TS7016: Could not find a declaration file for module 'enzyme'。这是未解析的行

import { shallow, configure } from 'enzyme';

我在 node_modules 下看到酶。我做错了什么?

您应该运行以下命令来安装酶类型:

  • yarn add @types/enzyme
  • yarn add @types/enzyme-adapter-react-16