How to Fix the “Cannot find name ‘describe’” Error in TypeScript

Learn how to easily fix the “cannot find name ‘describe’ error” in TypeScript.

Tari Ibaba
4 min readSep 26, 2022

To fix the “cannot find name ‘describe’” error, install the type definitions for your testing framework, and then add the definitions to the types array in your tsconfig.json file.

This error occurs when you try to use the describe() function in a TypeScript file, but TypeScript cannot find the type definitions for the package.

index.ts

/* Cannot find name 'it'. 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`. ts(2582) */
describe('example', () => {
it('adds two numbers together', () => {
expect(2 + 2).toBe(4);
});
});

Install the type definitions for the testing framework you’re using by running one of the following commands at the root of your project directory.

# 👇 Jest
npm i -D @types/jest jest
# 👇 Mocha
npm i -D @types/mocha mocha
# Yarn
# 👇 Jest
yarn add --dev @types/jest jest
# 👇 Mocha
yarn add --dev @types/mocha mocha

Add typings to types array in tsconfig.json

In some cases, this is all you need to fix the error. But if it persists, you might need to add the newly installed typings to the types array of your tsconfig.json file.

So if you’re using Jest, you’ll add a jest string to the types array, and then your tsconfig.json file will look something like this:

tsconfig.json

{
"compilerOptions": {
"types": [
// ... other types
"jest"
]
// ..other settings
}
}

Similarly, if you’re using Mocha, you’ll add a mocha string to the types array.

tsconfig.json

{
"compilerOptions": {
"types": [
// ... other types
"mocha"
]
// ..other settings
}
}

Include test files

If the error still doesn’t go away after doing this, ensure that TypeScript is not ignoring the directory containing your test files. If you’ve set the include array in your tsconfig.json file, ensure the patterns specified in this array match the directory where your test files are located.

For example, if your test files are located in a src directory, TypeScript will detect them with a configuration like this:

tsconfig.json

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

But if they’re located in a tests directory, we'll need to add an additional glob pattern to make TypeScript detect them:

tsconfig.json

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

We can also include glob patterns to match files with a specific ending or extension. For example, we can include all files ending with .spec.ts and .test.ts with the following configuration:

tsconfig.json

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

If you’ve set the exclude array in your tsconfig.json file, make sure that none of the patterns in the array match the directory containing the test files, as this will prevent TypeScript from detecting them.

For example, in the tsconfig.json file below, files ending with .spec.ts have been excluded, so TypeScript will ignore them and the error will occur when you attempt to use describe() in them.

tsconfig.json

{
"compilerOptions": {},
"include": [
"src/**/*"
],
"exclude": [
"**/*.test.ts"
],
}

We’ll fix this issue by simply moving the pattern string from exclude to include:

tsconfig.json

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

Import module in file

Instead of adding the typings to the types array, you can also import them at the top of the file where you are using describe().

So if we’re using Jest, we’ll add an import 'jest' line at the beginning of the file.

index.ts

// 👇
import 'jest';
// ✅ No error
describe('example', () => {
it('adds two numbers together', () => {
expect(2 + 2).toBe(4);
});
});

Similarly, if we’re using Mocha, we’ll add an import 'mocha' line:

index.ts

// 👇
import 'mocha';
// ✅ No error
describe('example', () => {
it('adds two numbers together', () => {
expect(2 + 2).toBe(4);
});
});

Restart IDE

If the error persists after doing all of this, restarting your IDE might help.

Originally published at codingbeautydev.com

11 Amazing New JavaScript Features in ES13

This guide will bring you up to speed with all the latest features added in ECMAScript 13. These powerful new features will modernize your JavaScript with shorter and more expressive code.

Sign up and receive a free copy immediately.

--

--