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 happens if you try to use the describe()
function in a TypeScript file, but type definitions for the package are missing.
/* 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:
{
"compilerOptions": {
"types": [
// ... other types
"jest"
]
// ..other settings
}
}
If you’re using Mocha too, you’ll add a mocha
string to the types
array.
{
"compilerOptions": {
"types": [
// ... other types
"mocha"
]
// ..other settings
}
}
Include test files
If the error still doesn’t go away after doing this, make sure that TypeScript is not ignoring the directory containing your test files.
If you’ve set the include
array in your tsconfig.json
file, make sure 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:
{
"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:
{
"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 this config:
{
"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.
{
"compilerOptions": {},
"include": [
"src/**/*"
],
"exclude": [
"**/*.test.ts"
],
}
We’ll fix this issue by simply moving the pattern string from exclude
to include
:
{
"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.
// 👇
import 'jest';
// ✅ No error
describe('example', () => {
it('adds two numbers together', () => {
expect(2 + 2).toBe(4);
});
});
If we’re also using Mocha, we’ll add an import 'mocha'
line:
// 👇
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.
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.