TypeScript does not know describe, it or expect in my Jest tests
Cannot find name 'it'. Do you need to install type definitions for a test runner?Short answer
TypeScript only knows describe, it and expect when Jest's types are loaded. Either install @types/jest and make sure your tsconfig does not hide it with a types list, or stop using globals and import describe, it and expect from @jest/globals in every test file.
Fix it fast
1Find out which cause you have
Check whether the Jest typings are installed in the project at all.
npm ls @types/jest @jest/globals2Match what you see, then jump to its fix
- 1
TS2582: Cannot find name 'it'. Do you need to install type definitions for a test runner?No Jest typings are installedFix - 2
TS2593: the same message, and it tells you to add 'jest' to the types field of tsconfigtsconfig.json has a types list that leaves jest outFix - 3
The project uses Jest's own types, or two test runners are installed and their globals clashYou prefer typed imports instead of globalsFix
- 1
What does the error look like?
Cannot find name 'it'. Do you need to install type definitions for a test runner?Cannot find name 'describe'. Do you need to install type definitions for a test runner?Cannot find name 'expect'.
Cause 1 of 3
No Jest typings are installed
1Check
The Jest globals get their types from a separate package, so a project with only jest and ts-jest installed may have none. The command in the first step prints (empty) for both packages.
npm ls @types/jest @jest/globals2Fix
Install @types/jest, which types the globals with no imports needed. Keep its version close to your Jest version, because it is maintained by a third party.
npm install --save-dev @types/jest3Verify
Type-check the project without running the tests.
npx tsc --noEmitConfirmed by raw.githubusercontent.com
Cause 2 of 3
tsconfig.json has a types list that leaves jest out
1Check
Look for a types array under compilerOptions. When types is set, only the packages listed in it are included in the global scope, so jest must be in the list. From TypeScript 6.0 the default for types is an empty list, so older configs that relied on 'all visible @types' can break after an upgrade.
grep -n '"types"' tsconfig.json2Fix
Add jest to the list (and node if you use Node.js globals). If the app and the tests share one tsconfig, this is the simplest fix.
{
"compilerOptions": {
"types": ["node", "jest"]
}
}3Verify
Run the type check again.
npx tsc --noEmitConfirmed by typescriptlang.org
Cause 3 of 3
You prefer typed imports instead of globals
1Check
Jest ships its own type definitions in @jest/globals, and they are updated whenever Jest is. The package needs no tsconfig changes, but every test file has to import what it uses.
grep -rL "@jest/globals" src --include=*.test.ts2Fix
Install @jest/globals and import the functions in each test file.
npm install --save-dev @jest/globals
// sum.test.ts
import { describe, expect, it } from '@jest/globals';3Verify
Type-check and run Jest.
npx tsc --noEmit
npx jestConfirmed by raw.githubusercontent.com
How do I stop it coming back?
- Commit the Jest typings choice (@types/jest or @jest/globals) with the first test, not later.
- After a TypeScript major upgrade, read the tsconfig types option again.
- Run tsc --noEmit in CI so a missing typing fails the build, not only the test run.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- QistLockA NestJS and React SaaS driving an Android Device Owner agent that locks financed phones when payments lapse.
- WorkforceA field app, an admin console and a self-hosted Node.js server for push and reconciliation.
Related fixes
Back end
TypeScript cannot find '@angular/platform-browser' in an Angular project
Cannot find module '@angular/platform-browser' or its corresponding type declarations.
TS2307 means TypeScript could not turn the import into a file.
3 causes, each with a fixBack end
Angular build fails with TS2707 or "has no exported member" after installing a library
Module '"@angular/core"' has no exported member
These errors mean a library was compiled by a newer Angular than your app uses.
3 causes, each with a fixBack end
cdk synth says --app is required
--app is required either in command-line, in cdk.json or in ~/.cdk.json
The CDK CLI needs a command that runs your app and finds it in three places: the --app option, cdk.json in the project folder, or cdk.json in your home folder.
3 causes, each with a fixBack end
TypeScript cannot find module '@nestjs/core' or its type declarations
error TS2307: Cannot find module '@nestjs/common' or its corresponding type declarations.
TS2307 means TypeScript searched node_modules and found neither the package nor its types.
3 causes, each with a fixBack end
Mocha or Node cannot find ts-node/register
Error: Cannot find module 'ts-node/register'
Node resolves ts-node/register from your project's node_modules, so the package must be installed there, next to typescript.
2 causes, each with a fixBack end
TypeError: Class constructor cannot be invoked without 'new' in compiled TypeScript
TypeError: Class constructors cannot be invoked without 'new'
Node.js throws this when a native class is called like a plain function.
2 causes, each with a fix