@@ -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'
2020import { processError } from '@vitest/utils/error'
2121import { collectTests } from './collect'
2222import { PendingError } from './errors'
@@ -174,26 +174,14 @@ export async function callSuiteHook<T extends keyof SuiteHooks>(
174174
175175const packs = new Map < string , [ TaskResult | undefined , TaskMeta ] > ( )
176176const eventsPacks : [ string , TaskUpdateEvent ] [ ] = [ ]
177- let updateTimer : any
178- let previousUpdate : Promise < void > | undefined
179177
180178export 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+
208210async 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