Test runners such as Jest, Mocha and Vitest (with its globals option) put describe, it and test in the global scope. TypeScript knows these names are usually test globals, so instead of plain TS2304 it reports TS2582, suggesting types to install.
With a types list in the config that leaves the runner out, the same problem is TS2593, whose message adds the step of naming the package in types. Names the checker does not associate with test runners, such as expect, stay TS2304.
At a glance
| Code | TS2582 |
|---|---|
| Message | Cannot find name '{0}'. 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`. |
Also on this page: TS2593 | Cannot find name '{0}'. 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. |
| TypeScript's category | error |
| Listed under | Unknown names and members |
| Depends on | types |
| In .js files | Reported when the file is checked: checkJs in jsconfig.json, or // @ts-check at the top of the file |
The messages are TypeScript's own text from its diagnostic messages file (Apache-2.0); {0} is filled in by tsc for each error.
What it looks like
describe("cart", () => {error TS2582: 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`.
it("starts empty", () => {error TS2582: 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`.
expect([].length).toBe(0);error TS2304: Cannot find name 'expect'.
});
});jsconfig.json for this example
{
"compilerOptions": {
"checkJs": true,
"noEmit": true,
"strict": true,
"target": "es2022",
"module": "nodenext",
"typeRoots": ["./node_modules/@types"]
},
"include": ["**/*.js"]
} describe("cart", () => {error TS2593: 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.
it("starts empty", () => {error TS2593: 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` and then add 'jest' or 'mocha' to the types field in your tsconfig.
expect([].length).toBe(0);error TS2304: Cannot find name 'expect'.
});
});jsconfig.json for this example
{
"compilerOptions": {
"checkJs": true,
"noEmit": true,
"strict": true,
"target": "es2022",
"module": "nodenext",
"types": []
},
"include": ["**/*.js"]
} What causes it
- The runner's types are not installed, or not loaded: a
typeslist (this site's configs usetypes: []or a short list) loads only the packages it names. - The test files are checked by a config meant for application code.
How to fix it
Name the runner's global types
Add the runner's global declarations to types: "vitest/globals" for Vitest with globals on, "jest" (with @types/jest installed) for Jest, "mocha" for Mocha. The fixed example uses Vitest, which this site has installed.
describe("cart", () => {
it("starts empty", () => {
expect([].length).toBe(0);
});
});jsconfig.json for this example
{
"compilerOptions": {
"checkJs": true,
"noEmit": true,
"strict": true,
"target": "es2022",
"module": "nodenext",
"types": ["vitest/globals"]
},
"include": ["**/*.js"]
} Or import the functions
Runners that export them let you write import { describe, it, expect } from "vitest"; (or @jest/globals), which needs no global types at all. The recipe for test files shows both.
About the examples
The first failing example has no types list and points typeRoots at its own empty folder, to stand for a project with no test runner types installed; the second has a types list that leaves the runner out, which is the TS2593 case.
Under TypeScript 6.0 and 7.0
Every example on this page also runs under TypeScript 6.0.3 and 7.0.2 when this site is built. One of them reports something different from 5.9.3, the version the page describes; the ✗ on its badge links to its entry below.
types defaults to an empty list, so the hint names the types field
The pattern: A global from a package that is not installed (process, describe, it) in a config that does not set types.
Why: Only the 6.0 default types: [] causes this. The name is still missing in every version, but 6.0 and 7.0 report it with the code and message for a project that lists its types (TS2591 instead of TS2580, TS2593 instead of TS2582), which add "and then add 'node' to the types field in your tsconfig". Source: TypeScript 6.0 release notes: types now defaults to an empty list.
Test globals with no test runner types and no types list cart.test.js
- TypeScript 5.9.3
cart.test.js(2,1): error TS2582: 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`. cart.test.js(4,3): error TS2582: 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`. cart.test.js(6,5): error TS2304: Cannot find name 'expect'.
- TypeScript 6.0.3differs from 5.9.3
cart.test.js(2,1): error TS2593: 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. cart.test.js(4,3): error TS2593: 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` and then add 'jest' or 'mocha' to the types field in your tsconfig. cart.test.js(6,5): error TS2304: Cannot find name 'expect'.
- TypeScript 7.0.2differs from 5.9.3
cart.test.js(2,1): error TS2593: 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. cart.test.js(4,3): error TS2593: 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` and then add 'jest' or 'mocha' to the types field in your tsconfig. cart.test.js(6,5): error TS2304: Cannot find name 'expect'.
What works in every version: Install the types and name them: "types": ["node"], plus your test runner's.
const port = Number(process.env.PORT ?? 3000);
export { port };jsconfig.json for this example
{
"compilerOptions": {
"checkJs": true,
"noEmit": true,
"strict": true,
"target": "es2022",
"module": "nodenext",
"types": ["node"]
},
"include": ["*.js"]
} Gotchas
- A separate config for tests (
jsconfig.test.jsonextending the main one, with the runner intypes) keeps test globals out of application files, so a straydescribeinsrc/is still caught.
Sources
- TSConfig Reference: types · TypeScript
- Vitest: globals · Vitest docs
- diagnosticMessages.json (TypeScript 5.9.3) · TypeScript on GitHub
Last reviewed 21 October 2026.