Skip to content

Commit

Permalink
fix(vitest): clarify slowTestThreshold, print slow tests in non-TTY m…
Browse files Browse the repository at this point in the history
…ode (#6715)
  • Loading branch information
sheremet-va authored Oct 15, 2024
1 parent 7a375cd commit 2e6aa64
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 19 deletions.
4 changes: 4 additions & 0 deletions docs/advanced/reporters.md
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,10 @@ export interface TestResultSkipped {
}

export interface TestDiagnostic {
/**
* If the duration of the test is above `slowTestThreshold`.
*/
slow: boolean
/**
* The amount of memory used by the test in bytes.
* This value is only available if the test was executed with `logHeapUsage` flag.
Expand Down
2 changes: 1 addition & 1 deletion docs/config/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -2174,7 +2174,7 @@ Path to custom tsconfig, relative to the project root.
- **Default**: `300`
- **CLI**: `--slow-test-threshold=<number>`, `--slowTestThreshold=<number>`

The number of milliseconds after which a test is considered slow and reported as such in the results.
The number of milliseconds after which a test or suite is considered slow and reported as such in the results.

### chaiConfig {#chaiconfig}

Expand Down
2 changes: 1 addition & 1 deletion packages/vitest/src/node/cli/cli-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -656,7 +656,7 @@ export const cliOptionsConfig: VitestCLIOptions = {
},
slowTestThreshold: {
description:
'Threshold in milliseconds for a test to be considered slow (default: `300`)',
'Threshold in milliseconds for a test or suite to be considered slow (default: `300`)',
argument: '<threshold>',
},
teardownTimeout: {
Expand Down
45 changes: 31 additions & 14 deletions packages/vitest/src/node/reporters/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import type { Vitest } from '../core'
import type { Reporter } from '../types/reporter'
import type { ErrorWithDiff, UserConsoleLog } from '../../types/general'
import { hasFailedSnapshot } from '../../utils/tasks'
import { F_POINTER, F_RIGHT } from './renderers/figures'
import { F_CHECK, F_POINTER, F_RIGHT } from './renderers/figures'
import {
countTestErrors,
divider,
Expand Down Expand Up @@ -131,13 +131,7 @@ export abstract class BaseReporter implements Reporter {
state += ` ${c.dim('|')} ${c.yellow(`${skipped.length} skipped`)}`
}
let suffix = c.dim(' (') + state + c.dim(')')
if (task.result.duration) {
const color
= task.result.duration > this.ctx.config.slowTestThreshold
? c.yellow
: c.gray
suffix += color(` ${Math.round(task.result.duration)}${c.dim('ms')}`)
}
suffix += this.getDurationPrefix(task)
if (this.ctx.config.logHeapUsage && task.result.heap != null) {
suffix += c.magenta(
` ${Math.floor(task.result.heap / 1024 / 1024)} MB heap used`,
Expand All @@ -154,13 +148,36 @@ export abstract class BaseReporter implements Reporter {
title += `${task.name} ${suffix}`
logger.log(title)

// print short errors, full errors will be at the end in summary
for (const test of failed) {
logger.log(c.red(` ${taskFail} ${getTestName(test, c.dim(' > '))}`))
test.result?.errors?.forEach((e) => {
logger.log(c.red(` ${F_RIGHT} ${(e as any)?.message}`))
})
for (const test of tests) {
const duration = test.result?.duration
if (test.result?.state === 'fail') {
const suffix = this.getDurationPrefix(test)
logger.log(c.red(` ${taskFail} ${getTestName(test, c.dim(' > '))}${suffix}`))

test.result?.errors?.forEach((e) => {
// print short errors, full errors will be at the end in summary
logger.log(c.red(` ${F_RIGHT} ${(e as any)?.message}`))
})
}
// also print slow tests
else if (duration && duration > this.ctx.config.slowTestThreshold) {
logger.log(
` ${c.yellow(c.dim(F_CHECK))} ${getTestName(test, c.dim(' > '))}${c.yellow(
` ${Math.round(duration)}${c.dim('ms')}`,
)}`,
)
}
}
}

private getDurationPrefix(task: Task) {
if (!task.result?.duration) {
return ''
}
const color = task.result.duration > this.ctx.config.slowTestThreshold
? c.yellow
: c.gray
return color(` ${Math.round(task.result.duration)}${c.dim('ms')}`)
}

onWatcherStart(
Expand Down
9 changes: 8 additions & 1 deletion packages/vitest/src/node/reporters/reported-tasks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,9 +155,12 @@ export class TestCase extends ReportedTaskImplementation {
if (!result || result.state === 'run' || !result.startTime) {
return undefined
}
const duration = result.duration || 0
const slow = duration > this.project.globalConfig.slowTestThreshold
return {
slow,
heap: result.heap,
duration: result.duration!,
duration,
startTime: result.startTime,
retryCount: result.retryCount ?? 0,
repeatCount: result.repeatCount ?? 0,
Expand Down Expand Up @@ -441,6 +444,10 @@ export interface TestResultSkipped {
}

export interface TestDiagnostic {
/**
* If the duration of the test is above `slowTestThreshold`.
*/
slow: boolean
/**
* The amount of memory used by the test in bytes.
* This value is only available if the test was executed with `logHeapUsage` flag.
Expand Down
4 changes: 2 additions & 2 deletions test/reporters/tests/merge-reports.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,13 @@ test('merge reports', async () => {
test 1-2
❯ first.test.ts (2 tests | 1 failed) <time>
× test 1-2
× test 1-2 <time>
→ expected 1 to be 2 // Object.is equality
stdout | second.test.ts > test 2-1
test 2-1
❯ second.test.ts (3 tests | 1 failed) <time>
× test 2-1
× test 2-1 <time>
→ expected 1 to be 2 // Object.is equality
Test Files 2 failed (2)
Expand Down

0 comments on commit 2e6aa64

Please sign in to comment.