Skip to content

Commit

Permalink
fix: prevent hang when process is mocked (#5430)
Browse files Browse the repository at this point in the history
  • Loading branch information
AriPerkkio authored Mar 26, 2024
1 parent 26718eb commit 0ec4d0e
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 9 deletions.
2 changes: 1 addition & 1 deletion packages/vitest/package.json
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { expect, it, vi } from 'vitest'

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

it('should not hang', () => {
expect(1).toBe(1)
})
5 changes: 3 additions & 2 deletions test/coverage-test/test/coverage.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit 0ec4d0e

Please sign in to comment.