Skip to content

Commit 6240d51

Browse files
authored
fix: bind process in case global is overwritten (#8916)
1 parent d41fa74 commit 6240d51

File tree

2 files changed

+36
-9
lines changed

2 files changed

+36
-9
lines changed

packages/vitest/src/runtime/moduleRunner/errorCatcher.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
import type { WorkerGlobalState } from '../../types/worker'
22
import { serializeValue } from '@vitest/utils/serialize'
33

4+
// Store globals in case tests overwrite them
5+
const processListeners = process.listeners.bind(process)
6+
const processOn = process.on.bind(process)
7+
const processOff = process.off.bind(process)
8+
49
const dispose: (() => void)[] = []
510

611
export function listenForErrors(state: () => WorkerGlobalState): void {
@@ -10,7 +15,7 @@ export function listenForErrors(state: () => WorkerGlobalState): void {
1015
function catchError(err: any, type: string, event: 'uncaughtException' | 'unhandledRejection') {
1116
const worker = state()
1217

13-
const listeners = process.listeners(event as 'uncaughtException')
18+
const listeners = processListeners(event as 'uncaughtException')
1419
// if there is another listener, assume that it's handled by user code
1520
// one is Vitest's own listener
1621
if (listeners.length > 1) {
@@ -32,11 +37,11 @@ export function listenForErrors(state: () => WorkerGlobalState): void {
3237
const uncaughtException = (e: Error) => catchError(e, 'Uncaught Exception', 'uncaughtException')
3338
const unhandledRejection = (e: Error) => catchError(e, 'Unhandled Rejection', 'unhandledRejection')
3439

35-
process.on('uncaughtException', uncaughtException)
36-
process.on('unhandledRejection', unhandledRejection)
40+
processOn('uncaughtException', uncaughtException)
41+
processOn('unhandledRejection', unhandledRejection)
3742

3843
dispose.push(() => {
39-
process.off('uncaughtException', uncaughtException)
40-
process.off('unhandledRejection', unhandledRejection)
44+
processOff('uncaughtException', uncaughtException)
45+
processOff('unhandledRejection', unhandledRejection)
4146
})
4247
}

packages/vitest/src/runtime/workers/init.ts

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,20 @@ export function init(worker: Options): void {
5050
return
5151
}
5252

53+
try {
54+
process.env.VITEST_POOL_ID = String(message.poolId)
55+
process.env.VITEST_WORKER_ID = String(message.context.workerId)
56+
}
57+
catch (error) {
58+
return send({
59+
type: 'testfileFinished',
60+
__vitest_worker_response__,
61+
error: serializeError(error),
62+
usedMemory: reportMemory ? memoryUsage().heapUsed : undefined,
63+
})
64+
}
65+
5366
isRunning = true
54-
process.env.VITEST_POOL_ID = String(message.poolId)
55-
process.env.VITEST_WORKER_ID = String(message.context.workerId)
5667

5768
try {
5869
runPromise = entrypoint.run(message.context, worker)
@@ -85,9 +96,20 @@ export function init(worker: Options): void {
8596
return
8697
}
8798

99+
try {
100+
process.env.VITEST_POOL_ID = String(message.poolId)
101+
process.env.VITEST_WORKER_ID = String(message.context.workerId)
102+
}
103+
catch (error) {
104+
return send({
105+
type: 'testfileFinished',
106+
__vitest_worker_response__,
107+
error: serializeError(error),
108+
usedMemory: reportMemory ? memoryUsage().heapUsed : undefined,
109+
})
110+
}
111+
88112
isRunning = true
89-
process.env.VITEST_POOL_ID = String(message.poolId)
90-
process.env.VITEST_WORKER_ID = String(message.context.workerId)
91113

92114
try {
93115
runPromise = entrypoint.collect(message.context, worker)

0 commit comments

Comments
 (0)