Search code examples
typescriptcreate-react-appenzyme

How to add enzyme to typescript project


I downloaded a typescript react app project like this

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

I am trying to add enzyme to it like this

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

Afterwards the package.json file look like this

  "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"
  }
}

but for some reason I cannot import it into a test file. I get this message TS7016: Could not find a declaration file for module 'enzyme'. This is the line that is not resolving

import { shallow, configure } from 'enzyme';

I see enzyme under node_modules. What am I doing wrong?


Solution

  • You should run the following commands to install enzyme types:

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