Cannot find name 'describe'. Do you need to install type definitions for a test runner?

Ronin picture Ronin · Jan 11, 2019 · Viewed 46.1k times · Source

When using TypeScript in conjunction with Jest, my specs would fail with error messages like:

test/unit/some.spec.ts:1:1 - error TS2582: 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`.
test/unit/some.spec.ts:2:3 - error TS2582: Cannot find name 'it'. Do you need to install type definitions for a test runner? Try `npm i @types/jest` or `npm i @types/mocha`.
test/unit/some.spec.ts:3:7 - error TS2304: Cannot find name 'expect'.
test/unit/some.spec.ts:7:1 - error TS2582: Cannot find name 'test'. Do you need to install type definitions for a test runner? Try `npm i @types/jest` or `npm i @types/mocha`.

The types are already installed.

I use:

    "@types/jest": "^23.3.12",
    "jest": "^23.6.0",
    "ts-jest": "^23.10.5",
    "typescript": "^3.1.6"

I run tests using jest --forceExit --coverage --verbose

Answer

Melvin Sy picture Melvin Sy · Feb 11, 2019

I'm using VSCode as my IDE and in my Angular project, I had to comment-out/remove types in tsconfig.json and add jest in types at tsconfig.spec.json.

tsconfig.json

{
  "compilerOptions": {
    // "types": []
  }
}

tsconfig.spec.json

{
  "compilerOptions": {
    "types": ["jest", "node"]
  }
}