Skip to content

Commit b80062d

Browse files
authoredApr 11, 2024··
fix(vitest): close inspector immediately if run is canceled (#5519)
1 parent fc26f56 commit b80062d

File tree

3 files changed

+30
-7
lines changed

3 files changed

+30
-7
lines changed
 

‎packages/vitest/src/runtime/inspector.ts

+19-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { createRequire } from 'node:module'
22

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

55
const __require = createRequire(import.meta.url)
66
let inspector: typeof import('node:inspector')
@@ -44,11 +44,7 @@ export function setupInspect(ctx: ContextRPC) {
4444
}
4545
}
4646

47-
// In watch mode the inspector can persist re-runs if isolation is disabled and a single worker is used
48-
const isIsolatedSingleThread = config.pool === 'threads' && config.poolOptions?.threads?.isolate === false && config.poolOptions?.threads?.singleThread
49-
const isIsolatedSingleFork = config.pool === 'forks' && config.poolOptions?.forks?.isolate === false && config.poolOptions?.forks?.singleFork
50-
51-
const keepOpen = config.watch && (isIsolatedSingleFork || isIsolatedSingleThread)
47+
const keepOpen = shouldKeepOpen(config)
5248

5349
return function cleanup() {
5450
if (isEnabled && !keepOpen && inspector) {
@@ -57,3 +53,20 @@ export function setupInspect(ctx: ContextRPC) {
5753
}
5854
}
5955
}
56+
57+
export function closeInspector(config: ResolvedConfig) {
58+
const keepOpen = shouldKeepOpen(config)
59+
60+
if (inspector && !keepOpen) {
61+
inspector.close()
62+
session?.disconnect()
63+
}
64+
}
65+
66+
function shouldKeepOpen(config: ResolvedConfig) {
67+
// In watch mode the inspector can persist re-runs if isolation is disabled and a single worker is used
68+
const isIsolatedSingleThread = config.pool === 'threads' && config.poolOptions?.threads?.isolate === false && config.poolOptions?.threads?.singleThread
69+
const isIsolatedSingleFork = config.pool === 'forks' && config.poolOptions?.forks?.isolate === false && config.poolOptions?.forks?.singleFork
70+
71+
return config.watch && (isIsolatedSingleFork || isIsolatedSingleThread)
72+
}

‎packages/vitest/src/runtime/runBaseTests.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { setupChaiConfig } from '../integrations/chai/config'
88
import { setupGlobalEnv, withEnv } from './setup-node'
99
import type { VitestExecutor } from './execute'
1010
import { resolveTestRunner } from './runners'
11+
import { closeInspector } from './inspector'
1112

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

2223
const runner = await resolveTestRunner(config, executor)
2324

24-
workerState.onCancel.then(reason => runner.onCancel?.(reason))
25+
workerState.onCancel.then((reason) => {
26+
closeInspector(config)
27+
runner.onCancel?.(reason)
28+
})
2529

2630
workerState.durations.prepare = performance.now() - workerState.durations.prepare
2731
workerState.durations.environment = performance.now()

‎packages/vitest/src/runtime/runVmTests.ts

+6
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import * as VitestIndex from '../index'
1515
import type { VitestExecutor } from './execute'
1616
import { resolveTestRunner } from './runners'
1717
import { setupCommonEnv } from './setup-common'
18+
import { closeInspector } from './inspector'
1819

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

5758
const runner = await resolveTestRunner(config, executor)
5859

60+
workerState.onCancel.then((reason) => {
61+
closeInspector(config)
62+
runner.onCancel?.(reason)
63+
})
64+
5965
workerState.durations.prepare = performance.now() - workerState.durations.prepare
6066

6167
const { vi } = VitestIndex

0 commit comments

Comments
 (0)
Please sign in to comment.