From 90f79fd137f449dd91f87e53a16c7e23a6727cb3 Mon Sep 17 00:00:00 2001 From: AriPerkkio Date: Mon, 25 Mar 2024 19:03:04 +0200 Subject: [PATCH] fix: prevent hang when `process` is mocked --- packages/vitest/package.json | 2 +- packages/vitest/src/runtime/workers/utils.ts | 8 ++++++-- pnpm-lock.yaml | 8 ++++---- test/core/test/mocked-process.test.ts | 7 +++++++ 4 files changed, 18 insertions(+), 7 deletions(-) create mode 100644 test/core/test/mocked-process.test.ts diff --git a/packages/vitest/package.json b/packages/vitest/package.json index c41c7faa182c..7dae5cc2374c 100644 --- a/packages/vitest/package.json +++ b/packages/vitest/package.json @@ -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" diff --git a/packages/vitest/src/runtime/workers/utils.ts b/packages/vitest/src/runtime/workers/utils.ts index 9ffbf2b4983d..bef492bdbc54 100644 --- a/packages/vitest/src/runtime/workers/utils.ts +++ b/packages/vitest/src/runtime/workers/utils.ts @@ -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) }, @@ -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 diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 1ee182d27ca4..7a9e86a5de95 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -1373,8 +1373,8 @@ importers: specifier: ^2.5.1 version: 2.5.1 tinypool: - specifier: ^0.8.2 - version: 0.8.2 + specifier: ^0.8.3 + version: 0.8.3 vite: specifier: ^5.0.12 version: 5.0.12(@types/node@20.11.5)(less@4.1.3) @@ -25476,8 +25476,8 @@ packages: js-tokens: 8.0.2 dev: true - /tinypool@0.8.2: - resolution: {integrity: sha512-SUszKYe5wgsxnNOVlBYO6IC+8VGWdVGZWAqUxp3UErNBtptZvWbwyUOyzNL59zigz2rCA92QiL3wvG+JDSdJdQ==} + /tinypool@0.8.3: + resolution: {integrity: sha512-Ud7uepAklqRH1bvwy22ynrliC7Dljz7Tm8M/0RBUW+YRa4YHhZ6e4PpgE+fu1zr/WqB1kbeuVrdfeuyIBpy4tw==} engines: {node: '>=14.0.0'} dev: false diff --git a/test/core/test/mocked-process.test.ts b/test/core/test/mocked-process.test.ts new file mode 100644 index 000000000000..380a8ad72a37 --- /dev/null +++ b/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) +})