Skip to content

Commit 38d3d57

Browse files
[Vitest Integration] add vitest debugging guide (#20360)
Co-authored-by: ToriLindsay <[email protected]>
1 parent 36ad6a3 commit 38d3d57

File tree

1 file changed

+83
-0
lines changed

1 file changed

+83
-0
lines changed
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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+
This guide shows you how to debug your Workers tests with Vitest. This is available with `@cloudflare/vitest-pool-workers` v0.7.5 or later.
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+
## Customize the inspector port
21+
22+
By default, the inspector will be opened on port `9229`. If you need to use a different port (for example, `3456`), you can run the following command:
23+
24+
```sh
25+
vitest --inspect=3456 --no-file-parallelism
26+
```
27+
28+
Alternatively, you can 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, 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+
"resolveSourceMapLocations": null,
69+
"attachExistingChildren": false,
70+
"autoAttachChildProcesses": false,
71+
}
72+
],
73+
"compounds": [
74+
{
75+
"name": "Debug Workers tests",
76+
"configurations": ["Open inspector with Vitest", "Attach to Workers Runtime"],
77+
"stopAll": true
78+
}
79+
]
80+
}
81+
```
82+
83+
Select **Debug Workers tests** at the top of the **Run & Debug** panel to open an inspector with Vitest and attach a debugger to the Workers runtime. Then you can add breakpoints to your test files and start debugging.

0 commit comments

Comments
 (0)