Skip to content

Commit 0ec4d0e

Browse files
authoredMar 26, 2024··
fix: prevent hang when process is mocked (#5430)
1 parent 26718eb commit 0ec4d0e

File tree

5 files changed

+21
-9
lines changed

5 files changed

+21
-9
lines changed
 

‎packages/vitest/package.json

+1-1
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

+6-2
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

+4-4
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

+7
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+
})

‎test/coverage-test/test/coverage.test.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ import virtualFile2 from '\0vitest-custom-virtual-file-2'
1414

1515
// Browser mode crashes with dynamic files. Enable this when browser mode works.
1616
// To keep istanbul report consistent between browser and node, skip dynamic tests when istanbul is used.
17-
const skipDynamicFiles = '__vitest_browser__' in globalThis || globalThis.process?.env.COVERAGE_PROVIDER === 'istanbul' || !globalThis.process?.env.COVERAGE_PROVIDER
17+
const provider = globalThis.process?.env.COVERAGE_PROVIDER
18+
const skipDynamicFiles = '__vitest_browser__' in globalThis || provider === 'istanbul' || !provider
1819

1920
const { pythagoras } = await (() => {
2021
if ('__vitest_browser__' in globalThis)
@@ -78,7 +79,7 @@ test('decorators', () => {
7879
})
7980

8081
// Istanbul fails to follow source maps on windows
81-
test.skipIf(globalThis.process?.env.COVERAGE_PROVIDER === 'istanbul')('pre-transpiled code with source maps to original', async () => {
82+
test.runIf(provider === 'v8' || provider === 'custom')('pre-transpiled code with source maps to original', async () => {
8283
const transpiled = await import('../src/transpiled.js')
8384

8485
transpiled.hello()

0 commit comments

Comments
 (0)
Please sign in to comment.