Skip to content

Commit

Permalink
refactor(browser): remove process mock (#3701)
Browse files Browse the repository at this point in the history
  • Loading branch information
Marcel-G authored Jun 29, 2023
1 parent c8c1b4a commit a96bc22
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 15 deletions.
4 changes: 0 additions & 4 deletions packages/browser/src/client/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,6 @@ import { setupDialogsSpy } from './dialog'
import { BrowserSnapshotEnvironment } from './snapshot'
import { VitestBrowserClientMocker } from './mocker'

// @ts-expect-error mocking some node apis
globalThis.process = { env: {}, argv: [], cwd: () => '/', stdout: { write: () => {} }, nextTick: cb => cb() }
globalThis.global = globalThis

export const PORT = import.meta.hot ? '51204' : location.port
export const HOST = [location.hostname, PORT].filter(Boolean).join(':')
export const ENTRY_URL = `${
Expand Down
16 changes: 11 additions & 5 deletions packages/browser/src/client/rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,20 @@ function withSafeTimers(getTimers: typeof getSafeTimers, fn: () => void) {

const currentSetTimeout = globalThis.setTimeout
const currentClearTimeout = globalThis.clearTimeout
const currentNextTick = globalThis.process.nextTick
const currentSetImmediate = globalThis.setImmediate
const currentClearImmediate = globalThis.clearImmediate

const currentNextTick = globalThis.process?.nextTick

try {
globalThis.setTimeout = setTimeout
globalThis.clearTimeout = clearTimeout
globalThis.process.nextTick = nextTick
globalThis.setImmediate = setImmediate
globalThis.clearImmediate = clearImmediate

if (globalThis.process)
globalThis.process.nextTick = nextTick

const result = fn()
return result
}
Expand All @@ -29,9 +32,12 @@ function withSafeTimers(getTimers: typeof getSafeTimers, fn: () => void) {
globalThis.clearTimeout = currentClearTimeout
globalThis.setImmediate = currentSetImmediate
globalThis.clearImmediate = currentClearImmediate
nextTick(() => {
globalThis.process.nextTick = currentNextTick
})

if (globalThis.process) {
nextTick(() => {
globalThis.process.nextTick = currentNextTick
})
}
}
}

Expand Down
16 changes: 11 additions & 5 deletions packages/vitest/src/runtime/rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,20 @@ function withSafeTimers(fn: () => void) {

const currentSetTimeout = globalThis.setTimeout
const currentClearTimeout = globalThis.clearTimeout
const currentNextTick = globalThis.process.nextTick
const currentSetImmediate = globalThis.setImmediate
const currentClearImmediate = globalThis.clearImmediate

const currentNextTick = globalThis.process?.nextTick

try {
globalThis.setTimeout = setTimeout
globalThis.clearTimeout = clearTimeout
globalThis.process.nextTick = nextTick
globalThis.setImmediate = setImmediate
globalThis.clearImmediate = clearImmediate

if (globalThis.process)
globalThis.process.nextTick = nextTick

const result = fn()
return result
}
Expand All @@ -29,9 +32,12 @@ function withSafeTimers(fn: () => void) {
globalThis.clearTimeout = currentClearTimeout
globalThis.setImmediate = currentSetImmediate
globalThis.clearImmediate = currentClearImmediate
nextTick(() => {
globalThis.process.nextTick = currentNextTick
})

if (globalThis.process) {
nextTick(() => {
globalThis.process.nextTick = currentNextTick
})
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion test/coverage-test/test/coverage.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { runDynamicFileCJS, runDynamicFileESM } from '../src/dynamic-files'

// 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 = process.env.COVERAGE_PROVIDER === 'istanbul' || !process.env.COVERAGE_PROVIDER
const skipDynamicFiles = globalThis.process?.env.COVERAGE_PROVIDER === 'istanbul' || !globalThis.process?.env.COVERAGE_PROVIDER

const { pythagoras } = await (() => {
if ('__vitest_browser__' in globalThis)
Expand Down

0 comments on commit a96bc22

Please sign in to comment.