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: dot reporter scrollback buffer spam #3415

Merged
merged 4 commits into from May 24, 2023
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
56 changes: 35 additions & 21 deletions packages/vitest/src/node/reporters/renderers/dotRenderer.ts
Expand Up @@ -27,54 +27,68 @@ function getIcon(task: Task) {
}
}

function render(tasks: Task[]): string {
function render(tasks: Task[], width: number): string {
const all = getTests(tasks)
const output: string[] = []
let currentIcon = pending
let currentTasks = 0
let previousLineWidth = 0
let output = ''

// The log-update uses various ANSI helper utilities, e.g. ansi-warp, ansi-slice,
// when printing. Passing it hundreds of single characters containing ANSI codes reduces
// performances. We can optimize it by reducing amount of ANSI codes, e.g. by coloring
// multiple tasks at once instead of each task separately.
let currentIcon = pending
let currentTasks = 0

const addOutput = () => output.push(currentIcon.color(currentIcon.char.repeat(currentTasks)))
const addOutput = () => {
const { char, color } = currentIcon
const availableWidth = width - previousLineWidth
if (availableWidth > currentTasks) {
output += color(char.repeat(currentTasks))
previousLineWidth += currentTasks
}
else {
// We need to split the line otherwise it will mess up log-update's height calculation
// and spam the scrollback buffer with dots.
let buf = `${char.repeat(availableWidth)}\n`
let remaining = currentTasks - availableWidth
buf += `${char.repeat(width)}\n`.repeat(Math.floor(remaining / width))
remaining %= width

if (remaining > 0) {
buf += char.repeat(remaining)
previousLineWidth = remaining
}
else {
previousLineWidth = 0
}
gtm-nayan marked this conversation as resolved.
Show resolved Hide resolved

output += color(buf)
}
}
for (const task of all) {
const icon = getIcon(task)
const isLast = all.indexOf(task) === all.length - 1

if (icon === currentIcon) {
currentTasks++

if (isLast)
addOutput()

continue
}

// Task mode/state has changed, add previous group to output
addOutput()

// Start tracking new group
currentTasks = 1
currentIcon = icon

if (isLast)
addOutput()
}

return output.join('')
addOutput()
return output
}

export function createDotRenderer(_tasks: Task[], options: DotRendererOptions) {
let tasks = _tasks
let timer: any

const log = options.logger.logUpdate
const { logUpdate: log, outputStream } = options.logger

function update() {
log(render(tasks))
log(render(tasks, outputStream.columns))
}

return {
Expand All @@ -94,7 +108,7 @@ export function createDotRenderer(_tasks: Task[], options: DotRendererOptions) {
timer = undefined
}
log.clear()
options.logger.log(render(tasks))
options.logger.log(render(tasks, outputStream.columns))
return this
},
clear() {
Expand Down