/
tasks.ts
86 lines (71 loc) · 2.29 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
import type { ChainableFunction } from '../runtime/chain'
import type { Awaitable, ErrorWithDiff, MutableArray } 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
suite?: Suite
file?: File
result?: TaskResult
logs?: UserConsoleLog[]
}
export interface TaskResult {
state: TaskState
duration?: number
error?: ErrorWithDiff
}
export type TaskResultPack = [id: string, result: TaskResult | undefined]
export interface Suite extends TaskBase {
type: 'suite'
tasks: Task[]
}
export interface File extends Suite {
filepath: string
collectDuration?: number
}
export interface Test extends TaskBase {
type: 'test'
suite: Suite
result?: TaskResult
fails?: boolean
}
export type Task = Test | Suite | File
export type DoneCallback = (error?: any) => void
export type TestFunction = (done: DoneCallback) => Awaitable<void>
export type EachFunction = <T>(cases: T[] | readonly T[]) => (name: string, fn: (...args: T extends any[] | readonly any[] ? MutableArray<T> : [T]) => void) => void
export type TestAPI = ChainableFunction<
'concurrent' | 'only' | 'skip' | 'todo' | 'fails',
[name: string, fn?: TestFunction, timeout?: number],
void
> & { each: EachFunction }
export type SuiteAPI = ChainableFunction<
'concurrent' | 'only' | 'skip' | 'todo',
[name: string, factory?: SuiteFactory],
SuiteCollector
> & { each: EachFunction }
export type HookListener<T extends any[]> = (...args: T) => Awaitable<void>
export interface SuiteHooks {
beforeAll: HookListener<[Suite]>[]
afterAll: HookListener<[Suite]>[]
beforeEach: HookListener<[Test, Suite]>[]
afterEach: HookListener<[Test, Suite]>[]
}
export interface SuiteCollector {
readonly name: string
readonly mode: RunMode
type: 'collector'
test: TestAPI
tasks: (Suite | Test | SuiteCollector)[]
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
}