Skip to content

Commit

Permalink
fix(benchmark): prevent runner reporting passed tests twice
Browse files Browse the repository at this point in the history
  • Loading branch information
AriPerkkio committed Dec 13, 2024
1 parent d4223fd commit 9ed93b5
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 11 deletions.
18 changes: 17 additions & 1 deletion packages/vitest/src/node/reporters/benchmark/reporter.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Task } from '@vitest/runner'
import type { Task, TaskResultPack } from '@vitest/runner'
import type { Vitest } from '../../core'
import fs from 'node:fs'
import { getFullName } from '@vitest/runner/utils'
Expand Down Expand Up @@ -31,6 +31,22 @@ export class BenchmarkReporter extends DefaultReporter {
}
}

onTaskUpdate(packs: TaskResultPack[]): void {
for (const pack of packs) {
const task = this.ctx.state.idMap.get(pack[0])

if (task?.type === 'suite' && task.result?.state !== 'run') {
task.tasks.filter(task => task.result?.benchmark)
.sort((benchA, benchB) => benchA.result!.benchmark!.mean - benchB.result!.benchmark!.mean)
.forEach((bench, idx) => {
bench.result!.benchmark!.rank = Number(idx) + 1
})
}
}

super.onTaskUpdate(packs)
}

printTask(task: Task) {
if (task?.type !== 'suite' || !task.result?.state || task.result?.state === 'run') {
return
Expand Down
10 changes: 0 additions & 10 deletions packages/vitest/src/runtime/runners/benchmark.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,16 +137,6 @@ async function runBenchmarkSuite(suite: Suite, runner: NodeBenchmarkRunner) {
suite.result!.duration = performance.now() - start
suite.result!.state = 'pass'

tasks
.sort(([taskA], [taskB]) => taskA.result!.mean - taskB.result!.mean)
.forEach(([, benchmark], idx) => {
benchmark.result!.state = 'pass'
if (benchmark) {
const result = benchmark.result!.benchmark!
result.rank = Number(idx) + 1
updateTask(benchmark)
}
})
updateTask(suite)
defer.resolve(null)

Expand Down
1 change: 1 addition & 0 deletions test/benchmark/test/basic.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ it('basic', { timeout: 60_000 }, async () => {
// Verify that type testing cannot be used with benchmark
typecheck: { enabled: true },
}, [], 'benchmark')
expect(result.stderr).toBe('')
expect(result.exitCode).toBe(0)

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

0 comments on commit 9ed93b5

Please sign in to comment.