Fix - Cannot find name 'describe' Error in TypeScript

avatar

Borislav Hadzhiev

Last updated: Mar 13, 2022

banner

Check out my new book

Fix - Cannot find name 'describe' Error in TypeScript #

To solve the "Cannot find name 'describe'" error, install the type definitions for your test runner, e.g. npm i -D @types/jest or npm i -D @types/jasmine and make sure to add the typings for the package in the types array in your tsconfig.json file.

cannot find name describe

Here is an example of how the error occurs.

index.ts
// ⛔️ Error: Cannot find name 'describe'. Do you // need to install type definitions for a test runner? // Try `npm i --save-dev @types/jest` or // `npm i --save-dev @types/mocha` and then // add 'jest' or 'mocha' to the types field in your tsconfig.ts(2593) describe('example', () => { it('does work', () => { expect(2).toBe(2); }); });

Open your terminal in the root directory of your project and install the typings for your test runner. Here are the comments for jest, mocha and jasmine.

shell
# 👇️ if you use jest npm i -D @types/jest ts-jest jest # 👇️ if you use mocha npm i -D @types/mocha mocha # 👇️ if you use jasmine npm i -D @types/jasmine jasmine

Once the typings are installed, you have to add them to the types array in your tsconfig.json file.

This should be what your types array looks like if you use the jest test runner.

tsconfig.json
{ "compilerOptions": { "types": [ "reflect-metadata", "jest", // ... your other types ], // ... your other options } }

And this is what your types array should look like if you use jasmine.

tsconfig.json
{ "compilerOptions": { "types": [ "jasmine", // ... your other types ], // ... your other options } }

If you are still getting the error after adding the typings with jasmine, try adding an import at the beginning of your test file:

index.ts
import 'jasmine';

And this is what your types array should look like if you use mocha.

tsconfig.json
{ "compilerOptions": { "types": [ "mocha", // ... your other types ], // ... your other options } }

If the error is still not resolved, make sure that TypeScript is picking up the directory in which your tests are located. If you've set the include array in your tsconfig.json file, it should also include the directory in which your tests are.

For example, if your tests are in an src directory, the following config is fine:

tsconfig.json
{ "compilerOptions": {}, "include": ["src/**/*"], }

However, if your tests are in a tests directory next to your src directory, TypeScript won't pick them up with the config above.

tsconfig.json
{ "compilerOptions": {}, "include": [ "src/**/*", "tests/**/*" ], }

You can also use glob patterns. Here is an example that includes files ending in .spec.ts and .test.ts.

tsconfig.json
{ "compilerOptions": {}, "include": [ "src/**/*", "**/*.spec.ts", "**/*.test.ts" ], }

The example above makes sure to also include your test files in your project. If you need a way to exclude your test files from compilation, but still have them type checked, check out my other article - Exclude test files from Compilation in TypeScript.

If the error persists and your runtime is Node.js, make sure to install the typings for node, by opening your terminal in your project's root directory and running the following command:

shell
npm i -D @types/node

Also if running your code using Node.js, make sure the types array in your tsconfig.json file contains "node".

tsconfig.json
{ "compilerOptions": { "types": [ // ... your other types "node" ], }, }

If the error is not resolved, try to delete your node_modules and package-lock.json files, re-run npm install and restart your IDE.

shell
rm -rf node_modules package-lock.json npm install

Make sure to restart your IDE if the error still persists. VSCode glitches often and a reboot solves things sometimes.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.