できない.dev

TypeScript「Cannot find name 'describe'」の原因と直し方

Cannot find name 'describe' は、tsconfig の types に "jest" か "vitest/globals" を足せば直る。
TypeScript 6.0 は types の既定が空になり、@types/jest を入れても読まない。

公開:

症状できない · exit 2
npx tsc -p .
src/sum.test.ts(1,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.
src/sum.test.ts(2,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.
src/sum.test.ts(3,5): error TS2304: Cannot find name 'expect'.
解決後できた · exit 0
npx tsc -p .

2026-11-07 に node:24(TypeScript 6.0.3 / Node.js 24.22.2) で実際に打って取った出力。検証環境の詳細

要約

error TS2593: Cannot find name 'describe'. Do you need to install type definitions for a test runner? は、テストランナーの型定義が読まれていないときに出る型エラーだ。
describe と it がこの形で止まり、expect は案内の付かない TS2304: Cannot find name 'expect'. になる。
tsc やエディタの型検査で出るもので、テストの実行時に出る ReferenceError: describe is not defined とは別のエラーである。

TypeScript 6.0 では、tsconfig.json の types の既定が [] になった。
@types/jest を入れてあっても、types に "jest" を書くまで読まれない。

実行例

node:24 のコンテナで、types を書いていない tsconfig.json を TypeScript 6.0.3 の tsc にかけた。
@types/jest が入っていても同じエラーになり、Jest は "jest"、Vitest は "vitest/globals" を types に足すと終了コードが 0 になった。

$ npx tsc -v
Version 6.0.3
$ cat src/sum.test.ts
describe("sum", () => {
  it("adds", () => {
    expect(1 + 2).toBe(3);
  });
});
$ cat tsconfig.json
{
  "compilerOptions": {
    "noEmit": true
  },
  "include": ["src"]
}
$ npx tsc -p .
src/sum.test.ts(1,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.
src/sum.test.ts(2,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.
src/sum.test.ts(3,5): error TS2304: Cannot find name 'expect'.
$ echo $?
2
$ npm ls @types/jest
tmp.f3tc3rzuoq@1.0.0 /tmp/tmp.F3tc3RzuOQ
`-- @types/jest@30.0.0
$ cat tsconfig.json
{
  "compilerOptions": {
    "noEmit": true,
    "types": ["jest"]
  },
  "include": ["src"]
}
$ npx tsc -p .
$ echo $?
0
$ cat vitest.config.ts
import { defineConfig } from "vitest/config";
 
export default defineConfig({ test: { globals: true } });
$ npx tsc -p .
src/sum.test.ts(1,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.
src/sum.test.ts(2,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.
src/sum.test.ts(3,5): error TS2304: Cannot find name 'expect'.
$ echo $?
2
$ cat tsconfig.json
{
  "compilerOptions": {
    "noEmit": true,
    "types": ["vitest/globals"]
  },
  "include": ["src"]
}
$ npx tsc -p .
$ echo $?
0
$ npx tsc -v
Version 5.9.3
$ npx tsc -p .
src/sum.test.ts(1,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`.
src/sum.test.ts(2,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`.
src/sum.test.ts(3,5): error TS2304: Cannot find name 'expect'.
$ echo $?
2

— 2026-11-07 時点の出力

検証環境Node.js 24.22.2 / npm 11.19.1 / Python 3.11.2 / 2026-11-07 検証(ほか 2 件)
検証日
実行環境
node:24 Debian GNU/Linux 12 (bookworm)
バージョン
  • Node.js 24.22.2
  • npm 11.19.1
  • Python 3.11.2
  • Git 2.39.5
  • TypeScript 6.0.3

この記事の「実行例」は、上記の環境で実際にコマンドを実行して得られた出力をそのまま掲載しています。 再現手順はリポジトリの検証スクリプトとして管理し、定期的に再実行して出力を更新しています。

解決策

1. Jest は types に jest を足す

npm install --save-dev @types/jest
{
  "compilerOptions": {
    "types": ["jest"]
  }
}

types は書いた名前だけを読むので、Node.js の process や fs も使うなら ["node", "jest"] と並べる(process の記事)。
5.x と同じく node_modules/@types を全部読ませるなら、"types": ["*"] と書く。

2. Vitest は vitest/globals を足す

{
  "compilerOptions": {
    "types": ["vitest/globals"]
  }
}

Vitest の型は @types の下ではなく vitest 本体に入っている。
エラーの文言は @types/jest を案内するが、Vitest のプロジェクトでは入れなくても vitest/globals だけで通る。
globals を使わないなら、テストファイルの先頭で vitest から describe・it・expect を import すれば types は要らない(Vitest の globals(新しいタブで開く))。

3. テストだけを別の tsconfig で検査する

アプリのコードからテストランナーの名前が見えると、本番のコードに describe を書いても型エラーにならない。
テスト用に tsconfig.test.json を作り、extends で元の設定を引き継いだうえで、そちらの types にだけテストランナーを足す。

よくある原因

  1. TypeScript を 6.0 に上げた。
    5.x は types を書かなければ node_modules/@types の型定義を全部読んでいたので、@types/jest を入れるだけで通っていた。
  2. @types/jest を入れていない。
    5.x では TS2582 になり、文言は Try \npm i --save-dev @types/jest` or `npm i --save-dev @types/mocha`.で終わる。6.0 ではTS2593 になり、types` に足すよう促す一文が後ろに付く。
  3. Vitest の globals: true で describe を import せずに使っている。
    Vitest の型は @types の下に無いので、5.x でも types に vitest/globals を書かなければ読まれない。