Skip to content

Commit

Permalink
fix: prevent hang when process is mocked
Browse files Browse the repository at this point in the history
  • Loading branch information
AriPerkkio committed Mar 25, 2024
1 parent a8da192 commit 094e482
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
8 changes: 6 additions & 2 deletions packages/vitest/src/runtime/workers/utils.ts
Expand Up @@ -6,6 +6,10 @@ import type { WorkerRpcOptions } from './types'

const REGEXP_WRAP_PREFIX = '$$vitest:'

// Store global APIs in case process is overwritten by tests
const processSend = process.send?.bind(process)
const processOn = process.on?.bind(process)

export function createThreadsRpcOptions({ port }: WorkerContext): WorkerRpcOptions {
return {
post: (v) => { port.postMessage(v) },
Expand All @@ -17,9 +21,9 @@ export function createForksRpcOptions(nodeV8: typeof import('v8')): WorkerRpcOpt
return {
serialize: nodeV8.serialize,
deserialize: v => nodeV8.deserialize(Buffer.from(v)),
post(v) { process.send!(v) },
post(v) { processSend!(v) },
on(fn) {
process.on('message', (message: any, ...extras: any) => {
processOn('message', (message: any, ...extras: any) => {
// Do not react on Tinypool's internal messaging
if ((message as TinypoolWorkerMessage)?.__tinypool_worker_message__)
return
Expand Down
7 changes: 7 additions & 0 deletions test/core/test/mocked-process.test.ts
@@ -0,0 +1,7 @@
import { expect, it, vi } from 'vitest'

vi.stubGlobal('process', { badMock: true })

it('should not hang', () => {
expect(1).toBe(1)
})

0 comments on commit 094e482

Please sign in to comment.