diff --git a/packages/vitest/src/integrations/snapshot/client.ts b/packages/vitest/src/integrations/snapshot/client.ts index 2e9acaff33f2..3a12d733e03e 100644 --- a/packages/vitest/src/integrations/snapshot/client.ts +++ b/packages/vitest/src/integrations/snapshot/client.ts @@ -34,7 +34,7 @@ export class SnapshotClient { this.testFile = this.test!.file!.filepath this.snapshotState = new SnapshotState( resolveSnapshotPath(this.testFile), - process.__vitest_worker__!.config.snapshotOptions, + __vitest_worker__!.config.snapshotOptions, ) } } diff --git a/packages/vitest/src/node/mocker.ts b/packages/vitest/src/node/mocker.ts index f83c2f86134f..3a20d0ec18f3 100644 --- a/packages/vitest/src/node/mocker.ts +++ b/packages/vitest/src/node/mocker.ts @@ -73,7 +73,7 @@ export class VitestMocker { } public getSuiteFilepath() { - return process.__vitest_worker__?.filepath || 'global' + return __vitest_worker__?.filepath || 'global' } public getMocks() { diff --git a/packages/vitest/src/runtime/context.ts b/packages/vitest/src/runtime/context.ts index 99021b648c32..a4eaed40f2f5 100644 --- a/packages/vitest/src/runtime/context.ts +++ b/packages/vitest/src/runtime/context.ts @@ -17,11 +17,11 @@ export async function runWithSuite(suite: SuiteCollector, fn: (() => Awaitable any)>(fn: T, _timeout?: number): T { diff --git a/packages/vitest/src/runtime/entry.ts b/packages/vitest/src/runtime/entry.ts index 0a01691d2869..cad96f36d89d 100644 --- a/packages/vitest/src/runtime/entry.ts +++ b/packages/vitest/src/runtime/entry.ts @@ -14,12 +14,12 @@ export async function run(files: string[], config: ResolvedConfig): Promise { await startTests([file], config) }) - process.__vitest_worker__.filepath = undefined + __vitest_worker__.filepath = undefined } } diff --git a/packages/vitest/src/runtime/rpc.ts b/packages/vitest/src/runtime/rpc.ts index 900bff38271b..f466b3019193 100644 --- a/packages/vitest/src/runtime/rpc.ts +++ b/packages/vitest/src/runtime/rpc.ts @@ -1,3 +1,3 @@ export const rpc = () => { - return process.__vitest_worker__!.rpc + return __vitest_worker__!.rpc } diff --git a/packages/vitest/src/runtime/run.ts b/packages/vitest/src/runtime/run.ts index d92145981a14..c87a216d932e 100644 --- a/packages/vitest/src/runtime/run.ts +++ b/packages/vitest/src/runtime/run.ts @@ -63,7 +63,7 @@ export async function runTest(test: Test) { getSnapshotClient().setTest(test) - process.__vitest_worker__.current = test + __vitest_worker__.current = test try { await callSuiteHook(test.suite, 'beforeEach', [test, test.suite]) @@ -114,7 +114,7 @@ export async function runTest(test: Test) { test.result.duration = performance.now() - start - process.__vitest_worker__.current = undefined + __vitest_worker__.current = undefined updateTask(test) } @@ -213,7 +213,7 @@ export async function startTests(paths: string[], config: ResolvedConfig) { } export function clearModuleMocks() { - const { clearMocks, mockReset, restoreMocks } = process.__vitest_worker__.config + const { clearMocks, mockReset, restoreMocks } = __vitest_worker__.config // since each function calls another, we can just call one if (restoreMocks) diff --git a/packages/vitest/src/runtime/setup.ts b/packages/vitest/src/runtime/setup.ts index 2ca5dccbee07..7b7f9cefe065 100644 --- a/packages/vitest/src/runtime/setup.ts +++ b/packages/vitest/src/runtime/setup.ts @@ -26,7 +26,7 @@ export function setupConsoleLogSpy() { rpc().onUserConsoleLog({ type: 'stdout', content: String(data), - taskId: process.__vitest_worker__.current?.id, + taskId: __vitest_worker__.current?.id, time: Date.now(), }) callback() @@ -37,7 +37,7 @@ export function setupConsoleLogSpy() { rpc().onUserConsoleLog({ type: 'stderr', content: String(data), - taskId: process.__vitest_worker__.current?.id, + taskId: __vitest_worker__.current?.id, time: Date.now(), }) callback() @@ -69,7 +69,7 @@ export async function runSetupFiles(config: ResolvedConfig) { const files = toArray(config.setupFiles) await Promise.all( files.map(async(file) => { - process.__vitest_worker__.moduleCache.delete(file) + __vitest_worker__.moduleCache.delete(file) await import(file) }), ) diff --git a/packages/vitest/src/runtime/worker.ts b/packages/vitest/src/runtime/worker.ts index 0c2ce10415d8..d788c16b0f9a 100644 --- a/packages/vitest/src/runtime/worker.ts +++ b/packages/vitest/src/runtime/worker.ts @@ -1,7 +1,6 @@ import { resolve } from 'pathe' -import type { BirpcReturn } from 'birpc' import { createBirpc } from 'birpc' -import type { ModuleCache, ResolvedConfig, Test, WorkerContext, WorkerRPC } from '../types' +import type { ModuleCache, ResolvedConfig, WorkerContext, WorkerGlobalState, WorkerRPC } from '../types' import { distDir } from '../constants' import { executeInViteNode } from '../node/execute' import { rpc } from './rpc' @@ -10,6 +9,7 @@ let _viteNode: { run: (files: string[], config: ResolvedConfig) => Promise collect: (files: string[], config: ResolvedConfig) => Promise } +let __vitest_worker__: WorkerGlobalState const moduleCache: Map = new Map() const mockMap = {} @@ -53,14 +53,15 @@ async function startViteNode(ctx: WorkerContext) { } function init(ctx: WorkerContext) { - if (process.__vitest_worker__ && ctx.config.threads && ctx.config.isolate) - throw new Error(`worker for ${ctx.files.join(',')} already initialized by ${process.__vitest_worker__.ctx.files.join(',')}. This is probably an internal bug of Vitest.`) + if (__vitest_worker__ && ctx.config.threads && ctx.config.isolate) + throw new Error(`worker for ${ctx.files.join(',')} already initialized by ${__vitest_worker__.ctx.files.join(',')}. This is probably an internal bug of Vitest.`) process.stdout.write('\0') const { config, port } = ctx - process.__vitest_worker__ = { + // @ts-expect-error I know what I am doing :P + globalThis.__vitest_worker__ = { ctx, moduleCache, config, @@ -92,16 +93,5 @@ export async function run(ctx: WorkerContext) { } declare global { - namespace NodeJS { - interface Process { - __vitest_worker__: { - ctx: WorkerContext - config: ResolvedConfig - rpc: BirpcReturn - current?: Test - filepath?: string - moduleCache: Map - } - } - } + let __vitest_worker__: import('vitest').WorkerGlobalState } diff --git a/packages/vitest/src/types/worker.ts b/packages/vitest/src/types/worker.ts index acd76cd5d1a0..8d326adc257e 100644 --- a/packages/vitest/src/types/worker.ts +++ b/packages/vitest/src/types/worker.ts @@ -1,7 +1,8 @@ import type { MessagePort } from 'worker_threads' -import type { FetchFunction, RawSourceMap, ViteNodeResolveId } from 'vite-node' +import type { FetchFunction, ModuleCache, RawSourceMap, ViteNodeResolveId } from 'vite-node' +import type { BirpcReturn } from 'birpc' import type { ResolvedConfig } from './config' -import type { File, TaskResultPack } from './tasks' +import type { File, TaskResultPack, Test } from './tasks' import type { SnapshotResult } from './snapshot' import type { UserConsoleLog } from './general' @@ -27,3 +28,12 @@ export interface WorkerRPC { snapshotSaved: (snapshot: SnapshotResult) => void } + +export interface WorkerGlobalState { + ctx: WorkerContext + config: ResolvedConfig + rpc: BirpcReturn + current?: Test + filepath?: string + moduleCache: Map +}