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

feat(reporter): always render test time #7529

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
13 changes: 5 additions & 8 deletions packages/vitest/src/node/reporters/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ export abstract class BaseReporter implements Reporter {
for (const test of tests) {
const { duration, retryCount, repeatCount } = test.result || {}
const padding = this.getTestIndentation(test)
let suffix = ''
let suffix = this.getDurationPrefix(test)

if (retryCount != null && retryCount > 0) {
suffix += c.yellow(` (retry x${retryCount})`)
Expand All @@ -142,7 +142,7 @@ export abstract class BaseReporter implements Reporter {
}

if (test.result?.state === 'fail') {
this.log(c.red(` ${padding}${taskFail} ${this.getTestName(test, c.dim(' > '))}${this.getDurationPrefix(test)}`) + suffix)
this.log(c.red(` ${padding}${taskFail} ${this.getTestName(test, c.dim(' > '))}`) + suffix)

// print short errors, full errors will be at the end in summary
test.result?.errors?.forEach((error) => {
Expand All @@ -156,10 +156,7 @@ export abstract class BaseReporter implements Reporter {

// also print slow tests
else if (duration && duration > this.ctx.config.slowTestThreshold) {
this.log(
` ${padding}${c.yellow(c.dim(F_CHECK))} ${this.getTestName(test, c.dim(' > '))}`
+ ` ${c.yellow(Math.round(duration) + c.dim('ms'))}${suffix}`,
)
this.log(` ${padding}${c.yellow(c.dim(F_CHECK))} ${this.getTestName(test, c.dim(' > '))} ${suffix}`)
}

else if (this.ctx.config.hideSkippedTests && (test.mode === 'skip' || test.result?.state === 'skip')) {
Expand Down Expand Up @@ -194,14 +191,14 @@ export abstract class BaseReporter implements Reporter {
return ' '
}

private getDurationPrefix(task: Task) {
protected getDurationPrefix(task: Task): string {
if (!task.result?.duration) {
return ''
}

const color = task.result.duration > this.ctx.config.slowTestThreshold
? c.yellow
: c.gray
: c.green

return color(` ${Math.round(task.result.duration)}${c.dim('ms')}`)
}
Expand Down
6 changes: 4 additions & 2 deletions packages/vitest/src/node/reporters/benchmark/tableRender.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,10 @@ export function renderTable(
suffix += c.dim(c.gray(' [skipped]'))
}

if (duration != null && duration > options.slowTestThreshold) {
suffix += c.yellow(` ${Math.round(duration)}${c.dim('ms')}`)
if (duration != null) {
const color = duration > options.slowTestThreshold ? c.yellow : c.green

suffix += color(` ${Math.round(duration)}${c.dim('ms')}`)
}

if (options.showHeap && task.result?.heap != null) {
Expand Down
6 changes: 1 addition & 5 deletions packages/vitest/src/node/reporters/verbose.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,14 @@ export class VerboseReporter extends DefaultReporter {
return
}

const duration = task.result.duration
let title = ` ${getStateSymbol(task)} `

if (task.file.projectName) {
title += formatProjectName(task.file.projectName)
}

title += getFullName(task, c.dim(' > '))

if (duration != null && duration > this.ctx.config.slowTestThreshold) {
title += c.yellow(` ${Math.round(duration)}${c.dim('ms')}`)
}
title += super.getDurationPrefix(task)

if (this.ctx.config.logHeapUsage && task.result.heap != null) {
title += c.magenta(` ${Math.floor(task.result.heap / 1024 / 1024)} MB heap used`)
Expand Down
68 changes: 37 additions & 31 deletions test/reporters/tests/default.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,41 +22,37 @@ describe('default reporter', async () => {
},
})

const rows = stdout.replace(/\d+ms/g, '[...]ms').split('\n')
rows.splice(0, rows.findIndex(row => row.includes('b1.test.ts')))
rows.splice(rows.findIndex(row => row.includes('Test Files')))

expect(rows.join('\n').trim()).toMatchInlineSnapshot(`
expect(trimReporterOutput(stdout)).toMatchInlineSnapshot(`
"❯ b1.test.ts (13 tests | 1 failed) [...]ms
✓ b1 passed > b1 test
✓ b1 passed > b2 test
✓ b1 passed > b3 test
✓ b1 passed > nested b > nested b1 test
✓ b1 passed > nested b > nested b2 test
✓ b1 passed > nested b > nested b3 test
✓ b1 failed > b1 test
✓ b1 failed > b2 test
✓ b1 failed > b3 test
✓ b1 passed > b1 test [...]ms
✓ b1 passed > b2 test [...]ms
✓ b1 passed > b3 test [...]ms
✓ b1 passed > nested b > nested b1 test [...]ms
✓ b1 passed > nested b > nested b2 test [...]ms
✓ b1 passed > nested b > nested b3 test [...]ms
✓ b1 failed > b1 test [...]ms
✓ b1 failed > b2 test [...]ms
✓ b1 failed > b3 test [...]ms
× b1 failed > b failed test [...]ms
→ expected 1 to be 2 // Object.is equality
✓ b1 failed > nested b > nested b1 test
✓ b1 failed > nested b > nested b2 test
✓ b1 failed > nested b > nested b3 test
✓ b1 failed > nested b > nested b1 test [...]ms
✓ b1 failed > nested b > nested b2 test [...]ms
✓ b1 failed > nested b > nested b3 test [...]ms
❯ b2.test.ts (13 tests | 1 failed) [...]ms
✓ b2 passed > b1 test
✓ b2 passed > b2 test
✓ b2 passed > b3 test
✓ b2 passed > nested b > nested b1 test
✓ b2 passed > nested b > nested b2 test
✓ b2 passed > nested b > nested b3 test
✓ b2 failed > b1 test
✓ b2 failed > b2 test
✓ b2 failed > b3 test
✓ b2 passed > b1 test [...]ms
✓ b2 passed > b2 test [...]ms
✓ b2 passed > b3 test [...]ms
✓ b2 passed > nested b > nested b1 test [...]ms
✓ b2 passed > nested b > nested b2 test [...]ms
✓ b2 passed > nested b > nested b3 test [...]ms
✓ b2 failed > b1 test [...]ms
✓ b2 failed > b2 test [...]ms
✓ b2 failed > b3 test [...]ms
× b2 failed > b failed test [...]ms
→ expected 1 to be 2 // Object.is equality
✓ b2 failed > nested b > nested b1 test
✓ b2 failed > nested b > nested b2 test
✓ b2 failed > nested b > nested b3 test"
✓ b2 failed > nested b > nested b1 test [...]ms
✓ b2 failed > nested b > nested b2 test [...]ms
✓ b2 failed > nested b > nested b3 test [...]ms"
`)
})

Expand Down Expand Up @@ -164,7 +160,7 @@ describe('default reporter', async () => {
})

expect(stdout).toContain('1 passed')
expect(stdout).toContain('✓ pass after retries (retry x3)')
expect(trimReporterOutput(stdout)).toContain('✓ pass after retries [...]ms (retry x3)')
})

test('prints repeat count', async () => {
Expand All @@ -175,6 +171,16 @@ describe('default reporter', async () => {
})

expect(stdout).toContain('1 passed')
expect(stdout).toContain('✓ repeat couple of times (repeat x3)')
expect(trimReporterOutput(stdout)).toContain('✓ repeat couple of times [...]ms (repeat x3)')
})
}, 120000)

function trimReporterOutput(report: string) {
const rows = report.replace(/\d+ms/g, '[...]ms').split('\n')

// Trim start and end, capture just rendered tree
rows.splice(0, 1 + rows.findIndex(row => row.includes('RUN v')))
rows.splice(rows.findIndex(row => row.includes('Test Files')))

return rows.join('\n').trim()
}
6 changes: 3 additions & 3 deletions test/reporters/tests/merge-reports.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ test('merge reports', async () => {
test 1-2

❯ first.test.ts (2 tests | 1 failed) <time>
✓ test 1-1
✓ test 1-1 <time>
× test 1-2 <time>
→ expected 1 to be 2 // Object.is equality
stdout | second.test.ts > test 2-1
Expand All @@ -100,8 +100,8 @@ test('merge reports', async () => {
❯ second.test.ts (3 tests | 1 failed) <time>
× test 2-1 <time>
→ expected 1 to be 2 // Object.is equality
✓ group > test 2-2
✓ group > test 2-3
✓ group > test 2-2 <time>
✓ group > test 2-3 <time>

Test Files 2 failed (2)
Tests 2 failed | 3 passed (5)
Expand Down
67 changes: 37 additions & 30 deletions test/reporters/tests/verbose.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ test('duration', async () => {
env: { CI: '1' },
})

expect(trimReporterOutput(stdout)).toContain(`
✓ basic.test.ts > fast
✓ basic.test.ts > slow [...]ms`,
)
expect(trimReporterOutput(stdout)).toMatchInlineSnapshot(`
"✓ basic.test.ts > fast [...]ms
✓ basic.test.ts > slow [...]ms"
`)
})

test('prints error properties', async () => {
Expand All @@ -32,9 +32,11 @@ test('prints skipped tests by default', async () => {
config: false,
})

expect(stdout).toContain('✓ fixtures/all-passing-or-skipped.test.ts (2 tests | 1 skipped)')
expect(stdout).toContain('✓ 2 + 3 = 5')
expect(stdout).toContain('↓ 3 + 3 = 6')
expect(trimReporterOutput(stdout)).toMatchInlineSnapshot(`
"✓ fixtures/all-passing-or-skipped.test.ts (2 tests | 1 skipped) [...]ms
✓ 2 + 3 = 5 [...]ms
↓ 3 + 3 = 6"
`)
})

test('hides skipped tests when --hideSkippedTests', async () => {
Expand All @@ -45,9 +47,10 @@ test('hides skipped tests when --hideSkippedTests', async () => {
config: false,
})

expect(stdout).toContain('✓ fixtures/all-passing-or-skipped.test.ts (2 tests | 1 skipped)')
expect(stdout).toContain('✓ 2 + 3 = 5')
expect(stdout).not.toContain('↓ 3 + 3 = 6')
expect(trimReporterOutput(stdout)).toMatchInlineSnapshot(`
"✓ fixtures/all-passing-or-skipped.test.ts (2 tests | 1 skipped) [...]ms
✓ 2 + 3 = 5 [...]ms"
`)
})

test('prints retry count', async () => {
Expand All @@ -58,8 +61,10 @@ test('prints retry count', async () => {
config: false,
})

expect(stdout).toContain('1 passed')
expect(stdout).toContain('✓ pass after retries (retry x3)')
expect(trimReporterOutput(stdout)).toMatchInlineSnapshot(`
"✓ fixtures/retry.test.ts (1 test) [...]ms
✓ pass after retries [...]ms (retry x3)"
`)
})

test('prints repeat count', async () => {
Expand All @@ -69,8 +74,10 @@ test('prints repeat count', async () => {
config: false,
})

expect(stdout).toContain('1 passed')
expect(stdout).toContain('✓ repeat couple of times (repeat x3)')
expect(trimReporterOutput(stdout)).toMatchInlineSnapshot(`
"✓ fixtures/repeats.test.ts (1 test) [...]ms
✓ repeat couple of times [...]ms (repeat x3)"
`)
})

test('renders tree when in TTY', async () => {
Expand All @@ -94,14 +101,14 @@ test('renders tree when in TTY', async () => {

expect(trimReporterOutput(stdout)).toMatchInlineSnapshot(`
"❯ fixtures/verbose/example-1.test.ts (10 tests | 1 failed | 4 skipped) [...]ms
✓ test pass in root
✓ test pass in root [...]ms
↓ test skip in root
❯ suite in root (5)
✓ test pass in 1. suite #1
✓ test pass in 1. suite #2
✓ test pass in 1. suite #1 [...]ms
✓ test pass in 1. suite #2 [...]ms
❯ suite in suite (3)
✓ test pass in nested suite #1
✓ test pass in nested suite #2
✓ test pass in nested suite #1 [...]ms
✓ test pass in nested suite #2 [...]ms
❯ suite in nested suite (1)
× test failure in 2x nested suite [...]ms
↓ suite skip in root (3)
Expand All @@ -110,10 +117,10 @@ test('renders tree when in TTY', async () => {
↓ test in nested suite
↓ test failure in nested suite of skipped suite
✓ fixtures/verbose/example-2.test.ts (3 tests | 1 skipped) [...]ms
✓ test 0.1
✓ test 0.1 [...]ms
↓ test 0.2
✓ suite 1.1 (1)
✓ test 1.1"
✓ test 1.1 [...]ms"
`)
})

Expand All @@ -137,23 +144,23 @@ test('does not render tree when in non-TTY', async () => {
})

expect(trimReporterOutput(stdout)).toMatchInlineSnapshot(`
"✓ fixtures/verbose/example-1.test.ts > test pass in root
✓ fixtures/verbose/example-1.test.ts > suite in root > test pass in 1. suite #1
✓ fixtures/verbose/example-1.test.ts > suite in root > test pass in 1. suite #2
✓ fixtures/verbose/example-1.test.ts > suite in root > suite in suite > test pass in nested suite #1
✓ fixtures/verbose/example-1.test.ts > suite in root > suite in suite > test pass in nested suite #2
× fixtures/verbose/example-1.test.ts > suite in root > suite in suite > suite in nested suite > test failure in 2x nested suite
"✓ fixtures/verbose/example-1.test.ts > test pass in root [...]ms
✓ fixtures/verbose/example-1.test.ts > suite in root > test pass in 1. suite #1 [...]ms
✓ fixtures/verbose/example-1.test.ts > suite in root > test pass in 1. suite #2 [...]ms
✓ fixtures/verbose/example-1.test.ts > suite in root > suite in suite > test pass in nested suite #1 [...]ms
✓ fixtures/verbose/example-1.test.ts > suite in root > suite in suite > test pass in nested suite #2 [...]ms
× fixtures/verbose/example-1.test.ts > suite in root > suite in suite > suite in nested suite > test failure in 2x nested suite [...]ms
→ expected 'should fail' to be 'as expected' // Object.is equality
✓ fixtures/verbose/example-2.test.ts > test 0.1
✓ fixtures/verbose/example-2.test.ts > suite 1.1 > test 1.1"
✓ fixtures/verbose/example-2.test.ts > test 0.1 [...]ms
✓ fixtures/verbose/example-2.test.ts > suite 1.1 > test 1.1 [...]ms"
`)
})

function trimReporterOutput(report: string) {
const rows = report.replace(/\d+ms/g, '[...]ms').split('\n')

// Trim start and end, capture just rendered tree
rows.splice(0, rows.findIndex(row => row.includes('fixtures/verbose/example-')))
rows.splice(0, 1 + rows.findIndex(row => row.includes('RUN v')))
rows.splice(rows.findIndex(row => row.includes('Test Files')))

return rows.join('\n').trim()
Expand Down