Skip to content

Commit

Permalink
refactor: cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
sheremet-va committed Jan 31, 2023
1 parent 1919e01 commit 3d21d1b
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 8 deletions.
8 changes: 8 additions & 0 deletions packages/utils/src/timers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,17 @@ export function getSafeTimers() {
setInterval: safeSetInterval,
clearInterval: safeClearInterval,
clearTimeout: safeClearTimeout,
setImmediate: safeSetImmediate,
clearImmediate: safeClearImmediate,
} = (globalThis as any)[SAFE_TIMERS_SYMBOL] || globalThis

return {
setTimeout: safeSetTimeout,
setInterval: safeSetInterval,
clearInterval: safeClearInterval,
clearTimeout: safeClearTimeout,
setImmediate: safeSetImmediate,
clearImmediate: safeClearImmediate,
}
}

Expand All @@ -22,13 +26,17 @@ export function setSafeTimers() {
setInterval: safeSetInterval,
clearInterval: safeClearInterval,
clearTimeout: safeClearTimeout,
setImmediate: safeSetImmediate,
clearImmediate: safeClearImmediate,
} = globalThis

const timers = {
setTimeout: safeSetTimeout,
setInterval: safeSetInterval,
clearInterval: safeClearInterval,
clearTimeout: safeClearTimeout,
setImmediate: safeSetImmediate,
clearImmediate: safeClearImmediate,
}

;(globalThis as any)[SAFE_TIMERS_SYMBOL] = timers
Expand Down
7 changes: 3 additions & 4 deletions packages/vitest/src/node/pool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,14 @@ export interface ProcessPool {
close: () => Promise<void>
}

const loaderPath = pathToFileURL(resolve(distDir, './loader.js')).href

const suppressLoaderWarningsPath = resolve(rootDir, './suppress-warnings.cjs')

export interface PoolProcessOptions {
execArgv: string[]
env: Record<string, string>
}

const loaderPath = pathToFileURL(resolve(distDir, './loader.js')).href
const suppressLoaderWarningsPath = resolve(rootDir, './suppress-warnings.cjs')

export function createPool(ctx: Vitest): ProcessPool {
const conditions = ctx.server.config.resolve.conditions?.flatMap(c => ['--conditions', c]) || []

Expand Down
19 changes: 18 additions & 1 deletion packages/vitest/src/node/pools/child.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,23 @@ function setupChildProcessChannel(ctx: Vitest, fork: ChildProcess) {
)
}

function stringifyRegex(input: RegExp | string): any {
if (typeof input === 'string')
return input
return `$$vitest:${input.toString()}`
}

function getTestConfig(ctx: Vitest) {
const config = ctx.getSerializableConfig()
// v8 serialize does not support regex
return {
...config,
testNamePattern: config.testNamePattern
? stringifyRegex(config.testNamePattern)
: undefined,
}
}

export function createChildProcessPool(ctx: Vitest, { execArgv, env }: PoolProcessOptions): ProcessPool {
// isolation is disabled with --no-threads
let child: ChildProcess
Expand All @@ -44,7 +61,7 @@ export function createChildProcessPool(ctx: Vitest, { execArgv, env }: PoolProce
function runWithFiles(files: string[], invalidates: string[] = []) {
const data: ChildContext = {
command: 'start',
config: ctx.getSerializableConfig(),
config: getTestConfig(ctx),
files,
invalidates,
}
Expand Down
15 changes: 15 additions & 0 deletions packages/vitest/src/runtime/child.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { createBirpc } from 'birpc'
import { processError } from '@vitest/runner/utils'
import { ModuleCacheMap } from 'vite-node/client'
import { isPrimitive } from 'vite-node/utils'
import { parseRegexp } from '@vitest/utils'
import type { ResolvedConfig } from '../types'
import { distDir } from '../constants'
import { getWorkerState } from '../utils/global'
Expand Down Expand Up @@ -106,6 +107,19 @@ function init(ctx: ChildContext) {
ctx.files.forEach(i => moduleCache.delete(i))
}

function parsePossibleRegexp(str: string | RegExp) {
const prefix = '$$vitest:'
if (typeof str === 'string' && str.startsWith(prefix))
return parseRegexp(str.slice(prefix.length))
return str
}

function unwrapConfig(config: ResolvedConfig) {
if (config.testNamePattern)
config.testNamePattern = parsePossibleRegexp(config.testNamePattern) as RegExp
return config
}

export async function run(ctx: ChildContext) {
init(ctx)
const { run } = await startViteNode(ctx)
Expand All @@ -117,6 +131,7 @@ const procesExit = process.exit
process.on('message', async (message: any) => {
if (typeof message === 'object' && message.command === 'start') {
try {
message.config = unwrapConfig(message.config)
await run(message)
}
finally {
Expand Down
11 changes: 8 additions & 3 deletions packages/vitest/src/runtime/rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,27 @@ import {
} from '@vitest/utils'
import { getWorkerState } from '../utils'

const { get } = Reflect
const safeRandom = Math.random

function withSafeTimers(fn: () => void) {
const { setTimeout: safeSetTimeout } = getSafeTimers()
const { setTimeout, clearTimeout } = getSafeTimers()

const currentSetTimeout = globalThis.setTimeout
const currentClearTimeout = globalThis.clearTimeout
const currentRandom = globalThis.Math.random

try {
globalThis.setTimeout = safeSetTimeout
globalThis.setTimeout = setTimeout
globalThis.clearTimeout = clearTimeout
globalThis.Math.random = safeRandom

const result = fn()
return result
}
finally {
globalThis.setTimeout = currentSetTimeout
globalThis.clearTimeout = currentClearTimeout
globalThis.Math.random = currentRandom
}
}
Expand All @@ -27,7 +32,7 @@ export const rpc = () => {
const { rpc } = getWorkerState()
return new Proxy(rpc, {
get(target, p, handler) {
const sendCall = Reflect.get(target, p, handler)
const sendCall = get(target, p, handler)
const safeSendCall = (...args: any[]) => withSafeTimers(() => sendCall(...args))
safeSendCall.asEvent = sendCall.asEvent
return safeSendCall
Expand Down
2 changes: 2 additions & 0 deletions test/core/test/timers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1186,6 +1186,8 @@ describe('FakeTimers', () => {

expect(global.setImmediate).not.toBe(nativeSetImmediate)
expect(global.clearImmediate).not.toBe(nativeClearImmediate)

fakeTimers.useRealTimers()
})
})

Expand Down

0 comments on commit 3d21d1b

Please sign in to comment.