Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 256
Ronin Avatar asked Jan 11 '19 01:01

Ronin


5 Answers

I'm using Visual Studio Code as my IDE and in my Angular project, and I had to comment-out/remove types in file tsconfig.json and add "jest" in types in tsconfig.spec.json.

File tsconfig.json

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

File tsconfig.spec.json

{
  "compilerOptions": {
    "types": ["jest", "node"]
  }
}
like image 179
Melvin Sy Avatar answered Nov 14 '22 22:11

Melvin Sy


It's a bit tricky one because both: your IDE (i.e., Visual Studio Code) and TypeScript use tsconfig.json for their own purposes.

Simple checklist to solve the initial problem:

(for TypeScript and Jest)

  1. Make sure you have @types/jest and @types/node installed.
  2. Make sure you have linked these types in tsconfig.json so that: "types": ["jest", "node"]
  3. Make sure you don't have your tests or the tests directory excluded from tsconfig.json configuration in excluded property.

Side effect on transpilation

If you transpile from TypeScript to JavaScript using tsc or any custom module that relies on tsconfig.json then you may notice that your tests will be transpiled too in such case (you'll see their .js correspondence in your build directory).

However, in most cases you will have either:

  1. separate tsconfig.prod.json with a configuration that overwrites the default one. There are many settings like inlineSource, sourceMaps, inlineSourceMaps which you'd probably want to disable too and then use tsc --project tsconfig.prod.json to build
  2. terminal (npm/yarn script) command that overwrites the default configuration with the specific flags. Example: npx tsc --inlineSourceMap false --declarationMap false --inlineSources false --sourceMap false. At that point you can use --excludeFiles or --excludeDirectories flag to exclude your tests from the build as per documentation.

The alternative is to use package like rimraf and delete unnecessary files as a part of the build process. It might be less complex than overwriting the configuration and easier to maintain as a build step. In such a case, you may use the command: yarn rimraf build/**/*.test.js.

like image 115
Greg Wozniak Avatar answered Nov 15 '22 00:11

Greg Wozniak


This worked for me:

import '@types/jest';
like image 55
Krzysztof Hamerski Avatar answered Nov 14 '22 22:11

Krzysztof Hamerski


None of the previous answers fixed my issue.

I had to add "@types/jest" to the types array in the tsconfig.json file.

like image 51
Stevo Avatar answered Nov 14 '22 23:11

Stevo


The only way I was able to fix this was by adding the tests/ folder to "include" in the tsconfig.json file:

"include": [
  "src/**/*.ts",
  "tests/*.ts"
] 

For those who also have ESLint complaining, you need to add Jest to your .eslintrc.json file:

"env": {
  "es2020": true,
  "node": true,
  "jest": true
}
like image 31
Jels Boulangier Avatar answered Nov 14 '22 23:11

Jels Boulangier