From a2c742abb518b16e7fd64da82fef7202cabda808 Mon Sep 17 00:00:00 2001 From: Vladimir Sheremet Date: Wed, 10 Apr 2024 13:28:31 +0200 Subject: [PATCH] fix(vitest): close inspector immediately if run is canceled --- packages/vitest/src/runtime/inspector.ts | 25 ++++++++++++++++----- packages/vitest/src/runtime/runBaseTests.ts | 6 ++++- packages/vitest/src/runtime/runVmTests.ts | 6 +++++ 3 files changed, 30 insertions(+), 7 deletions(-) diff --git a/packages/vitest/src/runtime/inspector.ts b/packages/vitest/src/runtime/inspector.ts index 0631a7990bc5..19537e4b01a9 100644 --- a/packages/vitest/src/runtime/inspector.ts +++ b/packages/vitest/src/runtime/inspector.ts @@ -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') @@ -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) { @@ -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) +} diff --git a/packages/vitest/src/runtime/runBaseTests.ts b/packages/vitest/src/runtime/runBaseTests.ts index e89b3f42affa..d9775aa308b0 100644 --- a/packages/vitest/src/runtime/runBaseTests.ts +++ b/packages/vitest/src/runtime/runBaseTests.ts @@ -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 { @@ -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() diff --git a/packages/vitest/src/runtime/runVmTests.ts b/packages/vitest/src/runtime/runVmTests.ts index d022370d2890..3121b7e6ffa3 100644 --- a/packages/vitest/src/runtime/runVmTests.ts +++ b/packages/vitest/src/runtime/runVmTests.ts @@ -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 { const workerState = getWorkerState() @@ -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