TypeScript / Jest / VSCode の環境設定
必要なnpmライブラリをインストール
npm i -D ts-jest @types/jest
ファイルを設定
launch.json
{ "configurations": [ { "name": "Debug Jest Tests", "type": "node", "request": "launch", "runtimeArgs": [ "${workspaceRoot}/node_modules/jest/bin/jest.js", "--runInBand" ], "console": "integratedTerminal", "internalConsoleOptions": "neverOpen" } ] }
runtimeArgs: これはNode.jsプロセスに渡されるランタイム引数の配列です。この例では、"jest.js"というJestのエントリポイントに"--runInBand"というオプションを渡しています。"--runInBand"オプションは、すべてのテストを同期的に一度に一つずつ実行することを意味します。
console: デバッグ出力を表示するためのコンソールタイプを指定します。"integratedTerminal"はVSCode内の統合ターミナルを使用します。
internalConsoleOptions: "neverOpen"は、デバッグコンソールが自動的に開くのを防ぎます。
jest.config.js
module.exports = { preset: 'ts-jest', testEnvironment: 'node', };
tsconfig.json
{ "compilerOptions": { "module": "commonjs", "moduleResolution": "node", "sourceMap": true } }
test.spec.ts(サンプル)
test('adds 1 + 2 to equal 3', () => { expect(1 + 2).toBe(3); });