From 8e6c104a8755ca0db91700e0b9a513c0121aa1df Mon Sep 17 00:00:00 2001 From: Vladimir Date: Tue, 9 Jan 2024 14:27:49 +0100 Subject: [PATCH] fix(vitest): gracefully exit Vitest if `process.exit` is called inside the test (#4903) --- packages/vitest/src/node/error.ts | 3 +-- packages/vitest/src/node/pools/rpc.ts | 4 ---- packages/vitest/src/runtime/child.ts | 2 +- packages/vitest/src/runtime/execute.ts | 6 +----- packages/vitest/src/runtime/vm.ts | 2 +- packages/vitest/src/runtime/worker.ts | 2 +- packages/vitest/src/types/rpc.ts | 1 - 7 files changed, 5 insertions(+), 15 deletions(-) diff --git a/packages/vitest/src/node/error.ts b/packages/vitest/src/node/error.ts index c45bf1b94233..a1b3e0d5e23f 100644 --- a/packages/vitest/src/node/error.ts +++ b/packages/vitest/src/node/error.ts @@ -59,8 +59,7 @@ export async function printError(error: unknown, project: WorkspaceProject | und const nearest = error instanceof TypeCheckError ? error.stacks[0] : stacks.find(stack => - project.getModuleById(stack.file) - && existsSync(stack.file), + project.server && project.getModuleById(stack.file) && existsSync(stack.file), ) const errorProperties = getErrorProperties(e) diff --git a/packages/vitest/src/node/pools/rpc.ts b/packages/vitest/src/node/pools/rpc.ts index e291c63bf2e8..64454584d807 100644 --- a/packages/vitest/src/node/pools/rpc.ts +++ b/packages/vitest/src/node/pools/rpc.ts @@ -5,10 +5,6 @@ import type { WorkspaceProject } from '../workspace' export function createMethodsRPC(project: WorkspaceProject): RuntimeRPC { const ctx = project.ctx return { - async onWorkerExit(error, code) { - await ctx.logger.printError(error, { type: 'Unexpected Exit', fullStack: true }) - process.exit(code || 1) - }, snapshotSaved(snapshot) { ctx.snapshot.add(snapshot) }, diff --git a/packages/vitest/src/runtime/child.ts b/packages/vitest/src/runtime/child.ts index 1311a20ae284..962089a85158 100644 --- a/packages/vitest/src/runtime/child.ts +++ b/packages/vitest/src/runtime/child.ts @@ -34,7 +34,7 @@ async function init(ctx: ChildContext) { onCancel: setCancel, }, { - eventNames: ['onUserConsoleLog', 'onFinished', 'onCollected', 'onWorkerExit', 'onCancel'], + eventNames: ['onUserConsoleLog', 'onFinished', 'onCollected', 'onCancel'], serialize: v8.serialize, deserialize: v => v8.deserialize(Buffer.from(v)), post(v) { diff --git a/packages/vitest/src/runtime/execute.ts b/packages/vitest/src/runtime/execute.ts index be95df130652..2ceda37271b1 100644 --- a/packages/vitest/src/runtime/execute.ts +++ b/packages/vitest/src/runtime/execute.ts @@ -70,12 +70,8 @@ export async function startVitestExecutor(options: ContextExecutorOptions) { const state = (): WorkerGlobalState => globalThis.__vitest_worker__ || options.state const rpc = () => state().rpc - const processExit = process.exit - process.exit = (code = process.exitCode || 0): never => { - const error = new Error(`process.exit called with "${code}"`) - rpc().onWorkerExit(error, code) - return processExit(code) + throw new Error(`process.exit unexpectedly called with "${code}"`) } function catchError(err: unknown, type: string) { diff --git a/packages/vitest/src/runtime/vm.ts b/packages/vitest/src/runtime/vm.ts index 0214196cf7e4..a58a851e726e 100644 --- a/packages/vitest/src/runtime/vm.ts +++ b/packages/vitest/src/runtime/vm.ts @@ -31,7 +31,7 @@ export async function run(ctx: WorkerContext) { onCancel: setCancel, }, { - eventNames: ['onUserConsoleLog', 'onFinished', 'onCollected', 'onWorkerExit'], + eventNames: ['onUserConsoleLog', 'onFinished', 'onCollected'], post(v) { port.postMessage(v) }, on(fn) { port.addListener('message', fn) }, }, diff --git a/packages/vitest/src/runtime/worker.ts b/packages/vitest/src/runtime/worker.ts index a8d274ae0d60..96f2cf81ce88 100644 --- a/packages/vitest/src/runtime/worker.ts +++ b/packages/vitest/src/runtime/worker.ts @@ -32,7 +32,7 @@ async function init(ctx: WorkerContext) { onCancel: setCancel, }, { - eventNames: ['onUserConsoleLog', 'onFinished', 'onCollected', 'onWorkerExit', 'onCancel'], + eventNames: ['onUserConsoleLog', 'onFinished', 'onCollected', 'onCancel'], post(v) { port.postMessage(v) }, on(fn) { port.addListener('message', fn) }, }, diff --git a/packages/vitest/src/types/rpc.ts b/packages/vitest/src/types/rpc.ts index 9e72918d95af..1ea4d688f430 100644 --- a/packages/vitest/src/types/rpc.ts +++ b/packages/vitest/src/types/rpc.ts @@ -15,7 +15,6 @@ export interface RuntimeRPC { getSourceMap: (id: string, force?: boolean) => Promise onFinished: (files: File[], errors?: unknown[]) => void - onWorkerExit: (error: unknown, code?: number) => void onPathsCollected: (paths: string[]) => void onUserConsoleLog: (log: UserConsoleLog) => void onUnhandledError: (err: unknown, type: string) => void