fix(ui): use execution time from ws reporter (`onFinished`) (#8975) · vitest-dev/vitest@f56dc0c
@@ -15,6 +15,7 @@ import type {
1515WebSocketRPC,
1616} from './types'
1717import { existsSync, promises as fs } from 'node:fs'
18+import { performance } from 'node:perf_hooks'
1819import { noop } from '@vitest/utils/helpers'
1920import { createBirpc } from 'birpc'
2021import { parse, stringify } from 'flatted'
@@ -157,6 +158,8 @@ export function setup(ctx: Vitest, _server?: ViteDevServer): void {
157158}
158159159160export class WebSocketReporter implements Reporter {
161+private start = 0
162+private end = 0
160163constructor(
161164public ctx: Vitest,
162165public wss: WebSocketServer,
@@ -178,8 +181,8 @@ export class WebSocketReporter implements Reporter {
178181return
179182}
180183184+this.start = performance.now()
181185const serializedSpecs = specifications.map(spec => spec.toJSON())
182-183186this.clients.forEach((client) => {
184187client.onSpecsCollected?.(serializedSpecs)?.catch?.(noop)
185188})
@@ -205,6 +208,12 @@ export class WebSocketReporter implements Reporter {
205208})
206209}
207210211+private sum<T>(items: T[], cb: (_next: T) => number | undefined) {
212+return items.reduce((total, next) => {
213+return total + Math.max(cb(next) || 0, 0)
214+}, 0)
215+}
216+208217onTestRunEnd(testModules: ReadonlyArray<TestModule>, unhandledErrors: ReadonlyArray<SerializedError>): void {
209218if (!this.clients.size) {
210219return
@@ -213,8 +222,13 @@ export class WebSocketReporter implements Reporter {
213222const files = testModules.map(testModule => testModule.task)
214223const errors = [...unhandledErrors]
215224225+this.end = performance.now()
226+const blobs = this.ctx.state.blobs
227+// Execution time is either sum of all runs of `--merge-reports` or the current run's time
228+const executionTime = blobs?.executionTimes ? this.sum(blobs.executionTimes, time => time) : this.end - this.start
229+216230this.clients.forEach((client) => {
217-client.onFinished?.(files, errors)?.catch?.(noop)
231+client.onFinished?.(files, errors, undefined, executionTime)?.catch?.(noop)
218232})
219233}
220234