Skip to content

Commit

Permalink
fix(vitest): close inspector immediately if run is canceled (#5519)
Browse files Browse the repository at this point in the history
  • Loading branch information
sheremet-va authored Apr 11, 2024
1 parent fc26f56 commit b80062d
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 7 deletions.
25 changes: 19 additions & 6 deletions packages/vitest/src/runtime/inspector.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { createRequire } from 'node:module'

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

const __require = createRequire(import.meta.url)
let inspector: typeof import('node:inspector')
Expand Down Expand Up @@ -44,11 +44,7 @@ export function setupInspect(ctx: ContextRPC) {
}
}

// In watch mode the inspector can persist re-runs if isolation is disabled and a single worker is used
const isIsolatedSingleThread = config.pool === 'threads' && config.poolOptions?.threads?.isolate === false && config.poolOptions?.threads?.singleThread
const isIsolatedSingleFork = config.pool === 'forks' && config.poolOptions?.forks?.isolate === false && config.poolOptions?.forks?.singleFork

const keepOpen = config.watch && (isIsolatedSingleFork || isIsolatedSingleThread)
const keepOpen = shouldKeepOpen(config)

return function cleanup() {
if (isEnabled && !keepOpen && inspector) {
Expand All @@ -57,3 +53,20 @@ export function setupInspect(ctx: ContextRPC) {
}
}
}

export function closeInspector(config: ResolvedConfig) {
const keepOpen = shouldKeepOpen(config)

if (inspector && !keepOpen) {
inspector.close()
session?.disconnect()
}
}

function shouldKeepOpen(config: ResolvedConfig) {
// In watch mode the inspector can persist re-runs if isolation is disabled and a single worker is used
const isIsolatedSingleThread = config.pool === 'threads' && config.poolOptions?.threads?.isolate === false && config.poolOptions?.threads?.singleThread
const isIsolatedSingleFork = config.pool === 'forks' && config.poolOptions?.forks?.isolate === false && config.poolOptions?.forks?.singleFork

return config.watch && (isIsolatedSingleFork || isIsolatedSingleThread)
}
6 changes: 5 additions & 1 deletion packages/vitest/src/runtime/runBaseTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { setupChaiConfig } from '../integrations/chai/config'
import { setupGlobalEnv, withEnv } from './setup-node'
import type { VitestExecutor } from './execute'
import { resolveTestRunner } from './runners'
import { closeInspector } from './inspector'

// browser shouldn't call this!
export async function run(files: string[], config: ResolvedConfig, environment: ResolvedTestEnvironment, executor: VitestExecutor): Promise<void> {
Expand All @@ -21,7 +22,10 @@ export async function run(files: string[], config: ResolvedConfig, environment:

const runner = await resolveTestRunner(config, executor)

workerState.onCancel.then(reason => runner.onCancel?.(reason))
workerState.onCancel.then((reason) => {
closeInspector(config)
runner.onCancel?.(reason)
})

workerState.durations.prepare = performance.now() - workerState.durations.prepare
workerState.durations.environment = performance.now()
Expand Down
6 changes: 6 additions & 0 deletions packages/vitest/src/runtime/runVmTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import * as VitestIndex from '../index'
import type { VitestExecutor } from './execute'
import { resolveTestRunner } from './runners'
import { setupCommonEnv } from './setup-common'
import { closeInspector } from './inspector'

export async function run(files: string[], config: ResolvedConfig, executor: VitestExecutor): Promise<void> {
const workerState = getWorkerState()
Expand Down Expand Up @@ -56,6 +57,11 @@ export async function run(files: string[], config: ResolvedConfig, executor: Vit

const runner = await resolveTestRunner(config, executor)

workerState.onCancel.then((reason) => {
closeInspector(config)
runner.onCancel?.(reason)
})

workerState.durations.prepare = performance.now() - workerState.durations.prepare

const { vi } = VitestIndex
Expand Down

0 comments on commit b80062d

Please sign in to comment.