Skip to content

Commit 5ab201e

Browse files
fix: prevent duplicate bench result table for top-level bench() calls
Fixes #9718 When bench() is used at the top level of a file (outside any describe()), runBenchmarkSuite emitted suite-finished for the file root after running its benchmark tasks. The general runSuite wrapper that calls runner.runSuite() also unconditionally emits suite-finished for the file after returning, resulting in two suite-finished events → two onTestModuleEnd calls → two printSuiteTable invocations. Skip emitting suite-finished in runBenchmarkSuite when suite.file === suite (i.e. the suite is the file root). The general wrapper already handles this event at the file level. For describe-level suites, suite.file !== suite, so they continue to emit normally.
1 parent 36a6fd8 commit 5ab201e

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,13 @@ async function runBenchmarkSuite(suite: Suite, runner: NodeBenchmarkRunner) {
140140
suite.result!.duration = performance.now() - start
141141
suite.result!.state = 'pass'
142142

143-
updateTask('suite-finished', suite)
143+
// Don't emit suite-finished for the root file suite here. The general runSuite
144+
// wrapper that calls runner.runSuite() will emit suite-finished for the file
145+
// after this returns. Emitting it here would cause a duplicate event for files
146+
// with top-level bench() calls (i.e. bench() outside any describe()).
147+
if (suite.file !== suite) {
148+
updateTask('suite-finished', suite)
149+
}
144150
defer.resolve(null)
145151

146152
await defer

0 commit comments

Comments
 (0)