Skip to content

Commit 0b79231

Browse files
authored
fix: bind process in case global is overwritten (#11343)
1 parent 089354e commit 0b79231

3 files changed

Lines changed: 43 additions & 16 deletions

File tree

‎packages/vitest/src/runtime/moduleRunner/cachedResolver.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { splitFileAndPostfix } from '@vitest/utils/helpers'
44
import { join, normalize } from 'pathe'
55
import { distDir } from '../../paths'
66

7+
const platform = process.platform
78
const bareVitestRegexp = /^@?vitest(?:\/|$)/
89
const normalizedDistDir = normalize(distDir)
910
const relativeIds: Record<string, string> = {}
@@ -23,7 +24,7 @@ export function getCachedVitestImport(
2324
state: () => WorkerGlobalState,
2425
): null | { externalize: string; type: 'module' } {
2526
if (id.startsWith('/@fs/') || id.startsWith('\\@fs\\')) {
26-
id = id.slice(process.platform === 'win32' ? 5 : 4)
27+
id = id.slice(platform === 'win32' ? 5 : 4)
2728
}
2829

2930
if (externalizeMap.has(id)) {

‎packages/vitest/src/runtime/rpc.ts‎

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import { getWorkerState } from './utils'
88

99
const { get } = Reflect
1010

11+
const globalProcess = globalThis.process
12+
1113
function withSafeTimers(fn: () => void) {
1214
const { setTimeout, clearTimeout, nextTick, setImmediate, clearImmediate }
1315
= getSafeTimers()
@@ -17,7 +19,7 @@ function withSafeTimers(fn: () => void) {
1719
const currentSetImmediate = globalThis.setImmediate
1820
const currentClearImmediate = globalThis.clearImmediate
1921

20-
const currentNextTick = globalThis.process?.nextTick
22+
const currentNextTick = globalProcess?.nextTick
2123

2224
try {
2325
globalThis.setTimeout = setTimeout
@@ -30,8 +32,8 @@ function withSafeTimers(fn: () => void) {
3032
globalThis.clearImmediate = clearImmediate
3133
}
3234

33-
if (globalThis.process && nextTick) {
34-
globalThis.process.nextTick = nextTick
35+
if (globalProcess && nextTick) {
36+
globalProcess.nextTick = nextTick
3537
}
3638

3739
const result = fn()
@@ -43,9 +45,9 @@ function withSafeTimers(fn: () => void) {
4345
globalThis.setImmediate = currentSetImmediate
4446
globalThis.clearImmediate = currentClearImmediate
4547

46-
if (globalThis.process && nextTick) {
48+
if (globalProcess && nextTick) {
4749
nextTick(() => {
48-
globalThis.process.nextTick = currentNextTick
50+
globalProcess.nextTick = currentNextTick
4951
})
5052
}
5153
}
Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,41 @@
1-
import { expect, it, vi } from 'vitest'
1+
import { beforeAll, describe, expect, it, onTestFinished, vi } from 'vitest'
22

3-
vi.stubGlobal('process', { badMock: true })
3+
describe('undefined process', () => {
4+
// This has to be first test of the file to trigger rpc withSafeTimers race
5+
it('undefined process when importing file outside root', async () => {
6+
const realProcess = globalThis.process
47

5-
it('should not hang', () => {
6-
expect(1).toBe(1)
7-
})
8+
onTestFinished(async () => {
9+
globalThis.process = realProcess
10+
})
11+
12+
globalThis.process = undefined as any
813

9-
it('should not crash (#9173)', async () => {
10-
await import('./fixtures/increment')
14+
const mod = await import('../../test-utils/fixtures/external-math')
15+
expect(mod.sum(2, 3)).toBe(5)
16+
})
1117
})
1218

13-
it('should not hang', () => {
14-
vi.unstubAllGlobals()
19+
describe('stubbed process', () => {
20+
beforeAll(() => {
21+
vi.stubGlobal('process', { badMock: true })
22+
23+
return () => {
24+
vi.unstubAllGlobals()
25+
}
26+
})
27+
28+
it('should not hang', () => {
29+
expect(1).toBe(1)
30+
})
31+
32+
it('should not crash (#9173)', async () => {
33+
await import('./fixtures/increment')
34+
})
35+
36+
it('should not hang', () => {
37+
vi.unstubAllGlobals()
1538

16-
process.stdout.write = () => true
39+
process.stdout.write = () => true
40+
})
1741
})

0 commit comments

Comments
 (0)