Skip to content

Commit

Permalink
perf(reporters): reduce amount of printed rows during test run
Browse files Browse the repository at this point in the history
  • Loading branch information
AriPerkkio committed Mar 13, 2023
1 parent 1fe8286 commit 5bd2d2f
Showing 1 changed file with 32 additions and 15 deletions.
47 changes: 32 additions & 15 deletions packages/vitest/src/node/reporters/renderers/listRenderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ function formatNumber(number: number) {
+ (res[1] ? `.${res[1]}` : '')
}

function renderHookState(task: Task, hookName: keyof SuiteHooks, level = 0) {
function renderHookState(task: Task, hookName: keyof SuiteHooks, level = 0): string {
const state = task.result?.hooks?.[hookName]
if (state && state === 'run')
return `${' '.repeat(level)} ${getHookStateSymbol(task, hookName)} ${c.dim(`[ ${hookName} ]`)}`
Expand Down Expand Up @@ -86,10 +86,14 @@ function renderBenchmark(task: Benchmark, tasks: Task[]): string {
].join('')
}

export function renderTree(tasks: Task[], options: ListRendererOptions, level = 0) {
let output: string[] = []
export function renderTree(tasks: Task[], options: ListRendererOptions, level = 0, maxRows?: number): string {
const taskOutputs: (string[])[] = []
let currentRowCount = 0

// Go through tasks in reverse order since maxRows is used to bail out early when limit is reached
for (const task of [...tasks].reverse()) {
const taskOutput = []

for (const task of tasks) {
let suffix = ''
let prefix = ` ${getStateSymbol(task)} `

Expand Down Expand Up @@ -124,7 +128,7 @@ export function renderTree(tasks: Task[], options: ListRendererOptions, level =
? renderBenchmark(task as Benchmark, tasks)
: name

output.push(padding + prefix + body + suffix)
taskOutput.push(padding + prefix + body + suffix)

if ((task.result?.state !== 'pass') && outputMap.get(task) != null) {
let data: string | undefined = outputMap.get(task)
Expand All @@ -136,22 +140,29 @@ export function renderTree(tasks: Task[], options: ListRendererOptions, level =

if (data != null) {
const out = `${' '.repeat(level)}${F_RIGHT} ${data}`
output.push(` ${c.gray(cliTruncate(out, getCols(-3)))}`)
taskOutput.push(` ${c.gray(cliTruncate(out, getCols(-3)))}`)
}
}

output = output.concat(renderHookState(task, 'beforeAll', level + 1))
output = output.concat(renderHookState(task, 'beforeEach', level + 1))
taskOutput.push(renderHookState(task, 'beforeAll', level + 1))
taskOutput.push(renderHookState(task, 'beforeEach', level + 1))
if (task.type === 'suite' && task.tasks.length > 0) {
if ((task.result?.state === 'fail' || task.result?.state === 'run' || options.renderSucceed))
output = output.concat(renderTree(task.tasks, options, level + 1))
taskOutput.push(renderTree(task.tasks, options, level + 1, maxRows))
}
output = output.concat(renderHookState(task, 'afterAll', level + 1))
output = output.concat(renderHookState(task, 'afterEach', level + 1))
taskOutput.push(renderHookState(task, 'afterAll', level + 1))
taskOutput.push(renderHookState(task, 'afterEach', level + 1))

const rows = taskOutput.filter(Boolean)
taskOutputs.push(rows)
currentRowCount += rows.length

if (maxRows && currentRowCount >= maxRows)
break
}

// TODO: moving windows
return output.filter(Boolean).join('\n')
return taskOutputs.reverse().map(taskOutput => taskOutput.join('\n')).join('\n')
}

export const createListRenderer = (_tasks: Task[], options: ListRendererOptions) => {
Expand All @@ -161,19 +172,23 @@ export const createListRenderer = (_tasks: Task[], options: ListRendererOptions)
const log = options.logger.logUpdate

function update() {
log(renderTree(tasks, options))
log(renderTree(
tasks,
options,
0,
(process.stdout.rows || 80),
))
}

return {
start() {
if (timer)
return this
timer = setInterval(update, 200)
timer = setInterval(update, 16)
return this
},
update(_tasks: Task[]) {
tasks = _tasks
update()
return this
},
async stop() {
Expand All @@ -182,6 +197,8 @@ export const createListRenderer = (_tasks: Task[], options: ListRendererOptions)
timer = undefined
}
log.clear()

// Note that at this point the renderTree should output all tasks
options.logger.log(renderTree(tasks, options))
return this
},
Expand Down

0 comments on commit 5bd2d2f

Please sign in to comment.