Skip to content

Commit 17de89d

Browse files
committed
fix: clarify slowTestThreshold, print slow tests in non-TTY mode
1 parent 04c7175 commit 17de89d

File tree

5 files changed

+46
-17
lines changed

5 files changed

+46
-17
lines changed

docs/advanced/reporters.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,10 @@ export interface TestResultSkipped {
196196
}
197197

198198
export interface TestDiagnostic {
199+
/**
200+
* If the duration of the test is above `slowTestThreshold`.
201+
*/
202+
slow: boolean
199203
/**
200204
* The amount of memory used by the test in bytes.
201205
* This value is only available if the test was executed with `logHeapUsage` flag.

docs/config/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2174,7 +2174,7 @@ Path to custom tsconfig, relative to the project root.
21742174
- **Default**: `300`
21752175
- **CLI**: `--slow-test-threshold=<number>`, `--slowTestThreshold=<number>`
21762176

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

21792179
### chaiConfig {#chaiconfig}
21802180

packages/vitest/src/node/cli/cli-config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -656,7 +656,7 @@ export const cliOptionsConfig: VitestCLIOptions = {
656656
},
657657
slowTestThreshold: {
658658
description:
659-
'Threshold in milliseconds for a test to be considered slow (default: `300`)',
659+
'Threshold in milliseconds for a test or suite to be considered slow (default: `300`)',
660660
argument: '<threshold>',
661661
},
662662
teardownTimeout: {

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

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import {
1717
toArray,
1818
} from '../../utils'
1919
import type { Vitest } from '../core'
20-
import { F_POINTER, F_RIGHT } from '../../utils/figures'
20+
import { F_CHECK, F_POINTER, F_RIGHT } from '../../utils/figures'
2121
import type { Reporter } from '../types/reporter'
2222
import type { ErrorWithDiff, UserConsoleLog } from '../../types/general'
2323
import {
@@ -140,13 +140,7 @@ export abstract class BaseReporter implements Reporter {
140140
state += ` ${c.dim('|')} ${c.yellow(`${skipped.length} skipped`)}`
141141
}
142142
let suffix = c.dim(' (') + state + c.dim(')')
143-
if (task.result.duration) {
144-
const color
145-
= task.result.duration > this.ctx.config.slowTestThreshold
146-
? c.yellow
147-
: c.gray
148-
suffix += color(` ${Math.round(task.result.duration)}${c.dim('ms')}`)
149-
}
143+
suffix += this.getDurationPrefix(task)
150144
if (this.ctx.config.logHeapUsage && task.result.heap != null) {
151145
suffix += c.magenta(
152146
` ${Math.floor(task.result.heap / 1024 / 1024)} MB heap used`,
@@ -163,13 +157,37 @@ export abstract class BaseReporter implements Reporter {
163157
title += `${task.name} ${suffix}`
164158
logger.log(title)
165159

166-
// print short errors, full errors will be at the end in summary
167-
for (const test of failed) {
168-
logger.log(c.red(` ${taskFail} ${getTestName(test, c.dim(' > '))}`))
169-
test.result?.errors?.forEach((e) => {
170-
logger.log(c.red(` ${F_RIGHT} ${(e as any)?.message}`))
171-
})
160+
for (const test of tests) {
161+
const duration = test.result?.duration || 0
162+
if (test.result?.state === 'fail') {
163+
logger.log(c.red(` ${taskFail} ${getTestName(test, c.dim(' > '))}`))
164+
const suffix = this.getDurationPrefix(test)
165+
166+
test.result?.errors?.forEach((e) => {
167+
// print short errors, full errors will be at the end in summary
168+
logger.log(c.red(` ${F_RIGHT} ${(e as any)?.message}${suffix}`))
169+
})
170+
}
171+
// also print slow tests
172+
else if (duration > this.ctx.config.slowTestThreshold) {
173+
logger.log(
174+
` ${c.yellow(c.dim(F_CHECK))} ${getTestName(test, c.dim(' > '))}${c.yellow(
175+
` ${Math.round(duration)}${c.dim('ms')}`,
176+
)}`,
177+
)
178+
}
179+
}
180+
}
181+
182+
private getDurationPrefix(task: Task) {
183+
if (!task.result?.duration) {
184+
return ''
172185
}
186+
const color
187+
= task.result.duration > this.ctx.config.slowTestThreshold
188+
? c.yellow
189+
: c.gray
190+
return color(` ${Math.round(task.result.duration)}${c.dim('ms')}`)
173191
}
174192

175193
onWatcherStart(

packages/vitest/src/node/reporters/reported-tasks.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,9 +155,12 @@ export class TestCase extends ReportedTaskImplementation {
155155
if (!result || result.state === 'run' || !result.startTime) {
156156
return undefined
157157
}
158+
const duration = result.duration || 0
159+
const slow = duration > this.project.globalConfig.slowTestThreshold
158160
return {
161+
slow,
159162
heap: result.heap,
160-
duration: result.duration!,
163+
duration,
161164
startTime: result.startTime,
162165
retryCount: result.retryCount ?? 0,
163166
repeatCount: result.repeatCount ?? 0,
@@ -441,6 +444,10 @@ export interface TestResultSkipped {
441444
}
442445

443446
export interface TestDiagnostic {
447+
/**
448+
* If the duration of the test is above `slowTestThreshold`.
449+
*/
450+
slow: boolean
444451
/**
445452
* The amount of memory used by the test in bytes.
446453
* This value is only available if the test was executed with `logHeapUsage` flag.

0 commit comments

Comments
 (0)