Skip to content

browser: prepareDuration is not a duration (≈ orchestrator startTime), so the "worker" share in the summary grows with run length #11651

Closed

Description

@nikolailehbrink

Describe the bug

In browser mode, TestModule.diagnostic().prepareDuration is not the duration of the prepare phase. The value is approximately options.startTime: the orchestrator's performance.now() at the moment the file was handed to a tester iframe, i.e. the time elapsed since the orchestrator page was loaded. It grows with every file a browser session runs.

The Duration line of the default reporter sums prepareDuration over all files and prints it as worker, so the "worker" share is inflated by an amount that depends on the run length, not on the cost of preparing the test harness.

Expected (see ModuleDiagnostic.prepareDuration and the "Prepare" entry in the profiling guide): the time Vitest needs to set up the harness for a file (runner, mocks, iframe), which is in the order of tens to a few hundred milliseconds in the repro below.

Reproduction

Minimal setup with the Playwright provider, default isolate, and maxWorkers: 1 so the files run one after another in one browser session (with more workers every session has its own orchestrator page and the value grows per session in the same way).

package.json (dev dependencies): vitest@5.0.5, @vitest/browser-playwright@5.0.5, playwright@1.62.1, vite@8.3.4

vitest.config.ts

import { playwright } from '@vitest/browser-playwright'
import { defineConfig } from 'vitest/config'

export default defineConfig({
  test: {
    maxWorkers: 1,
    reporters: ['default', './reporter.ts'],
    browser: {
      enabled: true,
      headless: true,
      provider: playwright(),
      instances: [{ browser: 'chromium' }],
    },
  },
})

reporter.ts

import type { Reporter, TestModule } from 'vitest/node'

export default class DiagnosticReporter implements Reporter {
  onTestModuleEnd(testModule: TestModule) {
    const d = testModule.diagnostic()
    console.log(
      `${testModule.moduleId.split('/').pop()?.padEnd(12)} prepareDuration=${d.prepareDuration.toFixed(0).padStart(6)} ms` +
        `  collect=${d.collectDuration.toFixed(0).padStart(4)} ms  duration=${d.duration.toFixed(0).padStart(5)} ms`,
    )
  }
}

file1.test.ts … file6.test.ts (identical)

import { expect, test } from 'vitest'

test('waits a bit', async () => {
  await new Promise((resolve) => setTimeout(resolve, 2000))
  expect(1).toBe(1)
})

Run npx vitest run.

Actual

file1.test.ts prepareDuration=   120 ms  collect=   3 ms  duration= 2005 ms
file2.test.ts prepareDuration=  2483 ms  collect=   3 ms  duration= 2002 ms
file3.test.ts prepareDuration=  4542 ms  collect=   2 ms  duration= 2001 ms
file4.test.ts prepareDuration=  6580 ms  collect=   2 ms  duration= 2001 ms
file5.test.ts prepareDuration=  8617 ms  collect=   3 ms  duration= 2002 ms
file6.test.ts prepareDuration= 10657 ms  collect=   2 ms  duration= 2002 ms

   Duration  12.86s (worker 73%, tests 27%)

prepareDuration increases by about the length of the previous file (≈ 2 s) for each file, although nothing in the preparation of a file got slower. The run is 12.9 s long and 12 s of it are the six 2 s tests, yet the summary attributes 73 % to worker. A run with only file1.test.ts reports tests 95%, worker 5%.

The same happens with vitest@5.0.1 and @vitest/browser-playwright@5.0.1 (prepareDuration 111, 2441, 4493, 6532, 8570, 10611 ms; Duration 12.77s (worker 73%, tests 27%)) and with 5.1.0-beta.1 (108, 2393, 4425, 6459, 8492, 10525 ms; Duration 12.68s (worker 73%, tests 27%)).

In a larger run (42 test files, 2 workers, 73 s) the summed prepareDuration was 2,212 s and the summary read worker 94 %; the first file of a session reported 0.3 s and the last one 72.5 s.

Cause

Three places are involved (permalinks to e1b0d33f, current main):

  1. The orchestrator takes the timestamp once per run call, with its own document's clock and sends it with the prepare event: orchestrator.ts#L81 (const startTime = performance.now()).
  2. The tester computes the first value with the clock of the iframe document: tester.ts#L204 (state.durations.prepare = performance.now() - options.startTime). performance.now() is relative to the time origin of each document, so the orchestrator's and the iframe's values are not on the same scale.
  3. prepare() then treats that result as a start timestamp and subtracts it again: tester.ts#L267 (state.durations.prepare = performance.now() - state.durations.prepare).

With t1 (step 2) and t2 (step 3) as the iframe's performance.now(), the final value is t2 - (t1 - startTime) = startTime + (t2 - t1), which is dominated by startTime.

The Node pools use the same subtraction in runBaseTests.ts, but there durations.prepare is initialised with a start timestamp, so it is a single start→end pair. In the browser the first assignment already produces a duration, so the second subtraction should not be there.

Suggested fix

Measure once, with one clock for both sides, at the end of the prepare phase:

  • the orchestrator sends an absolute timestamp, performance.timeOrigin + performance.now();
  • the tester computes performance.timeOrigin + performance.now() - options.startTime once (at the end of prepare() if setup of the environment, coverage and the vitest import should be part of "prepare") and no longer subtracts state.durations.prepare in prepare().

Patching these lines in the built @vitest/browser@5.0.5 files of the repro above gives:

file1.test.ts prepareDuration=   306 ms
file2.test.ts prepareDuration=    29 ms
file3.test.ts prepareDuration=    29 ms
file4.test.ts prepareDuration=    33 ms
file5.test.ts prepareDuration=    28 ms
file6.test.ts prepareDuration=    39 ms

   Duration  12.78s (tests 96%, worker 4%)

(first file pays the cold start, later files are cheap, and the summary matches the actual time split).

System Info

vitest                     5.0.5 (also 5.0.1 and 5.1.0-beta.1)
@vitest/browser-playwright 5.0.5 (also 5.0.1 and 5.1.0-beta.1)
playwright                 1.62.1 (Chromium, headless)
vite                       8.3.4 (first observed with Vite 8 through vite-plus 1.0)
Node.js                    24.22.0
OS                         Windows 11 (10.0.26200)
Package manager            npm 11.19.1

Used Package Manager

npm

Activity

  1. added
    p3-minor-bug
    on Oct 20, 2026
  2. locked and limited conversation to collaborators on Nov 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    p3-minor-bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions