Skip to content

Commit

Permalink
fix: clarify slowTestThreshold, print slow tests in non-TTY mode
Browse files Browse the repository at this point in the history
  • Loading branch information
sheremet-va committed Oct 15, 2024
1 parent 04c7175 commit 17de89d
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 17 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
46 changes: 32 additions & 14 deletions packages/vitest/src/node/reporters/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
toArray,
} from '../../utils'
import type { Vitest } from '../core'
import { F_POINTER, F_RIGHT } from '../../utils/figures'
import { F_CHECK, F_POINTER, F_RIGHT } from '../../utils/figures'
import type { Reporter } from '../types/reporter'
import type { ErrorWithDiff, UserConsoleLog } from '../../types/general'
import {
Expand Down Expand Up @@ -140,13 +140,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 @@ -163,13 +157,37 @@ 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 || 0
if (test.result?.state === 'fail') {
logger.log(c.red(` ${taskFail} ${getTestName(test, c.dim(' > '))}`))
const suffix = this.getDurationPrefix(test)

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}${suffix}`))
})
}
// also print slow tests
else if (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

0 comments on commit 17de89d

Please sign in to comment.