11import { createRequire } from 'node:module'
22
3- import type { ResolvedConfig } from '../types'
3+ import type { ContextRPC } from '../types'
44
55const __require = createRequire ( import . meta. url )
66let 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}
0 commit comments