Skip to content

Commit 2e6aa64

Browse files
authoredOct 15, 2024··
fix(vitest): clarify slowTestThreshold, print slow tests in non-TTY mode (#6715)
1 parent 7a375cd commit 2e6aa64

File tree

6 files changed

+47
-19
lines changed

6 files changed

+47
-19
lines changed
 

‎docs/advanced/reporters.md

+4
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

+1-1
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

+1-1
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

+31-14
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import type { Vitest } from '../core'
1010
import type { Reporter } from '../types/reporter'
1111
import type { ErrorWithDiff, UserConsoleLog } from '../../types/general'
1212
import { hasFailedSnapshot } from '../../utils/tasks'
13-
import { F_POINTER, F_RIGHT } from './renderers/figures'
13+
import { F_CHECK, F_POINTER, F_RIGHT } from './renderers/figures'
1414
import {
1515
countTestErrors,
1616
divider,
@@ -131,13 +131,7 @@ export abstract class BaseReporter implements Reporter {
131131
state += ` ${c.dim('|')} ${c.yellow(`${skipped.length} skipped`)}`
132132
}
133133
let suffix = c.dim(' (') + state + c.dim(')')
134-
if (task.result.duration) {
135-
const color
136-
= task.result.duration > this.ctx.config.slowTestThreshold
137-
? c.yellow
138-
: c.gray
139-
suffix += color(` ${Math.round(task.result.duration)}${c.dim('ms')}`)
140-
}
134+
suffix += this.getDurationPrefix(task)
141135
if (this.ctx.config.logHeapUsage && task.result.heap != null) {
142136
suffix += c.magenta(
143137
` ${Math.floor(task.result.heap / 1024 / 1024)} MB heap used`,
@@ -154,13 +148,36 @@ export abstract class BaseReporter implements Reporter {
154148
title += `${task.name} ${suffix}`
155149
logger.log(title)
156150

157-
// print short errors, full errors will be at the end in summary
158-
for (const test of failed) {
159-
logger.log(c.red(` ${taskFail} ${getTestName(test, c.dim(' > '))}`))
160-
test.result?.errors?.forEach((e) => {
161-
logger.log(c.red(` ${F_RIGHT} ${(e as any)?.message}`))
162-
})
151+
for (const test of tests) {
152+
const duration = test.result?.duration
153+
if (test.result?.state === 'fail') {
154+
const suffix = this.getDurationPrefix(test)
155+
logger.log(c.red(` ${taskFail} ${getTestName(test, c.dim(' > '))}${suffix}`))
156+
157+
test.result?.errors?.forEach((e) => {
158+
// print short errors, full errors will be at the end in summary
159+
logger.log(c.red(` ${F_RIGHT} ${(e as any)?.message}`))
160+
})
161+
}
162+
// also print slow tests
163+
else if (duration && duration > this.ctx.config.slowTestThreshold) {
164+
logger.log(
165+
` ${c.yellow(c.dim(F_CHECK))} ${getTestName(test, c.dim(' > '))}${c.yellow(
166+
` ${Math.round(duration)}${c.dim('ms')}`,
167+
)}`,
168+
)
169+
}
170+
}
171+
}
172+
173+
private getDurationPrefix(task: Task) {
174+
if (!task.result?.duration) {
175+
return ''
163176
}
177+
const color = task.result.duration > this.ctx.config.slowTestThreshold
178+
? c.yellow
179+
: c.gray
180+
return color(` ${Math.round(task.result.duration)}${c.dim('ms')}`)
164181
}
165182

166183
onWatcherStart(

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

+8-1
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.

‎test/reporters/tests/merge-reports.test.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -89,13 +89,13 @@ test('merge reports', async () => {
8989
test 1-2
9090
9191
❯ first.test.ts (2 tests | 1 failed) <time>
92-
× test 1-2
92+
× test 1-2 <time>
9393
→ expected 1 to be 2 // Object.is equality
9494
stdout | second.test.ts > test 2-1
9595
test 2-1
9696
9797
❯ second.test.ts (3 tests | 1 failed) <time>
98-
× test 2-1
98+
× test 2-1 <time>
9999
→ expected 1 to be 2 // Object.is equality
100100
101101
Test Files 2 failed (2)

0 commit comments

Comments
 (0)
Please sign in to comment.