Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions packages/vitest/src/runtime/listeners.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import type { ModuleRunner } from 'vite/module-runner'

const cleanupListeners = new Set<() => unknown>()
const moduleRunnerListeners = new Set<(runner: ModuleRunner) => unknown>()

export function onCleanup(cb: () => unknown): void {
cleanupListeners.add(cb)
}

export async function cleanup(): Promise<void> {
await Promise.all([...cleanupListeners].map(l => l()))
}

export function onModuleRunner(cb: (runner: ModuleRunner) => unknown): void {
moduleRunnerListeners.add(cb)
}

export function emitModuleRunner(moduleRunner: ModuleRunner): void {
moduleRunnerListeners.forEach(l => l(moduleRunner))
}
6 changes: 3 additions & 3 deletions packages/vitest/src/runtime/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ import type { Traces } from '../utils/traces'
import type { VitestWorker } from './workers/types'
import { createStackString, parseStacktrace } from '@vitest/utils/source-map'
import { setupInspect } from './inspector'
import * as listeners from './listeners'
import { VitestEvaluatedModules } from './moduleRunner/evaluatedModules'
import { onCancel, rpcDone } from './rpc'

const resolvingModules = new Set<string>()
const globalListeners = new Set<() => unknown>()

async function execute(method: 'run' | 'collect', ctx: ContextRPC, worker: VitestWorker, traces: Traces) {
const prepareStart = performance.now()
Expand Down Expand Up @@ -40,7 +40,7 @@ async function execute(method: 'run' | 'collect', ctx: ContextRPC, worker: Vites
},
rpc,
onCancel,
onCleanup: listener => globalListeners.add(listener),
onCleanup: listeners.onCleanup,
providedContext: ctx.providedContext,
onFilterStackTrace(stack) {
return createStackString(parseStacktrace(stack))
Expand Down Expand Up @@ -73,7 +73,7 @@ export function collect(ctx: ContextRPC, worker: VitestWorker, traces: Traces):
}

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

const env = process.env
Expand Down
3 changes: 3 additions & 0 deletions packages/vitest/src/runtime/workers/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { runInThisContext } from 'node:vm'
import * as spyModule from '@vitest/spy'
import { setupChaiConfig } from '../../integrations/chai/config'
import { loadEnvironment } from '../../integrations/env/loader'
import { emitModuleRunner } from '../listeners'
import { VitestEvaluatedModules } from '../moduleRunner/evaluatedModules'
import { createNodeImportMeta } from '../moduleRunner/moduleRunner'
import { startVitestModuleRunner } from '../moduleRunner/startModuleRunner'
Expand Down Expand Up @@ -116,6 +117,8 @@ export async function runBaseTests(method: 'run' | 'collect', state: WorkerGloba
traces,
})

emitModuleRunner(moduleRunner as any)

await run(
method,
ctx.files,
Expand Down
4 changes: 4 additions & 0 deletions packages/vitest/src/runtime/workers/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type { WorkerSetupContext } from '../../types/worker'
import type { VitestWorker } from './types'
import { serializeError } from '@vitest/utils/error'
import { Traces } from '../../utils/traces'
import * as listeners from '../listeners'
import { createRuntimeRpc } from '../rpc'
import * as entrypoint from '../worker'

Expand All @@ -20,6 +21,9 @@ let traces!: Traces
/** @experimental */
export function init(worker: Options): void {
worker.on(onMessage)
if (worker.onModuleRunner) {
listeners.onModuleRunner(worker.onModuleRunner)
}

let runPromise: Promise<unknown> | undefined
let isRunning = false
Expand Down
3 changes: 2 additions & 1 deletion packages/vitest/src/runtime/workers/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { Awaitable } from '@vitest/utils'
import type { BirpcOptions } from 'birpc'
import type { ModuleRunner } from 'vite/module-runner'
import type { RuntimeRPC } from '../../types/rpc'
import type { WorkerGlobalState, WorkerSetupContext } from '../../types/worker'
import type { Traces } from '../../utils/traces'
Expand All @@ -12,6 +13,6 @@ type WorkerRpcOptions = Pick<
export interface VitestWorker extends WorkerRpcOptions {
runTests: (state: WorkerGlobalState, traces: Traces) => Awaitable<unknown>
collectTests: (state: WorkerGlobalState, traces: Traces) => Awaitable<unknown>

onModuleRunner?: (moduleRunner: ModuleRunner) => Awaitable<unknown>
setup?: (context: WorkerSetupContext) => Promise<() => Promise<unknown>>
}
10 changes: 4 additions & 6 deletions packages/vitest/src/runtime/workers/vm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { loadEnvironment } from '../../integrations/env/loader'
import { distDir } from '../../paths'
import { createCustomConsole } from '../console'
import { ExternalModulesExecutor } from '../external-executor'
import { emitModuleRunner } from '../listeners'
import { getDefaultRequestStubs } from '../moduleRunner/moduleEvaluator'
import { createNodeImportMeta } from '../moduleRunner/moduleRunner'
import { startVitestModuleRunner, VITEST_VM_CONTEXT_SYMBOL } from '../moduleRunner/startModuleRunner'
Expand Down Expand Up @@ -98,6 +99,8 @@ export async function runVmTests(method: 'run' | 'collect', state: WorkerGlobalS
traces,
})

emitModuleRunner(moduleRunner as any)

Object.defineProperty(context, VITEST_VM_CONTEXT_SYMBOL, {
value: {
context,
Expand All @@ -124,16 +127,11 @@ export async function runVmTests(method: 'run' | 'collect', state: WorkerGlobalS
const { run } = (await moduleRunner.import(
entryFile,
)) as typeof import('../runVmTests')
const fileSpecs = ctx.files.map(f =>
typeof f === 'string'
? { filepath: f, testLocations: undefined }
: f,
)

try {
await run(
method,
fileSpecs,
ctx.files,
ctx.config,
moduleRunner,
traces,
Expand Down
Loading