Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/config/slowtestthreshold.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ title: slowTestThreshold | Config
outline: deep
---

# slowTestThreshold <CRoot />
# slowTestThreshold

- **Type**: `number`
- **Default**: `300`
Expand Down
1 change: 1 addition & 0 deletions packages/runner/src/suite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,7 @@ function createSuiteCollector(
annotations: [],
artifacts: [],
tags: testTags,
slowTestThreshold: options.slowTestThreshold,
}
const handler = options.handler
if (task.mode === 'run' && !handler) {
Expand Down
10 changes: 10 additions & 0 deletions packages/runner/src/types/tasks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,11 @@ export interface Test<ExtraContext = object> extends TaskPopulated {
*/
artifacts: TestArtifact[]
fullTestName: string
/**
* Threshold in milliseconds for a test to be considered slow.
* Overrides the global `slowTestThreshold` config option.
*/
slowTestThreshold?: number
}

export type Task = Test | Suite | File
Expand Down Expand Up @@ -582,6 +587,11 @@ export interface TestOptions {
* Custom test metadata available to reporters.
*/
meta?: Partial<TaskMeta>
/**
* Threshold in milliseconds for a test to be considered slow.
* Overrides the global `slowTestThreshold` config option.
*/
slowTestThreshold?: number
}

export interface TestTags {}
Expand Down
1 change: 1 addition & 0 deletions packages/runner/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"target": "ESNext",
"module": "ESNext",
"moduleResolution": "Bundler",
"types": ["node"],
"isolatedDeclarations": true
},
"include": ["./src/**/*.ts"],
Expand Down
2 changes: 1 addition & 1 deletion packages/ui/client/composables/explorer/filter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ function* filterParents(
function matchState(task: Task, filter: Filter) {
if (filter.slow) {
if (task.type === 'test') {
const threshold = config.value.slowTestThreshold
const threshold = task.slowTestThreshold ?? config.value.slowTestThreshold
if (typeof threshold === 'number' && typeof task.result?.duration === 'number' && task.result.duration > threshold) {
return true
}
Expand Down
2 changes: 1 addition & 1 deletion packages/ui/client/composables/explorer/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export function isSlowTestTask(task: Task) {
return false
}

const threshold = config.value.slowTestThreshold
const threshold = task.slowTestThreshold ?? config.value.slowTestThreshold
return typeof threshold === 'number' && duration > threshold
}

Expand Down
8 changes: 5 additions & 3 deletions packages/vitest/src/node/reporters/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ export abstract class BaseReporter implements Reporter {
protected printTestCase(moduleState: TestModuleState, test: TestCase): void {
const testResult = test.result()

const { duration = 0 } = test.diagnostic() || {}
const diagnostic = test.diagnostic()
const padding = this.getTestIndentation(test.task)
const suffix = this.getTestCaseSuffix(test)

Expand All @@ -212,7 +212,7 @@ export abstract class BaseReporter implements Reporter {
}

// also print slow tests
else if (duration > this.ctx.config.slowTestThreshold) {
else if (diagnostic?.slow) {
this.log(` ${padding}${c.yellow(c.dim(F_CHECK))} ${this.getTestName(test.task, separator)} ${suffix}`)
}

Expand Down Expand Up @@ -372,7 +372,9 @@ export abstract class BaseReporter implements Reporter {
return ''
}

const color = duration > this.ctx.config.slowTestThreshold
const slowTestThreshold = ('slowTestThreshold' in task && task.slowTestThreshold)
|| this.ctx.config.slowTestThreshold
const color = duration > slowTestThreshold
? c.yellow
: c.green

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 @@ -216,7 +216,8 @@ export class TestCase extends ReportedTaskImplementation {
return undefined
}
const duration = result.duration || 0
const slow = duration > this.project.globalConfig.slowTestThreshold
const slowTestThreshold = this.task.slowTestThreshold ?? this.project.globalConfig.slowTestThreshold
const slow = duration > slowTestThreshold
return {
slow,
heap: result.heap,
Expand Down Expand Up @@ -576,6 +577,11 @@ export interface TaskOptions {
* Only tests have a `timeout` option.
*/
readonly timeout: number | undefined
/**
* Threshold in milliseconds for a test to be considered slow.
* Only tests have a `slowTestThreshold` option.
*/
readonly slowTestThreshold: number | undefined
readonly mode: 'run' | 'only' | 'skip' | 'todo'
}

Expand All @@ -591,6 +597,7 @@ function buildOptions(
repeats: task.repeats,
tags: task.tags,
timeout: task.type === 'test' ? task.timeout : undefined,
slowTestThreshold: task.type === 'test' ? task.slowTestThreshold : undefined,
// runner types are too broad, but the public API should be more strict
// the queued state exists only on Files and this method is called
// only for tests and suites
Expand Down
3 changes: 2 additions & 1 deletion packages/vitest/src/node/reporters/summary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,9 +183,10 @@ export class SummaryReporter implements Reporter {
onFinish: () => {},
}

const slowTestThreshold = test.options.slowTestThreshold ?? this.ctx.config.slowTestThreshold
const timeout = setTimeout(() => {
slowTest.visible = true
}, this.ctx.config.slowTestThreshold).unref()
}, slowTestThreshold).unref()

slowTest.onFinish = () => {
slowTest.hook?.onFinish()
Expand Down
Loading