Skip to content

Commit ccb2583

Browse files
authored
feat(vitest): show slow test duration in verbose reporter on CI (#4929)
1 parent 43fa6ba commit ccb2583

File tree

4 files changed

+31
-0
lines changed

4 files changed

+31
-0
lines changed

packages/vitest/src/node/reporters/verbose.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ export class VerboseReporter extends DefaultReporter {
2121
if (task.suite?.projectName)
2222
title += formatProjectName(task.suite.projectName)
2323
title += getFullName(task, c.dim(' > '))
24+
if (task.result.duration != null && task.result.duration > this.ctx.config.slowTestThreshold)
25+
title += c.yellow(` ${Math.round(task.result.duration)}${c.dim('ms')}`)
2426
if (this.ctx.config.logHeapUsage && task.result.heap != null)
2527
title += c.magenta(` ${Math.floor(task.result.heap / 1024 / 1024)} MB heap used`)
2628
this.ctx.logger.log(title)
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { test } from 'vitest';
2+
3+
test('fast', async () => {
4+
await sleep(10)
5+
});
6+
7+
test('slow', async () => {
8+
await sleep(200)
9+
});
10+
11+
const sleep = (ms: number) => new Promise(resolve => setTimeout(resolve, ms))
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { defineConfig } from 'vitest/config'
2+
3+
export default defineConfig({
4+
test: {
5+
slowTestThreshold: 100
6+
}
7+
})

test/reporters/tests/verbose.test.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { expect, test } from 'vitest'
2+
import { runVitestCli } from '../../test-utils'
3+
4+
test('duration', async () => {
5+
const result = await runVitestCli({ env: { CI: '1' } }, '--root=fixtures/duration', '--reporter=verbose')
6+
const output = result.stdout.replaceAll(/\d+ms/g, '[...]ms')
7+
expect(output).toContain(`
8+
✓ basic.test.ts > fast
9+
✓ basic.test.ts > slow [...]ms
10+
`)
11+
})

0 commit comments

Comments
 (0)