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 2027901
Showing 1 changed file with 23 additions and 13 deletions.
36 changes: 23 additions & 13 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,11 @@ 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) {
const output: string[] = []

for (const task of tasks) {
// Go through tasks in reverse order since maxRows is used to bail out early when limit is reached
for (const task of [...tasks].reverse()) {
let suffix = ''
let prefix = ` ${getStateSymbol(task)} `

Expand Down Expand Up @@ -140,18 +141,21 @@ export function renderTree(tasks: Task[], options: ListRendererOptions, level =
}
}

output = output.concat(renderHookState(task, 'beforeAll', level + 1))
output = output.concat(renderHookState(task, 'beforeEach', level + 1))
output.push(renderHookState(task, 'beforeAll', level + 1))
output.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))
output.push(renderTree(task.tasks, options, level + 1, maxRows))
}
output = output.concat(renderHookState(task, 'afterAll', level + 1))
output = output.concat(renderHookState(task, 'afterEach', level + 1))
output.push(renderHookState(task, 'afterAll', level + 1))
output.push(renderHookState(task, 'afterEach', level + 1))

if (maxRows && output.filter(Boolean).length >= maxRows)
break
}

// TODO: moving windows
return output.filter(Boolean).join('\n')
return output.reverse().filter(Boolean).join('\n')
}

export const createListRenderer = (_tasks: Task[], options: ListRendererOptions) => {
Expand All @@ -161,19 +165,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 +190,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 2027901

Please sign in to comment.