Skip to content

Commit

Permalink
fix(browser): show correct prepare time (#5852)
Browse files Browse the repository at this point in the history
  • Loading branch information
sheremet-va committed Jun 6, 2024
1 parent b117e87 commit 52d545b
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
15 changes: 12 additions & 3 deletions packages/browser/src/client/runner.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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 {
Expand Down Expand Up @@ -57,6 +58,14 @@ export function createBrowserRunner(
}

onCollected = async (files: File[]): Promise<unknown> => {
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)
Expand Down Expand Up @@ -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 [
Expand All @@ -100,7 +109,7 @@ export async function initiateRunner(config: ResolvedConfig) {
importId('vitest/browser') as Promise<typeof import('vitest/browser')>,
])
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)
Expand Down
8 changes: 6 additions & 2 deletions packages/browser/src/client/tester.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ async function tryCall<T>(fn: () => Promise<T>): Promise<T | false | undefined>
}
}

const startTime = performance.now()

async function prepareTestEnvironment(files: string[]) {
debug('trying to resolve runner', `${reloadStart}`)
const config = getConfig()
Expand Down Expand Up @@ -94,7 +96,7 @@ async function prepareTestEnvironment(files: string[]) {
rpc,
durations: {
environment: 0,
prepare: performance.now(),
prepare: startTime,
},
providedContext,
}
Expand All @@ -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<typeof import('vitest/browser')>,
])

Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 52d545b

Please sign in to comment.