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 be531e7 commit 90f79fd
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 7 deletions.
2 changes: 1 addition & 1 deletion packages/vitest/package.json
Expand Up @@ -159,7 +159,7 @@
"std-env": "^3.5.0",
"strip-literal": "^2.0.0",
"tinybench": "^2.5.1",
"tinypool": "^0.8.2",
"tinypool": "^0.8.3",
"vite": "^5.0.0",
"vite-node": "workspace:*",
"why-is-node-running": "^2.2.2"
Expand Down
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
8 changes: 4 additions & 4 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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 90f79fd

Please sign in to comment.