Skip to content

Commit 2409b11

Browse files
committed
fix: --inspect-brk to pause before execution
1 parent 6d1b145 commit 2409b11

2 files changed

Lines changed: 24 additions & 5 deletions

File tree

packages/vitest/src/runtime/inspector.ts

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
import { createRequire } from 'node:module'
22

3-
import type { ResolvedConfig } from '../types'
3+
import type { ContextRPC } from '../types'
44

55
const __require = createRequire(import.meta.url)
66
let inspector: typeof import('node:inspector')
7+
let session: InstanceType<typeof inspector.Session>
78

89
/**
910
* Enables debugging inside `worker_threads` and `child_process`.
1011
* Should be called as early as possible when worker/process has been set up.
1112
*/
12-
export function setupInspect(config: ResolvedConfig) {
13+
export function setupInspect(ctx: ContextRPC) {
14+
const config = ctx.config
1315
const isEnabled = config.inspect || config.inspectBrk
1416

1517
if (isEnabled) {
@@ -20,8 +22,23 @@ export function setupInspect(config: ResolvedConfig) {
2022
if (!isOpen) {
2123
inspector.open()
2224

23-
if (config.inspectBrk)
25+
if (config.inspectBrk) {
26+
const firstTestFile = ctx.files[0]
27+
28+
// Stop at first test file
29+
if (firstTestFile) {
30+
session = new inspector.Session()
31+
session.connect()
32+
33+
session.post('Debugger.enable')
34+
session.post('Debugger.setBreakpointByUrl', {
35+
lineNumber: 1,
36+
url: new URL(firstTestFile, import.meta.url).href,
37+
})
38+
}
39+
2440
inspector.waitForDebugger()
41+
}
2542
}
2643
}
2744

@@ -32,7 +49,9 @@ export function setupInspect(config: ResolvedConfig) {
3249
const keepOpen = config.watch && (isIsolatedSingleFork || isIsolatedSingleThread)
3350

3451
return function cleanup() {
35-
if (isEnabled && !keepOpen && inspector)
52+
if (isEnabled && !keepOpen && inspector) {
3653
inspector.close()
54+
session?.disconnect()
55+
}
3756
}
3857
}

packages/vitest/src/runtime/worker.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ if (isChildProcess())
1515
export async function run(ctx: ContextRPC) {
1616
const prepareStart = performance.now()
1717

18-
const inspectorCleanup = setupInspect(ctx.config)
18+
const inspectorCleanup = setupInspect(ctx)
1919

2020
process.env.VITEST_WORKER_ID = String(ctx.workerId)
2121
process.env.VITEST_POOL_ID = String(poolId)

0 commit comments

Comments
 (0)