/
tasks.ts
193 lines (168 loc) · 5.48 KB
/
tasks.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
import type { ChainableFunction } from '../runtime/chain'
import type { Awaitable, ErrorWithDiff } from './general'
import type { UserConsoleLog } from '.'
export type RunMode = 'run' | 'skip' | 'only' | 'todo'
export type TaskState = RunMode | 'pass' | 'fail'
export interface TaskBase {
id: string
name: string
mode: RunMode
concurrent?: boolean
shuffle?: boolean
suite?: Suite
file?: File
result?: TaskResult
logs?: UserConsoleLog[]
}
export interface TaskResult {
state: TaskState
duration?: number
startTime?: number
heap?: number
error?: ErrorWithDiff
htmlError?: string
hooks?: Partial<Record<keyof SuiteHooks, TaskState>>
}
export type TaskResultPack = [id: string, result: TaskResult | undefined]
export interface Suite extends TaskBase {
type: 'suite'
tasks: Task[]
filepath?: string
}
export interface File extends Suite {
filepath: string
collectDuration?: number
setupDuration?: number
}
export interface Test<ExtraContext = {}> extends TaskBase {
type: 'test'
suite: Suite
result?: TaskResult
fails?: boolean
context: TestContext & ExtraContext
}
export type Task = Test | Suite | File
export type DoneCallback = (error?: any) => void
export type TestFunction<ExtraContext = {}> = (context: TestContext & ExtraContext) => Awaitable<any> | void
// jest's ExtractEachCallbackArgs
type ExtractEachCallbackArgs<T extends ReadonlyArray<any>> = {
1: [T[0]]
2: [T[0], T[1]]
3: [T[0], T[1], T[2]]
4: [T[0], T[1], T[2], T[3]]
5: [T[0], T[1], T[2], T[3], T[4]]
6: [T[0], T[1], T[2], T[3], T[4], T[5]]
7: [T[0], T[1], T[2], T[3], T[4], T[5], T[6]]
8: [T[0], T[1], T[2], T[3], T[4], T[5], T[6], T[7]]
9: [T[0], T[1], T[2], T[3], T[4], T[5], T[6], T[7], T[8]]
10: [T[0], T[1], T[2], T[3], T[4], T[5], T[6], T[7], T[8], T[9]]
fallback: Array<T extends ReadonlyArray<infer U> ? U : any>
}[T extends Readonly<[any]>
? 1
: T extends Readonly<[any, any]>
? 2
: T extends Readonly<[any, any, any]>
? 3
: T extends Readonly<[any, any, any, any]>
? 4
: T extends Readonly<[any, any, any, any, any]>
? 5
: T extends Readonly<[any, any, any, any, any, any]>
? 6
: T extends Readonly<[any, any, any, any, any, any, any]>
? 7
: T extends Readonly<[any, any, any, any, any, any, any, any]>
? 8
: T extends Readonly<[any, any, any, any, any, any, any, any, any]>
? 9
: T extends Readonly<[any, any, any, any, any, any, any, any, any, any]>
? 10
: 'fallback']
interface SuiteEachFunction {
<T extends any[] | [any]>(cases: ReadonlyArray<T>): (
name: string,
fn: (...args: T) => Awaitable<void>,
) => void
<T extends ReadonlyArray<any>>(cases: ReadonlyArray<T>): (
name: string,
fn: (...args: ExtractEachCallbackArgs<T>) => Awaitable<void>,
) => void
<T>(cases: ReadonlyArray<T>): (
name: string,
fn: (...args: T[]) => Awaitable<void>,
) => void
}
interface TestEachFunction {
<T extends any[] | [any]>(cases: ReadonlyArray<T>): (
name: string,
fn: (...args: T) => Awaitable<void>,
timeout?: number,
) => void
<T extends ReadonlyArray<any>>(cases: ReadonlyArray<T>): (
name: string,
fn: (...args: ExtractEachCallbackArgs<T>) => Awaitable<void>,
timeout?: number,
) => void
<T>(cases: ReadonlyArray<T>): (
name: string,
fn: (...args: T[]) => Awaitable<void>,
timeout?: number,
) => void
}
type ChainableTestAPI<ExtraContext = {}> = ChainableFunction<
'concurrent' | 'only' | 'skip' | 'todo' | 'fails',
[name: string, fn?: TestFunction<ExtraContext>, timeout?: number],
void
>
export type TestAPI<ExtraContext = {}> = ChainableTestAPI<ExtraContext> & {
each: TestEachFunction
skipIf(condition: any): ChainableTestAPI<ExtraContext>
runIf(condition: any): ChainableTestAPI<ExtraContext>
}
type ChainableSuiteAPI<ExtraContext = {}> = ChainableFunction<
'concurrent' | 'only' | 'skip' | 'todo' | 'shuffle',
[name: string, factory?: SuiteFactory],
SuiteCollector<ExtraContext>
>
export type SuiteAPI<ExtraContext = {}> = ChainableSuiteAPI & {
each: SuiteEachFunction
skipIf(condition: any): ChainableSuiteAPI<ExtraContext>
runIf(condition: any): ChainableSuiteAPI<ExtraContext>
}
export type HookListener<T extends any[], Return = void> = (...args: T) => Awaitable<Return>
export type HookCleanupCallback = (() => Awaitable<unknown>) | void
export interface SuiteHooks {
beforeAll: HookListener<[Suite | File], HookCleanupCallback>[]
afterAll: HookListener<[Suite | File]>[]
beforeEach: HookListener<[TestContext, Suite], HookCleanupCallback>[]
afterEach: HookListener<[TestContext, Suite]>[]
}
export interface SuiteCollector<ExtraContext = {}> {
readonly name: string
readonly mode: RunMode
type: 'collector'
test: TestAPI<ExtraContext>
tasks: (Suite | Test | SuiteCollector<ExtraContext>)[]
collect: (file?: File) => Promise<Suite>
clear: () => void
on: <T extends keyof SuiteHooks>(name: T, ...fn: SuiteHooks[T]) => void
}
export type SuiteFactory = (test: (name: string, fn: TestFunction) => void) => Awaitable<void>
export interface RuntimeContext {
tasks: (SuiteCollector | Test)[]
currentSuite: SuiteCollector | null
}
export interface TestContext {
/**
* @deprecated Use promise instead
*/
(error?: any): void
/**
* Metadata of the current test
*/
meta: Readonly<Test>
/**
* A expect instance bound to the test
*/
expect: Vi.ExpectStatic
}