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) +}) diff --git a/test/coverage-test/test/coverage.test.ts b/test/coverage-test/test/coverage.test.ts index 5d1d1b72e894..5bb78b2ded4b 100644 --- a/test/coverage-test/test/coverage.test.ts +++ b/test/coverage-test/test/coverage.test.ts @@ -14,7 +14,8 @@ import virtualFile2 from '\0vitest-custom-virtual-file-2' // Browser mode crashes with dynamic files. Enable this when browser mode works. // To keep istanbul report consistent between browser and node, skip dynamic tests when istanbul is used. -const skipDynamicFiles = '__vitest_browser__' in globalThis || globalThis.process?.env.COVERAGE_PROVIDER === 'istanbul' || !globalThis.process?.env.COVERAGE_PROVIDER +const provider = globalThis.process?.env.COVERAGE_PROVIDER +const skipDynamicFiles = '__vitest_browser__' in globalThis || provider === 'istanbul' || !provider const { pythagoras } = await (() => { if ('__vitest_browser__' in globalThis) @@ -78,7 +79,7 @@ test('decorators', () => { }) // Istanbul fails to follow source maps on windows -test.skipIf(globalThis.process?.env.COVERAGE_PROVIDER === 'istanbul')('pre-transpiled code with source maps to original', async () => { +test.runIf(provider === 'v8' || provider === 'custom')('pre-transpiled code with source maps to original', async () => { const transpiled = await import('../src/transpiled.js') transpiled.hello()