Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: update test stats regularly #7700

Merged
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 23 additions & 21 deletions packages/runner/src/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import type {
Test,
TestContext,
} from './types/tasks'
import { getSafeTimers, shuffle } from '@vitest/utils'
import { shuffle } from '@vitest/utils'
import { processError } from '@vitest/utils/error'
import { collectTests } from './collect'
import { PendingError } from './errors'
Expand Down Expand Up @@ -174,26 +174,8 @@ export async function callSuiteHook<T extends keyof SuiteHooks>(

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

export function updateTask(event: TaskUpdateEvent, task: Task, runner: VitestRunner): void {
eventsPacks.push([task.id, event])
packs.set(task.id, [task.result, task.meta])

const { clearTimeout, setTimeout } = getSafeTimers()

clearTimeout(updateTimer)
updateTimer = setTimeout(() => {
previousUpdate = sendTasksUpdate(runner)
}, 10)
}

async function sendTasksUpdate(runner: VitestRunner) {
const { clearTimeout } = getSafeTimers()
clearTimeout(updateTimer)
await previousUpdate

function sendTasksUpdate(runner: VitestRunner) {
if (packs.size) {
const taskPacks = Array.from(packs).map<TaskResultPack>(([id, task]) => {
return [id, task[0], task[1]]
Expand All @@ -205,6 +187,26 @@ async function sendTasksUpdate(runner: VitestRunner) {
}
}

function throttle<T extends (...args: any[]) => void>(fn: T, ms: number): T {
let last = 0
return function (this: any, ...args: any[]) {
const now = unixNow()
if (now - last > ms) {
last = now
return fn.apply(this, args)
}
} as any
}

// throttle based on summary reporter's DURATION_UPDATE_INTERVAL_MS
const sendTasksUpdateThrottled = throttle(sendTasksUpdate, 100)

export function updateTask(event: TaskUpdateEvent, task: Task, runner: VitestRunner): void {
eventsPacks.push([task.id, event])
packs.set(task.id, [task.result, task.meta])
sendTasksUpdateThrottled(runner)
}

async function callCleanupHooks(cleanups: unknown[]) {
await Promise.all(
cleanups.map(async (fn) => {
Expand Down Expand Up @@ -561,7 +563,7 @@ export async function startTests(specs: string[] | FileSpecification[], runner:

await runner.onAfterRunFiles?.(files)

await sendTasksUpdate(runner)
sendTasksUpdate(runner)

return files
}
Expand Down