From 91d6ccd40bd27ee8ae72945ee1a3c715b3cae2ed Mon Sep 17 00:00:00 2001 From: poyoho <907415276@qq.com> Date: Wed, 9 Feb 2022 21:04:51 +0800 Subject: [PATCH 1/6] test: mess process --- test/core/test/basic.test.ts | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/test/core/test/basic.test.ts b/test/core/test/basic.test.ts index b646ce3220e4..aa429e7f5326 100644 --- a/test/core/test/basic.test.ts +++ b/test/core/test/basic.test.ts @@ -2,6 +2,12 @@ import { assert, expect, it, suite, test } from 'vitest' import { two } from '../src/submodule' import { timeout } from '../src/timeout' +test('mess process', () => { + // eslint-disable-next-line no-global-assign + process = 1 as any + expect(process).toStrictEqual(1) +}) + test('Math.sqrt()', async() => { assert.equal(Math.sqrt(4), two) assert.equal(Math.sqrt(2), Math.SQRT2) From a6744b1f3f4d811cd3807e9fd44a8bd08cd11a14 Mon Sep 17 00:00:00 2001 From: poyoho <907415276@qq.com> Date: Wed, 9 Feb 2022 22:22:17 +0800 Subject: [PATCH 2/6] feat: use global variables replace process --- packages/vitest/globals.d.ts | 1 + .../src/integrations/snapshot/client.ts | 2 +- packages/vitest/src/node/mocker.ts | 2 +- packages/vitest/src/runtime/context.ts | 4 ++-- packages/vitest/src/runtime/entry.ts | 4 ++-- packages/vitest/src/runtime/rpc.ts | 2 +- packages/vitest/src/runtime/run.ts | 6 ++--- packages/vitest/src/runtime/setup.ts | 6 ++--- packages/vitest/src/runtime/worker.ts | 24 ++++--------------- packages/vitest/src/types/worker.ts | 14 +++++++++-- test/core/test/basic.test.ts | 4 ++-- 11 files changed, 32 insertions(+), 37 deletions(-) diff --git a/packages/vitest/globals.d.ts b/packages/vitest/globals.d.ts index dbeb9fca262b..3e288db32f95 100644 --- a/packages/vitest/globals.d.ts +++ b/packages/vitest/globals.d.ts @@ -11,5 +11,6 @@ declare global { const afterAll: typeof import('vitest')['afterAll'] const beforeEach: typeof import('vitest')['beforeEach'] const afterEach: typeof import('vitest')['afterEach'] + const __vitest_worker__: import('vitest').WorkerGlobalState } export {} 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 8001c95e39e4..bb3c333d5088 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..fbb8f1b4808f 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..fa8772abfa49 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, WorkerRPC } from '../types' import { distDir } from '../constants' import { executeInViteNode } from '../node/execute' import { rpc } from './rpc' @@ -53,14 +52,14 @@ 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__ = { + __vitest_worker__ = { ctx, moduleCache, config, @@ -90,18 +89,3 @@ export async function run(ctx: WorkerContext) { const { run } = await startViteNode(ctx) return run(ctx.files, ctx.config) } - -declare global { - namespace NodeJS { - interface Process { - __vitest_worker__: { - ctx: WorkerContext - config: ResolvedConfig - rpc: BirpcReturn - current?: Test - filepath?: string - moduleCache: Map - } - } - } -} 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 +} diff --git a/test/core/test/basic.test.ts b/test/core/test/basic.test.ts index aa429e7f5326..5a6f85b872db 100644 --- a/test/core/test/basic.test.ts +++ b/test/core/test/basic.test.ts @@ -4,8 +4,8 @@ import { timeout } from '../src/timeout' test('mess process', () => { // eslint-disable-next-line no-global-assign - process = 1 as any - expect(process).toStrictEqual(1) + // process = 1 as any + // expect(process).toStrictEqual(1) }) test('Math.sqrt()', async() => { From 289af1664e25df08c75945f2ca7129ab08cb10b4 Mon Sep 17 00:00:00 2001 From: poyoho <907415276@qq.com> Date: Sat, 12 Feb 2022 16:29:35 +0800 Subject: [PATCH 3/6] fix: global var undefined --- packages/vitest/globals.d.ts | 2 +- packages/vitest/src/runtime/entry.ts | 2 +- packages/vitest/src/runtime/worker.ts | 6 ++++-- test/core/test/basic.test.ts | 6 ------ 4 files changed, 6 insertions(+), 10 deletions(-) diff --git a/packages/vitest/globals.d.ts b/packages/vitest/globals.d.ts index 3e288db32f95..cb095b867b07 100644 --- a/packages/vitest/globals.d.ts +++ b/packages/vitest/globals.d.ts @@ -11,6 +11,6 @@ declare global { const afterAll: typeof import('vitest')['afterAll'] const beforeEach: typeof import('vitest')['beforeEach'] const afterEach: typeof import('vitest')['afterEach'] - const __vitest_worker__: import('vitest').WorkerGlobalState + let __vitest_worker__: import('vitest').WorkerGlobalState } export {} diff --git a/packages/vitest/src/runtime/entry.ts b/packages/vitest/src/runtime/entry.ts index fbb8f1b4808f..cad96f36d89d 100644 --- a/packages/vitest/src/runtime/entry.ts +++ b/packages/vitest/src/runtime/entry.ts @@ -14,7 +14,7 @@ export async function run(files: string[], config: ResolvedConfig): Promise { await startTests([file], config) diff --git a/packages/vitest/src/runtime/worker.ts b/packages/vitest/src/runtime/worker.ts index fa8772abfa49..aac33fd22f73 100644 --- a/packages/vitest/src/runtime/worker.ts +++ b/packages/vitest/src/runtime/worker.ts @@ -1,6 +1,6 @@ import { resolve } from 'pathe' import { createBirpc } from 'birpc' -import type { ModuleCache, ResolvedConfig, 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' @@ -9,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 = {} @@ -59,7 +60,8 @@ function init(ctx: WorkerContext) { const { config, port } = ctx - __vitest_worker__ = { + // @ts-expect-error I know what I am doing :P + globalThis.__vitest_worker__ = { ctx, moduleCache, config, diff --git a/test/core/test/basic.test.ts b/test/core/test/basic.test.ts index 5a6f85b872db..b646ce3220e4 100644 --- a/test/core/test/basic.test.ts +++ b/test/core/test/basic.test.ts @@ -2,12 +2,6 @@ import { assert, expect, it, suite, test } from 'vitest' import { two } from '../src/submodule' import { timeout } from '../src/timeout' -test('mess process', () => { - // eslint-disable-next-line no-global-assign - // process = 1 as any - // expect(process).toStrictEqual(1) -}) - test('Math.sqrt()', async() => { assert.equal(Math.sqrt(4), two) assert.equal(Math.sqrt(2), Math.SQRT2) From 6044f4241567e205d043e248be51ddfc4f66d0fa Mon Sep 17 00:00:00 2001 From: poyoho <907415276@qq.com> Date: Sat, 12 Feb 2022 16:53:08 +0800 Subject: [PATCH 4/6] fix: define the global vari dts in the project namespace --- packages/vitest/globals.d.ts | 1 - packages/vitest/src/runtime/worker.ts | 4 ++++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/packages/vitest/globals.d.ts b/packages/vitest/globals.d.ts index cb095b867b07..dbeb9fca262b 100644 --- a/packages/vitest/globals.d.ts +++ b/packages/vitest/globals.d.ts @@ -11,6 +11,5 @@ declare global { const afterAll: typeof import('vitest')['afterAll'] const beforeEach: typeof import('vitest')['beforeEach'] const afterEach: typeof import('vitest')['afterEach'] - let __vitest_worker__: import('vitest').WorkerGlobalState } export {} diff --git a/packages/vitest/src/runtime/worker.ts b/packages/vitest/src/runtime/worker.ts index aac33fd22f73..d788c16b0f9a 100644 --- a/packages/vitest/src/runtime/worker.ts +++ b/packages/vitest/src/runtime/worker.ts @@ -91,3 +91,7 @@ export async function run(ctx: WorkerContext) { const { run } = await startViteNode(ctx) return run(ctx.files, ctx.config) } + +declare global { + let __vitest_worker__: import('vitest').WorkerGlobalState +} From 1538626d2c7c0a6f58468670a1f0c26f5ca52f6e Mon Sep 17 00:00:00 2001 From: poyoho <907415276@qq.com> Date: Sat, 12 Feb 2022 19:51:00 +0800 Subject: [PATCH 5/6] test: mess process --- packages/vitest/src/runtime/entry.ts | 3 +++ test/core/test/basic.test.ts | 6 ++++++ 2 files changed, 9 insertions(+) diff --git a/packages/vitest/src/runtime/entry.ts b/packages/vitest/src/runtime/entry.ts index cad96f36d89d..93a526963e9a 100644 --- a/packages/vitest/src/runtime/entry.ts +++ b/packages/vitest/src/runtime/entry.ts @@ -17,7 +17,10 @@ export async function run(files: string[], config: ResolvedConfig): Promise { + const cacheProcess = process await startTests([file], config) + // eslint-disable-next-line no-global-assign + process = cacheProcess }) __vitest_worker__.filepath = undefined diff --git a/test/core/test/basic.test.ts b/test/core/test/basic.test.ts index b646ce3220e4..dcc726b00286 100644 --- a/test/core/test/basic.test.ts +++ b/test/core/test/basic.test.ts @@ -2,6 +2,12 @@ import { assert, expect, it, suite, test } from 'vitest' import { two } from '../src/submodule' import { timeout } from '../src/timeout' +test('mess process', () => { + // eslint-disable-next-line no-global-assign + process = 1 as any + expect(process).toBe(1) +}) + test('Math.sqrt()', async() => { assert.equal(Math.sqrt(4), two) assert.equal(Math.sqrt(2), Math.SQRT2) From d4fb7025a3592cbf329924fcd505a2773b6bbdd1 Mon Sep 17 00:00:00 2001 From: poyoho <907415276@qq.com> Date: Sat, 12 Feb 2022 20:43:11 +0800 Subject: [PATCH 6/6] Revert "test: mess process" This reverts commit 1538626d2c7c0a6f58468670a1f0c26f5ca52f6e. --- packages/vitest/src/runtime/entry.ts | 3 --- test/core/test/basic.test.ts | 6 ------ 2 files changed, 9 deletions(-) diff --git a/packages/vitest/src/runtime/entry.ts b/packages/vitest/src/runtime/entry.ts index 93a526963e9a..cad96f36d89d 100644 --- a/packages/vitest/src/runtime/entry.ts +++ b/packages/vitest/src/runtime/entry.ts @@ -17,10 +17,7 @@ export async function run(files: string[], config: ResolvedConfig): Promise { - const cacheProcess = process await startTests([file], config) - // eslint-disable-next-line no-global-assign - process = cacheProcess }) __vitest_worker__.filepath = undefined diff --git a/test/core/test/basic.test.ts b/test/core/test/basic.test.ts index dcc726b00286..b646ce3220e4 100644 --- a/test/core/test/basic.test.ts +++ b/test/core/test/basic.test.ts @@ -2,12 +2,6 @@ import { assert, expect, it, suite, test } from 'vitest' import { two } from '../src/submodule' import { timeout } from '../src/timeout' -test('mess process', () => { - // eslint-disable-next-line no-global-assign - process = 1 as any - expect(process).toBe(1) -}) - test('Math.sqrt()', async() => { assert.equal(Math.sqrt(4), two) assert.equal(Math.sqrt(2), Math.SQRT2)