はじめに
Jestでテストを書こうとしたら、エラーが出てdescribe, it, expectなどの関数が使えませんでした。
問題
Cannot find name 'jest'.
解決方法
tsconfig.app.jsonにjestの設定を加える。
{
"compilerOptions": {
"tsBuildInfoFile": "./node_modules/.tmp/tsconfig.app.tsbuildinfo",
"target": "es2023",
"lib": ["ES2023", "DOM"],
"module": "esnext",
- "types": ["vite/client"],
+ "types": ["vite/client", "jest", "@testing-library/jest-dom"],
"skipLibCheck": true,
/* Bundler mode */
"moduleResolution": "bundler",
"allowImportingTsExtensions": true,
"verbatimModuleSyntax": true,
"moduleDetection": "force",
"noEmit": true,
"jsx": "react-jsx",
/* Linting */
"noUnusedLocals": true,
"noUnusedParameters": true,
"erasableSyntaxOnly": true,
"noFallthroughCasesInSwitch": true
},
"include": ["src"]
}
おわりに
過去にも同じことがあって、その時はなんかの記事を見て解決しました。
今回はすぐに見つからなかったので、過去の自分が作ったリポジトリを見返してたら解決できました。
自分の記事やコードに自分自身が救われることも最近結構あるので、記録を残すのは大事ですね。