1

I am using create react app with Jest and I am getting the following error:

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.

I saw this question, but I could not remove the types property as suggested, so it did not help for me.

Also tried to put everything in dependencies... if I run npm test, the test is running but I get a lot of errors in my IDE.

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

My 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"
  },
Joshua Wade
  • 4,755
  • 2
  • 24
  • 44
Tuz
  • 1,810
  • 5
  • 29
  • 58
  • Running `npm i --save-dev ts-jest` should solve your issue – r3dst0rm Feb 24 '20 at 14:39
  • stil got it , also tried to put evreyting in dependies ... if i run npm test the test is running but i got lot of erros in the ide – Tuz Feb 24 '20 at 14:52

2 Answers2

1
then add `jest` or `mocha` to the types field in your tsconfig.

The error message you got from running the test suggests adding jest to your types field but you only have the following:

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

Change it to have

"types": ["reflect-metadata","node", "jest"],
kmui2
  • 2,227
  • 18
  • 19
0

I guess it's a general problem for a Javascript project. You can try to find solutions in cannot-find-name-describe:

And another problem, I had but not mentioned in the reference question, is that you don't open your project in the root directory with VS Code.

The solution is:

  • open the project with its' root directory.
oneWalker
  • 46
  • 8