|
| 1 | +--- |
| 2 | +title: Debugging |
| 3 | +pcx_content_type: concept |
| 4 | +sidebar: |
| 5 | + order: 8 |
| 6 | +head: [] |
| 7 | +description: Debug your Workers tests with Vitest. |
| 8 | +--- |
| 9 | + |
| 10 | +Starting with `@cloudflare/vitest-pool-workers` v0.7.5, you can now debug your Workers tests with Vitest. |
| 11 | + |
| 12 | +## Open inspector with Vitest |
| 13 | + |
| 14 | +To start debugging, run Vitest with the following command and attach a debugger to port `9229`: |
| 15 | + |
| 16 | +```sh |
| 17 | +vitest --inspect --no-file-parallelism |
| 18 | +``` |
| 19 | + |
| 20 | +## Customizing the inspector port |
| 21 | + |
| 22 | +By default, the inspector will be opened on port `9229`. If you need to use a different port (e.g. `3456`), you can run the following command: |
| 23 | + |
| 24 | +```sh |
| 25 | +vitest --inspect=3456 --no-file-parallelism |
| 26 | +``` |
| 27 | + |
| 28 | +or define it in your Vitest configuration file: |
| 29 | + |
| 30 | +```ts |
| 31 | +import { defineWorkersConfig } from "@cloudflare/vitest-pool-workers/config"; |
| 32 | + |
| 33 | +export default defineWorkersConfig({ |
| 34 | + test: { |
| 35 | + inspector: { |
| 36 | + port: 3456, |
| 37 | + }, |
| 38 | + poolOptions: { |
| 39 | + workers: { |
| 40 | + // ... |
| 41 | + }, |
| 42 | + }, |
| 43 | + }, |
| 44 | +}); |
| 45 | +``` |
| 46 | + |
| 47 | +## Setup VS Code to use breakpoints |
| 48 | + |
| 49 | +To setup VS Code for breakpoint debugging in your Worker tests, you can create a `.vscode/launch.json` file that contains the following configuration: |
| 50 | + |
| 51 | +```json |
| 52 | +{ |
| 53 | + "configurations": [ |
| 54 | + { |
| 55 | + "type": "node", |
| 56 | + "request": "launch", |
| 57 | + "name": "Open inspector with Vitest", |
| 58 | + "program": "${workspaceRoot}/node_modules/vitest/vitest.mjs", |
| 59 | + "console": "integratedTerminal", |
| 60 | + "args": ["--inspect=9229", "--no-file-parallelism"] |
| 61 | + }, |
| 62 | + { |
| 63 | + "name": "Attach to Workers Runtime", |
| 64 | + "type": "node", |
| 65 | + "request": "attach", |
| 66 | + "port": 9229, |
| 67 | + "cwd": "/" |
| 68 | + } |
| 69 | + ], |
| 70 | + "compounds": [ |
| 71 | + { |
| 72 | + "name": "Debug Workers tests", |
| 73 | + "configurations": ["Open inspector with Vitest", "Attach to Workers Runtime"], |
| 74 | + "stopAll": true |
| 75 | + } |
| 76 | + ] |
| 77 | +} |
| 78 | +``` |
| 79 | + |
| 80 | +You will find the **Debug Workers tests** option at the top of the **Run & Debug** panel. This will open inspector with Vitest and attach a debugger to the Workers runtime. You can then add breakpoints to your test files and start debugging. |
0 commit comments