Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

TypeScript does not know describe, it or expect in my Jest tests

ErrorCannot 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.

  • Applies to TypeScript 5.9, Jest 29/30, ts-jest, Node.js 22
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Check whether the Jest typings are installed in the project at all.

    npm ls @types/jest @jest/globals
  2. 2Match what you see, then jump to its fix

    1. 1TS2582: Cannot find name 'it'. Do you need to install type definitions for a test runner?No Jest typings are installedFix
    2. 2TS2593: 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. 3The project uses Jest's own types, or two test runners are installed and their globals clashYou prefer typed imports instead of globalsFix

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/globals

2Fix

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/jest

3Verify

Type-check the project without running the tests.

npx tsc --noEmit

Confirmed by raw.githubusercontent.com

Did this fix it?

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.json

2Fix

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 --noEmit

Confirmed by typescriptlang.org

Did this fix it?

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.ts

2Fix

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 jest

Confirmed by raw.githubusercontent.com

Did this fix it?

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:

Related fixes

Back 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 fix

Back 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 fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.