fix(ui): use execution time from ws reporter (`onFinished`) (#8975) · vitest-dev/vitest@f56dc0c

@@ -15,6 +15,7 @@ import type {

1515

WebSocketRPC,

1616

} from './types'

1717

import { existsSync, promises as fs } from 'node:fs'

18+

import { performance } from 'node:perf_hooks'

1819

import { noop } from '@vitest/utils/helpers'

1920

import { createBirpc } from 'birpc'

2021

import { parse, stringify } from 'flatted'

@@ -157,6 +158,8 @@ export function setup(ctx: Vitest, _server?: ViteDevServer): void {

157158

}

158159159160

export class WebSocketReporter implements Reporter {

161+

private start = 0

162+

private end = 0

160163

constructor(

161164

public ctx: Vitest,

162165

public wss: WebSocketServer,

@@ -178,8 +181,8 @@ export class WebSocketReporter implements Reporter {

178181

return

179182

}

180183184+

this.start = performance.now()

181185

const serializedSpecs = specifications.map(spec => spec.toJSON())

182-183186

this.clients.forEach((client) => {

184187

client.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+208217

onTestRunEnd(testModules: ReadonlyArray<TestModule>, unhandledErrors: ReadonlyArray<SerializedError>): void {

209218

if (!this.clients.size) {

210219

return

@@ -213,8 +222,13 @@ export class WebSocketReporter implements Reporter {

213222

const files = testModules.map(testModule => testModule.task)

214223

const 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+216230

this.clients.forEach((client) => {

217-

client.onFinished?.(files, errors)?.catch?.(noop)

231+

client.onFinished?.(files, errors, undefined, executionTime)?.catch?.(noop)

218232

})

219233

}

220234