Skip to content

Commit 2316cf1

Browse files
committed
fix(benchmark): prevent runner reporting passed tests twice
1 parent 8badc6a commit 2316cf1

File tree

3 files changed

+18
-11
lines changed

3 files changed

+18
-11
lines changed

packages/vitest/src/node/reporters/benchmark/reporter.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { Task } from '@vitest/runner'
1+
import type { Task, TaskResultPack } from '@vitest/runner'
22
import type { Vitest } from '../../core'
33
import fs from 'node:fs'
44
import { getFullName } from '@vitest/runner/utils'
@@ -31,6 +31,22 @@ export class BenchmarkReporter extends DefaultReporter {
3131
}
3232
}
3333

34+
onTaskUpdate(packs: TaskResultPack[]): void {
35+
for (const pack of packs) {
36+
const task = this.ctx.state.idMap.get(pack[0])
37+
38+
if (task?.type === 'suite' && task.result?.state !== 'run') {
39+
task.tasks.filter(task => task.result?.benchmark)
40+
.sort((benchA, benchB) => benchA.result!.benchmark!.mean - benchB.result!.benchmark!.mean)
41+
.forEach((bench, idx) => {
42+
bench.result!.benchmark!.rank = Number(idx) + 1
43+
})
44+
}
45+
}
46+
47+
super.onTaskUpdate(packs)
48+
}
49+
3450
printTask(task: Task) {
3551
if (task?.type !== 'suite' || !task.result?.state || task.result?.state === 'run') {
3652
return

packages/vitest/src/runtime/runners/benchmark.ts

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -137,16 +137,6 @@ async function runBenchmarkSuite(suite: Suite, runner: NodeBenchmarkRunner) {
137137
suite.result!.duration = performance.now() - start
138138
suite.result!.state = 'pass'
139139

140-
tasks
141-
.sort(([taskA], [taskB]) => taskA.result!.mean - taskB.result!.mean)
142-
.forEach(([, benchmark], idx) => {
143-
benchmark.result!.state = 'pass'
144-
if (benchmark) {
145-
const result = benchmark.result!.benchmark!
146-
result.rank = Number(idx) + 1
147-
updateTask(benchmark)
148-
}
149-
})
150140
updateTask(suite)
151141
defer.resolve(null)
152142

test/benchmark/test/basic.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ it('basic', { timeout: 60_000 }, async () => {
1717
// Verify that type testing cannot be used with benchmark
1818
typecheck: { enabled: true },
1919
}, [], 'benchmark')
20+
expect(result.stderr).toBe('')
2021
expect(result.exitCode).toBe(0)
2122

2223
const benchResult = await fs.promises.readFile(benchFile, 'utf-8')

0 commit comments

Comments
 (0)