Skip to content

Commit 52d545b

Browse files
authored
fix(browser): show correct prepare time (#5852)
1 parent b117e87 commit 52d545b

File tree

2 files changed

+18
-5
lines changed

2 files changed

+18
-5
lines changed

packages/browser/src/client/runner.ts

+12-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { File, Task, TaskResultPack, VitestRunner } from '@vitest/runner'
2-
import type { ResolvedConfig } from 'vitest'
2+
import type { ResolvedConfig, WorkerGlobalState } from 'vitest'
33
import type { VitestExecutor } from 'vitest/execute'
44
import { rpc } from './rpc'
55
import { importId } from './utils'
@@ -17,6 +17,7 @@ interface CoverageHandler {
1717

1818
export function createBrowserRunner(
1919
runnerClass: { new(config: ResolvedConfig): VitestRunner },
20+
state: WorkerGlobalState,
2021
coverageModule: CoverageHandler | null,
2122
): { new(options: BrowserRunnerOptions): VitestRunner } {
2223
return class BrowserTestRunner extends runnerClass implements VitestRunner {
@@ -57,6 +58,14 @@ export function createBrowserRunner(
5758
}
5859

5960
onCollected = async (files: File[]): Promise<unknown> => {
61+
files.forEach((file) => {
62+
file.prepareDuration = state.durations.prepare
63+
file.environmentLoad = state.durations.environment
64+
// should be collected only for a single test file in a batch
65+
state.durations.prepare = 0
66+
state.durations.environment = 0
67+
})
68+
6069
if (this.config.includeTaskLocation) {
6170
try {
6271
await updateFilesLocations(files)
@@ -89,7 +98,7 @@ export function createBrowserRunner(
8998

9099
let cachedRunner: VitestRunner | null = null
91100

92-
export async function initiateRunner(config: ResolvedConfig) {
101+
export async function initiateRunner(state: WorkerGlobalState, config: ResolvedConfig) {
93102
if (cachedRunner)
94103
return cachedRunner
95104
const [
@@ -100,7 +109,7 @@ export async function initiateRunner(config: ResolvedConfig) {
100109
importId('vitest/browser') as Promise<typeof import('vitest/browser')>,
101110
])
102111
const runnerClass = config.mode === 'test' ? VitestTestRunner : NodeBenchmarkRunner
103-
const BrowserRunner = createBrowserRunner(runnerClass, {
112+
const BrowserRunner = createBrowserRunner(runnerClass, state, {
104113
takeCoverage: () => takeCoverageInsideWorker(config.coverage, { executeId: importId }),
105114
})
106115
if (!config.snapshotOptions.snapshotEnvironment)

packages/browser/src/client/tester.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ async function tryCall<T>(fn: () => Promise<T>): Promise<T | false | undefined>
5454
}
5555
}
5656

57+
const startTime = performance.now()
58+
5759
async function prepareTestEnvironment(files: string[]) {
5860
debug('trying to resolve runner', `${reloadStart}`)
5961
const config = getConfig()
@@ -94,7 +96,7 @@ async function prepareTestEnvironment(files: string[]) {
9496
rpc,
9597
durations: {
9698
environment: 0,
97-
prepare: performance.now(),
99+
prepare: startTime,
98100
},
99101
providedContext,
100102
}
@@ -117,7 +119,7 @@ async function prepareTestEnvironment(files: string[]) {
117119
})
118120

119121
const [runner, { startTests, setupCommonEnv, Spy }] = await Promise.all([
120-
initiateRunner(config),
122+
initiateRunner(state, config),
121123
importId('vitest/browser') as Promise<typeof import('vitest/browser')>,
122124
])
123125

@@ -184,6 +186,8 @@ async function runTests(files: string[]) {
184186

185187
state.durations.prepare = performance.now() - state.durations.prepare
186188

189+
debug('prepare time', state.durations.prepare, 'ms')
190+
187191
try {
188192
await setupCommonEnv(config)
189193
for (const file of files)

0 commit comments

Comments
 (0)