diff --git a/packages/vitest/src/runtime/moduleRunner/errorCatcher.ts b/packages/vitest/src/runtime/moduleRunner/errorCatcher.ts index 59fc279f3db1..9568051695d1 100644 --- a/packages/vitest/src/runtime/moduleRunner/errorCatcher.ts +++ b/packages/vitest/src/runtime/moduleRunner/errorCatcher.ts @@ -1,6 +1,11 @@ import type { WorkerGlobalState } from '../../types/worker' import { serializeValue } from '@vitest/utils/serialize' +// Store globals in case tests overwrite them +const processListeners = process.listeners.bind(process) +const processOn = process.on.bind(process) +const processOff = process.off.bind(process) + const dispose: (() => void)[] = [] export function listenForErrors(state: () => WorkerGlobalState): void { @@ -10,7 +15,7 @@ export function listenForErrors(state: () => WorkerGlobalState): void { function catchError(err: any, type: string, event: 'uncaughtException' | 'unhandledRejection') { const worker = state() - const listeners = process.listeners(event as 'uncaughtException') + const listeners = processListeners(event as 'uncaughtException') // if there is another listener, assume that it's handled by user code // one is Vitest's own listener if (listeners.length > 1) { @@ -32,11 +37,11 @@ export function listenForErrors(state: () => WorkerGlobalState): void { const uncaughtException = (e: Error) => catchError(e, 'Uncaught Exception', 'uncaughtException') const unhandledRejection = (e: Error) => catchError(e, 'Unhandled Rejection', 'unhandledRejection') - process.on('uncaughtException', uncaughtException) - process.on('unhandledRejection', unhandledRejection) + processOn('uncaughtException', uncaughtException) + processOn('unhandledRejection', unhandledRejection) dispose.push(() => { - process.off('uncaughtException', uncaughtException) - process.off('unhandledRejection', unhandledRejection) + processOff('uncaughtException', uncaughtException) + processOff('unhandledRejection', unhandledRejection) }) } diff --git a/packages/vitest/src/runtime/workers/init.ts b/packages/vitest/src/runtime/workers/init.ts index e8ecd6c5227a..537ec713acc0 100644 --- a/packages/vitest/src/runtime/workers/init.ts +++ b/packages/vitest/src/runtime/workers/init.ts @@ -50,9 +50,20 @@ export function init(worker: Options): void { return } + try { + process.env.VITEST_POOL_ID = String(message.poolId) + process.env.VITEST_WORKER_ID = String(message.context.workerId) + } + catch (error) { + return send({ + type: 'testfileFinished', + __vitest_worker_response__, + error: serializeError(error), + usedMemory: reportMemory ? memoryUsage().heapUsed : undefined, + }) + } + isRunning = true - process.env.VITEST_POOL_ID = String(message.poolId) - process.env.VITEST_WORKER_ID = String(message.context.workerId) try { runPromise = entrypoint.run(message.context, worker) @@ -85,9 +96,20 @@ export function init(worker: Options): void { return } + try { + process.env.VITEST_POOL_ID = String(message.poolId) + process.env.VITEST_WORKER_ID = String(message.context.workerId) + } + catch (error) { + return send({ + type: 'testfileFinished', + __vitest_worker_response__, + error: serializeError(error), + usedMemory: reportMemory ? memoryUsage().heapUsed : undefined, + }) + } + isRunning = true - process.env.VITEST_POOL_ID = String(message.poolId) - process.env.VITEST_WORKER_ID = String(message.context.workerId) try { runPromise = entrypoint.collect(message.context, worker)