Skip to content

Commit

Permalink
fix: --inspect-brk to pause before execution
Browse files Browse the repository at this point in the history
  • Loading branch information
AriPerkkio committed Mar 9, 2024
1 parent 6d1b145 commit 2409b11
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
27 changes: 23 additions & 4 deletions packages/vitest/src/runtime/inspector.ts
@@ -1,15 +1,17 @@
import { createRequire } from 'node:module'

import type { ResolvedConfig } from '../types'
import type { ContextRPC } from '../types'

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

/**
* Enables debugging inside `worker_threads` and `child_process`.
* Should be called as early as possible when worker/process has been set up.
*/
export function setupInspect(config: ResolvedConfig) {
export function setupInspect(ctx: ContextRPC) {
const config = ctx.config
const isEnabled = config.inspect || config.inspectBrk

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

if (config.inspectBrk)
if (config.inspectBrk) {
const firstTestFile = ctx.files[0]

// Stop at first test file
if (firstTestFile) {
session = new inspector.Session()
session.connect()

session.post('Debugger.enable')
session.post('Debugger.setBreakpointByUrl', {
lineNumber: 1,
url: new URL(firstTestFile, import.meta.url).href,
})
}

inspector.waitForDebugger()
}
}
}

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

return function cleanup() {
if (isEnabled && !keepOpen && inspector)
if (isEnabled && !keepOpen && inspector) {
inspector.close()
session?.disconnect()
}
}
}
2 changes: 1 addition & 1 deletion packages/vitest/src/runtime/worker.ts
Expand Up @@ -15,7 +15,7 @@ if (isChildProcess())
export async function run(ctx: ContextRPC) {
const prepareStart = performance.now()

const inspectorCleanup = setupInspect(ctx.config)
const inspectorCleanup = setupInspect(ctx)

process.env.VITEST_WORKER_ID = String(ctx.workerId)
process.env.VITEST_POOL_ID = String(poolId)
Expand Down

0 comments on commit 2409b11

Please sign in to comment.