Skip to content

Commit

Permalink
feat(vitest): show slow test duration in verbose reporter on CI (#4929)
Browse files Browse the repository at this point in the history
  • Loading branch information
hi-ogawa committed Jan 12, 2024
1 parent 43fa6ba commit ccb2583
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 0 deletions.
2 changes: 2 additions & 0 deletions packages/vitest/src/node/reporters/verbose.ts
Expand Up @@ -21,6 +21,8 @@ export class VerboseReporter extends DefaultReporter {
if (task.suite?.projectName)
title += formatProjectName(task.suite.projectName)
title += getFullName(task, c.dim(' > '))
if (task.result.duration != null && task.result.duration > this.ctx.config.slowTestThreshold)
title += c.yellow(` ${Math.round(task.result.duration)}${c.dim('ms')}`)
if (this.ctx.config.logHeapUsage && task.result.heap != null)
title += c.magenta(` ${Math.floor(task.result.heap / 1024 / 1024)} MB heap used`)
this.ctx.logger.log(title)
Expand Down
11 changes: 11 additions & 0 deletions test/reporters/fixtures/duration/basic.test.ts
@@ -0,0 +1,11 @@
import { test } from 'vitest';

test('fast', async () => {
await sleep(10)
});

test('slow', async () => {
await sleep(200)
});

const sleep = (ms: number) => new Promise(resolve => setTimeout(resolve, ms))
7 changes: 7 additions & 0 deletions test/reporters/fixtures/duration/vitest.config.ts
@@ -0,0 +1,7 @@
import { defineConfig } from 'vitest/config'

export default defineConfig({
test: {
slowTestThreshold: 100
}
})
11 changes: 11 additions & 0 deletions test/reporters/tests/verbose.test.ts
@@ -0,0 +1,11 @@
import { expect, test } from 'vitest'
import { runVitestCli } from '../../test-utils'

test('duration', async () => {
const result = await runVitestCli({ env: { CI: '1' } }, '--root=fixtures/duration', '--reporter=verbose')
const output = result.stdout.replaceAll(/\d+ms/g, '[...]ms')
expect(output).toContain(`
✓ basic.test.ts > fast
✓ basic.test.ts > slow [...]ms
`)
})

0 comments on commit ccb2583

Please sign in to comment.