Skip to content

Commit e977f3d

Browse files
authored
feat(experimental): add onModuleRunner hook to worker.init (#9286)
1 parent 2f367fa commit e977f3d

File tree

6 files changed

+36
-10
lines changed

6 files changed

+36
-10
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import type { ModuleRunner } from 'vite/module-runner'
2+
3+
const cleanupListeners = new Set<() => unknown>()
4+
const moduleRunnerListeners = new Set<(runner: ModuleRunner) => unknown>()
5+
6+
export function onCleanup(cb: () => unknown): void {
7+
cleanupListeners.add(cb)
8+
}
9+
10+
export async function cleanup(): Promise<void> {
11+
await Promise.all([...cleanupListeners].map(l => l()))
12+
}
13+
14+
export function onModuleRunner(cb: (runner: ModuleRunner) => unknown): void {
15+
moduleRunnerListeners.add(cb)
16+
}
17+
18+
export function emitModuleRunner(moduleRunner: ModuleRunner): void {
19+
moduleRunnerListeners.forEach(l => l(moduleRunner))
20+
}

packages/vitest/src/runtime/worker.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ import type { Traces } from '../utils/traces'
33
import type { VitestWorker } from './workers/types'
44
import { createStackString, parseStacktrace } from '@vitest/utils/source-map'
55
import { setupInspect } from './inspector'
6+
import * as listeners from './listeners'
67
import { VitestEvaluatedModules } from './moduleRunner/evaluatedModules'
78
import { onCancel, rpcDone } from './rpc'
89

910
const resolvingModules = new Set<string>()
10-
const globalListeners = new Set<() => unknown>()
1111

1212
async function execute(method: 'run' | 'collect', ctx: ContextRPC, worker: VitestWorker, traces: Traces) {
1313
const prepareStart = performance.now()
@@ -40,7 +40,7 @@ async function execute(method: 'run' | 'collect', ctx: ContextRPC, worker: Vites
4040
},
4141
rpc,
4242
onCancel,
43-
onCleanup: listener => globalListeners.add(listener),
43+
onCleanup: listeners.onCleanup,
4444
providedContext: ctx.providedContext,
4545
onFilterStackTrace(stack) {
4646
return createStackString(parseStacktrace(stack))
@@ -73,7 +73,7 @@ export function collect(ctx: ContextRPC, worker: VitestWorker, traces: Traces):
7373
}
7474

7575
export async function teardown(): Promise<void> {
76-
await Promise.all([...globalListeners].map(l => l()))
76+
await listeners.cleanup()
7777
}
7878

7979
const env = process.env

packages/vitest/src/runtime/workers/base.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { runInThisContext } from 'node:vm'
77
import * as spyModule from '@vitest/spy'
88
import { setupChaiConfig } from '../../integrations/chai/config'
99
import { loadEnvironment } from '../../integrations/env/loader'
10+
import { emitModuleRunner } from '../listeners'
1011
import { VitestEvaluatedModules } from '../moduleRunner/evaluatedModules'
1112
import { createNodeImportMeta } from '../moduleRunner/moduleRunner'
1213
import { startVitestModuleRunner } from '../moduleRunner/startModuleRunner'
@@ -116,6 +117,8 @@ export async function runBaseTests(method: 'run' | 'collect', state: WorkerGloba
116117
traces,
117118
})
118119

120+
emitModuleRunner(moduleRunner as any)
121+
119122
await run(
120123
method,
121124
ctx.files,

packages/vitest/src/runtime/workers/init.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import type { WorkerSetupContext } from '../../types/worker'
44
import type { VitestWorker } from './types'
55
import { serializeError } from '@vitest/utils/error'
66
import { Traces } from '../../utils/traces'
7+
import * as listeners from '../listeners'
78
import { createRuntimeRpc } from '../rpc'
89
import * as entrypoint from '../worker'
910

@@ -20,6 +21,9 @@ let traces!: Traces
2021
/** @experimental */
2122
export function init(worker: Options): void {
2223
worker.on(onMessage)
24+
if (worker.onModuleRunner) {
25+
listeners.onModuleRunner(worker.onModuleRunner)
26+
}
2327

2428
let runPromise: Promise<unknown> | undefined
2529
let isRunning = false

packages/vitest/src/runtime/workers/types.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { Awaitable } from '@vitest/utils'
22
import type { BirpcOptions } from 'birpc'
3+
import type { ModuleRunner } from 'vite/module-runner'
34
import type { RuntimeRPC } from '../../types/rpc'
45
import type { WorkerGlobalState, WorkerSetupContext } from '../../types/worker'
56
import type { Traces } from '../../utils/traces'
@@ -12,6 +13,6 @@ type WorkerRpcOptions = Pick<
1213
export interface VitestWorker extends WorkerRpcOptions {
1314
runTests: (state: WorkerGlobalState, traces: Traces) => Awaitable<unknown>
1415
collectTests: (state: WorkerGlobalState, traces: Traces) => Awaitable<unknown>
15-
16+
onModuleRunner?: (moduleRunner: ModuleRunner) => Awaitable<unknown>
1617
setup?: (context: WorkerSetupContext) => Promise<() => Promise<unknown>>
1718
}

packages/vitest/src/runtime/workers/vm.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { loadEnvironment } from '../../integrations/env/loader'
88
import { distDir } from '../../paths'
99
import { createCustomConsole } from '../console'
1010
import { ExternalModulesExecutor } from '../external-executor'
11+
import { emitModuleRunner } from '../listeners'
1112
import { getDefaultRequestStubs } from '../moduleRunner/moduleEvaluator'
1213
import { createNodeImportMeta } from '../moduleRunner/moduleRunner'
1314
import { startVitestModuleRunner, VITEST_VM_CONTEXT_SYMBOL } from '../moduleRunner/startModuleRunner'
@@ -98,6 +99,8 @@ export async function runVmTests(method: 'run' | 'collect', state: WorkerGlobalS
9899
traces,
99100
})
100101

102+
emitModuleRunner(moduleRunner as any)
103+
101104
Object.defineProperty(context, VITEST_VM_CONTEXT_SYMBOL, {
102105
value: {
103106
context,
@@ -124,16 +127,11 @@ export async function runVmTests(method: 'run' | 'collect', state: WorkerGlobalS
124127
const { run } = (await moduleRunner.import(
125128
entryFile,
126129
)) as typeof import('../runVmTests')
127-
const fileSpecs = ctx.files.map(f =>
128-
typeof f === 'string'
129-
? { filepath: f, testLocations: undefined }
130-
: f,
131-
)
132130

133131
try {
134132
await run(
135133
method,
136-
fileSpecs,
134+
ctx.files,
137135
ctx.config,
138136
moduleRunner,
139137
traces,

0 commit comments

Comments
 (0)