このガイドでは、Vitest と @cloudflare/vitest-plugin を使って Workers のテストをデバッグする方法を説明します。
デバッグを始めるには、次のコマンドで Vitest を実行し、ポート 9229 にデバッガーを接続します。
vitest --inspect --no-file-parallelism既定では、インスペクターはポート 9229 で開きます。別のポート(例: 3456)を使う場合は、次のコマンドを実行します。
vitest --inspect=3456 --no-file-parallelismまたは、Vitest の設定ファイルで指定できます。
import { cloudflareTest } from "@cloudflare/vitest-plugin";
import { defineConfig } from "vitest/config";
export default defineConfig({
plugins: [
cloudflareTest({
// ...
}),
],
test: {
inspector: {
port: 3456,
},
},
});Worker のテストでブレークポイントデバッグを行うには、次の設定を含む .vscode/launch.json を作成します。
{
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Open inspector with Vitest",
"program": "${workspaceRoot}/node_modules/vitest/vitest.mjs",
"console": "integratedTerminal",
"args": ["--inspect=9229", "--no-file-parallelism"]
},
{
"name": "Attach to Workers Runtime",
"type": "node",
"request": "attach",
"port": 9229,
"cwd": "/",
"resolveSourceMapLocations": null,
"attachExistingChildren": false,
"autoAttachChildProcesses": false
}
],
"compounds": [
{
"name": "Debug Workers tests",
"configurations": [
"Open inspector with Vitest",
"Attach to Workers Runtime"
],
"stopAll": true
}
]
}Run & Debug パネル上部で Debug Workers tests を選び、Vitest のインスペクターを開き、Workers ランタイムにデバッガーを接続します。続いてテストファイルにブレークポイントを追加し、デバッグを始めます。