From 52d545bf9ccb49bba1e090bbb69cfc5228288bd5 Mon Sep 17 00:00:00 2001 From: Vladimir Date: Thu, 6 Jun 2024 18:41:18 +0200 Subject: [PATCH] fix(browser): show correct prepare time (#5852) --- packages/browser/src/client/runner.ts | 15 ++++++++++++--- packages/browser/src/client/tester.ts | 8 ++++++-- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/packages/browser/src/client/runner.ts b/packages/browser/src/client/runner.ts index 2e154543ffea..0b3f5fde90a2 100644 --- a/packages/browser/src/client/runner.ts +++ b/packages/browser/src/client/runner.ts @@ -1,5 +1,5 @@ import type { File, Task, TaskResultPack, VitestRunner } from '@vitest/runner' -import type { ResolvedConfig } from 'vitest' +import type { ResolvedConfig, WorkerGlobalState } from 'vitest' import type { VitestExecutor } from 'vitest/execute' import { rpc } from './rpc' import { importId } from './utils' @@ -17,6 +17,7 @@ interface CoverageHandler { export function createBrowserRunner( runnerClass: { new(config: ResolvedConfig): VitestRunner }, + state: WorkerGlobalState, coverageModule: CoverageHandler | null, ): { new(options: BrowserRunnerOptions): VitestRunner } { return class BrowserTestRunner extends runnerClass implements VitestRunner { @@ -57,6 +58,14 @@ export function createBrowserRunner( } onCollected = async (files: File[]): Promise => { + files.forEach((file) => { + file.prepareDuration = state.durations.prepare + file.environmentLoad = state.durations.environment + // should be collected only for a single test file in a batch + state.durations.prepare = 0 + state.durations.environment = 0 + }) + if (this.config.includeTaskLocation) { try { await updateFilesLocations(files) @@ -89,7 +98,7 @@ export function createBrowserRunner( let cachedRunner: VitestRunner | null = null -export async function initiateRunner(config: ResolvedConfig) { +export async function initiateRunner(state: WorkerGlobalState, config: ResolvedConfig) { if (cachedRunner) return cachedRunner const [ @@ -100,7 +109,7 @@ export async function initiateRunner(config: ResolvedConfig) { importId('vitest/browser') as Promise, ]) const runnerClass = config.mode === 'test' ? VitestTestRunner : NodeBenchmarkRunner - const BrowserRunner = createBrowserRunner(runnerClass, { + const BrowserRunner = createBrowserRunner(runnerClass, state, { takeCoverage: () => takeCoverageInsideWorker(config.coverage, { executeId: importId }), }) if (!config.snapshotOptions.snapshotEnvironment) diff --git a/packages/browser/src/client/tester.ts b/packages/browser/src/client/tester.ts index 2752a3ebf067..a2486a4df13d 100644 --- a/packages/browser/src/client/tester.ts +++ b/packages/browser/src/client/tester.ts @@ -54,6 +54,8 @@ async function tryCall(fn: () => Promise): Promise } } +const startTime = performance.now() + async function prepareTestEnvironment(files: string[]) { debug('trying to resolve runner', `${reloadStart}`) const config = getConfig() @@ -94,7 +96,7 @@ async function prepareTestEnvironment(files: string[]) { rpc, durations: { environment: 0, - prepare: performance.now(), + prepare: startTime, }, providedContext, } @@ -117,7 +119,7 @@ async function prepareTestEnvironment(files: string[]) { }) const [runner, { startTests, setupCommonEnv, Spy }] = await Promise.all([ - initiateRunner(config), + initiateRunner(state, config), importId('vitest/browser') as Promise, ]) @@ -184,6 +186,8 @@ async function runTests(files: string[]) { state.durations.prepare = performance.now() - state.durations.prepare + debug('prepare time', state.durations.prepare, 'ms') + try { await setupCommonEnv(config) for (const file of files)