Skip to content

Commit 631421d

Browse files
committed
fix: update test stats regularly
1 parent 9695d73 commit 631421d

File tree

1 file changed

+18
-16
lines changed

1 file changed

+18
-16
lines changed

packages/runner/src/run.ts

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import type {
1616
Test,
1717
TestContext,
1818
} from './types/tasks'
19-
import { getSafeTimers, shuffle } from '@vitest/utils'
19+
import { shuffle } from '@vitest/utils'
2020
import { processError } from '@vitest/utils/error'
2121
import { collectTests } from './collect'
2222
import { PendingError } from './errors'
@@ -174,26 +174,14 @@ export async function callSuiteHook<T extends keyof SuiteHooks>(
174174

175175
const packs = new Map<string, [TaskResult | undefined, TaskMeta]>()
176176
const eventsPacks: [string, TaskUpdateEvent][] = []
177-
let updateTimer: any
178-
let previousUpdate: Promise<void> | undefined
179177

180178
export function updateTask(event: TaskUpdateEvent, task: Task, runner: VitestRunner): void {
181179
eventsPacks.push([task.id, event])
182180
packs.set(task.id, [task.result, task.meta])
183-
184-
const { clearTimeout, setTimeout } = getSafeTimers()
185-
186-
clearTimeout(updateTimer)
187-
updateTimer = setTimeout(() => {
188-
previousUpdate = sendTasksUpdate(runner)
189-
}, 10)
181+
sendTasksUpdateThrottled(runner)
190182
}
191183

192-
async function sendTasksUpdate(runner: VitestRunner) {
193-
const { clearTimeout } = getSafeTimers()
194-
clearTimeout(updateTimer)
195-
await previousUpdate
196-
184+
function sendTasksUpdate(runner: VitestRunner) {
197185
if (packs.size) {
198186
const taskPacks = Array.from(packs).map<TaskResultPack>(([id, task]) => {
199187
return [id, task[0], task[1]]
@@ -205,6 +193,20 @@ async function sendTasksUpdate(runner: VitestRunner) {
205193
}
206194
}
207195

196+
function throttle<T extends (...args: any[]) => void>(fn: T, ms: number): T {
197+
let last = 0
198+
return function (this: any, ...args: any[]) {
199+
const now = unixNow()
200+
if (now - last > ms) {
201+
last = now
202+
return fn.apply(this, args)
203+
}
204+
} as any
205+
}
206+
207+
// throttle based on summary reporter's DURATION_UPDATE_INTERVAL_MS
208+
const sendTasksUpdateThrottled = throttle(sendTasksUpdate, 100)
209+
208210
async function callCleanupHooks(cleanups: unknown[]) {
209211
await Promise.all(
210212
cleanups.map(async (fn) => {
@@ -561,7 +563,7 @@ export async function startTests(specs: string[] | FileSpecification[], runner:
561563

562564
await runner.onAfterRunFiles?.(files)
563565

564-
await sendTasksUpdate(runner)
566+
sendTasksUpdate(runner)
565567

566568
return files
567569
}

0 commit comments

Comments
 (0)