Skip to content

Commit 90f79fd

Browse files
committed
fix: prevent hang when process is mocked
1 parent be531e7 commit 90f79fd

File tree

4 files changed

+18
-7
lines changed

4 files changed

+18
-7
lines changed

packages/vitest/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@
159159
"std-env": "^3.5.0",
160160
"strip-literal": "^2.0.0",
161161
"tinybench": "^2.5.1",
162-
"tinypool": "^0.8.2",
162+
"tinypool": "^0.8.3",
163163
"vite": "^5.0.0",
164164
"vite-node": "workspace:*",
165165
"why-is-node-running": "^2.2.2"

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ import type { WorkerRpcOptions } from './types'
66

77
const REGEXP_WRAP_PREFIX = '$$vitest:'
88

9+
// Store global APIs in case process is overwritten by tests
10+
const processSend = process.send?.bind(process)
11+
const processOn = process.on?.bind(process)
12+
913
export function createThreadsRpcOptions({ port }: WorkerContext): WorkerRpcOptions {
1014
return {
1115
post: (v) => { port.postMessage(v) },
@@ -17,9 +21,9 @@ export function createForksRpcOptions(nodeV8: typeof import('v8')): WorkerRpcOpt
1721
return {
1822
serialize: nodeV8.serialize,
1923
deserialize: v => nodeV8.deserialize(Buffer.from(v)),
20-
post(v) { process.send!(v) },
24+
post(v) { processSend!(v) },
2125
on(fn) {
22-
process.on('message', (message: any, ...extras: any) => {
26+
processOn('message', (message: any, ...extras: any) => {
2327
// Do not react on Tinypool's internal messaging
2428
if ((message as TinypoolWorkerMessage)?.__tinypool_worker_message__)
2529
return

pnpm-lock.yaml

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/core/test/mocked-process.test.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { expect, it, vi } from 'vitest'
2+
3+
vi.stubGlobal('process', { badMock: true })
4+
5+
it('should not hang', () => {
6+
expect(1).toBe(1)
7+
})

0 commit comments

Comments
 (0)