Skip to content

Commit 5f8d209

Browse files
feat(runner): add "queued" state (#6931)
Co-authored-by: Ari Perkkiö <[email protected]>
1 parent 4e60333 commit 5f8d209

File tree

26 files changed

+105
-30
lines changed

26 files changed

+105
-30
lines changed

packages/browser/src/client/tester/runner.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,10 @@ export function createBrowserRunner(
104104
}
105105
}
106106

107+
onCollectStart = (file: File) => {
108+
return rpc().onQueued(file)
109+
}
110+
107111
onCollected = async (files: File[]): Promise<unknown> => {
108112
files.forEach((file) => {
109113
file.prepareDuration = state.durations.prepare

packages/browser/src/node/rpc.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { ErrorWithDiff } from 'vitest'
2-
import type { BrowserCommandContext, ResolveSnapshotPathHandlerContext } from 'vitest/node'
2+
import type { BrowserCommandContext, ResolveSnapshotPathHandlerContext, TestModule } from 'vitest/node'
33
import type { WebSocket } from 'ws'
44
import type { BrowserServer } from './server'
55
import type { WebSocketBrowserEvents, WebSocketBrowserHandlers } from './types'
@@ -75,6 +75,11 @@ export function setupBrowserRpc(server: BrowserServer) {
7575
}
7676
ctx.state.catchError(error, type)
7777
},
78+
async onQueued(file) {
79+
ctx.state.collectFiles(project, [file])
80+
const testModule = ctx.state.getReportedEntity(file) as TestModule
81+
await ctx.report('onTestModuleQueued', testModule)
82+
},
7883
async onCollected(files) {
7984
ctx.state.collectFiles(project, files)
8085
await ctx.report('onCollected', files)

packages/browser/src/node/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ export interface WebSocketBrowserHandlers {
66
resolveSnapshotPath: (testPath: string) => string
77
resolveSnapshotRawPath: (testPath: string, rawPath: string) => string
88
onUnhandledError: (error: unknown, type: string) => Promise<void>
9+
onQueued: (file: RunnerTestFile) => void
910
onCollected: (files?: RunnerTestFile[]) => Promise<void>
1011
onTaskUpdate: (packs: TaskResultPack[]) => void
1112
onAfterSuiteRun: (meta: AfterSuiteRunMeta) => void

packages/runner/src/collect.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,10 @@ export async function collectTests(
107107
config.allowOnly,
108108
)
109109

110+
if (file.mode === 'queued') {
111+
file.mode = 'run'
112+
}
113+
110114
files.push(file)
111115
}
112116

packages/runner/src/run.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ async function callCleanupHooks(cleanups: HookCleanupCallback[]) {
196196
export async function runTest(test: Test, runner: VitestRunner): Promise<void> {
197197
await runner.onBeforeRunTask?.(test)
198198

199-
if (test.mode !== 'run') {
199+
if (test.mode !== 'run' && test.mode !== 'queued') {
200200
return
201201
}
202202

@@ -458,7 +458,7 @@ export async function runSuite(suite: Suite, runner: VitestRunner): Promise<void
458458
failTask(suite.result, e, runner.config.diffOptions)
459459
}
460460

461-
if (suite.mode === 'run') {
461+
if (suite.mode === 'run' || suite.mode === 'queued') {
462462
if (!runner.config.passWithNoTests && !hasTests(suite)) {
463463
suite.result.state = 'fail'
464464
if (!suite.result.errors?.length) {

packages/runner/src/types/tasks.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import type { Awaitable, ErrorWithDiff } from '@vitest/utils'
22
import type { FixtureItem } from '../fixture'
33
import type { ChainableFunction } from '../utils/chain'
44

5-
export type RunMode = 'run' | 'skip' | 'only' | 'todo'
5+
export type RunMode = 'run' | 'skip' | 'only' | 'todo' | 'queued'
66
export type TaskState = RunMode | 'pass' | 'fail'
77

88
export interface TaskBase {
@@ -23,6 +23,7 @@ export interface TaskBase {
2323
* - **only**: only this task and other tasks with `only` mode will run
2424
* - **todo**: task is marked as a todo, alias for `skip`
2525
* - **run**: task will run or already ran
26+
* - **queued**: task will start running next. It can only exist on the File
2627
*/
2728
mode: RunMode
2829
/**

packages/runner/src/utils/collect.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,8 @@ export function interpretTaskModes(
7272
})
7373

7474
// if all subtasks are skipped, mark as skip
75-
if (suite.mode === 'run') {
76-
if (suite.tasks.length && suite.tasks.every(i => i.mode !== 'run')) {
75+
if (suite.mode === 'run' || suite.mode === 'queued') {
76+
if (suite.tasks.length && suite.tasks.every(i => i.mode !== 'run' && i.mode !== 'queued')) {
7777
suite.mode = 'skip'
7878
}
7979
}
@@ -115,7 +115,7 @@ export function someTasksAreOnly(suite: Suite): boolean {
115115

116116
function skipAllTasks(suite: Suite) {
117117
suite.tasks.forEach((t) => {
118-
if (t.mode === 'run') {
118+
if (t.mode === 'run' || t.mode === 'queued') {
119119
t.mode = 'skip'
120120
if (t.type === 'suite') {
121121
skipAllTasks(t)
@@ -172,7 +172,7 @@ export function createFileTask(
172172
id: generateFileHash(path, projectName),
173173
name: path,
174174
type: 'suite',
175-
mode: 'run',
175+
mode: 'queued',
176176
filepath,
177177
tasks: [],
178178
meta: Object.create(null),

packages/vitest/src/node/pools/rpc.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type { RawSourceMap } from 'vite-node'
22
import type { RuntimeRPC } from '../../types/rpc'
33
import type { TestProject } from '../project'
4+
import type { TestModule } from '../reporters/reported-tasks'
45
import type { ResolveSnapshotPathHandlerContext } from '../types/config'
56
import { mkdir, writeFile } from 'node:fs/promises'
67
import { join } from 'pathe'
@@ -78,6 +79,11 @@ export function createMethodsRPC(project: TestProject, options: MethodsOptions =
7879
ctx.state.collectPaths(paths)
7980
return ctx.report('onPathsCollected', paths)
8081
},
82+
onQueued(file) {
83+
ctx.state.collectFiles(project, [file])
84+
const testModule = ctx.state.getReportedEntity(file) as TestModule
85+
return ctx.report('onTestModuleQueued', testModule)
86+
},
8187
onCollected(files) {
8288
ctx.state.collectFiles(project, files)
8389
return ctx.report('onCollected', files)

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,8 @@ export abstract class BaseReporter implements Reporter {
7575
if (
7676
!('filepath' in task)
7777
|| !task.result?.state
78-
|| task.result?.state === 'run') {
78+
|| task.result?.state === 'run'
79+
|| task.result?.state === 'queued') {
7980
return
8081
}
8182

packages/vitest/src/node/reporters/benchmark/table/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,13 @@ export class TableReporter extends BaseReporter {
7676
&& task.type === 'suite'
7777
&& task.result?.state
7878
&& task.result?.state !== 'run'
79+
&& task.result?.state !== 'queued'
7980
) {
8081
// render static table when all benches inside single suite are finished
8182
const benches = task.tasks.filter(t => t.meta.benchmark)
8283
if (
8384
benches.length > 0
84-
&& benches.every(t => t.result?.state !== 'run')
85+
&& benches.every(t => t.result?.state !== 'run' && t.result?.state !== 'queued')
8586
) {
8687
let title = ` ${getStateSymbol(task)} ${getFullName(
8788
task,

0 commit comments

Comments
 (0)