Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions packages/vitest/src/runtime/moduleRunner/errorCatcher.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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) {
Expand All @@ -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)
})
}
30 changes: 26 additions & 4 deletions packages/vitest/src/runtime/workers/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
Loading