-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy pathtest-fn.d.cts
238 lines (191 loc) · 8.15 KB
/
test-fn.d.cts
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
import type {Assertions} from './assertions.cjs';
import type {Subscribable} from './subscribable.cjs';
import type {TryFn} from './try-fn.cjs';
/** The `t` value passed to test & hook implementations. */
export type ExecutionContext<Context = unknown> = {
/** Test context, shared with hooks. */
context: Context;
/** Title of the test or hook. */
readonly title: string;
/** Whether the test has passed. Only accurate in afterEach hooks. */
readonly passed: boolean;
readonly log: LogFn;
readonly plan: PlanFn;
readonly teardown: TeardownFn;
readonly timeout: TimeoutFn;
readonly try: TryFn<Context>;
} & Assertions;
export type LogFn = {
/** Log one or more values. */
(...values: any[]): void;
/** Skip logging. */
skip(...values: any[]): void;
};
export type PlanFn = {
/**
* Plan how many assertion there are in the test. The test will fail if the actual assertion count doesn't match the
* number of planned assertions. See [assertion planning](https://github.com/avajs/ava#assertion-planning).
*/
(count: number): void;
/** Don't plan assertions. */
skip(count: number): void;
};
export type TimeoutFn = {
/**
* Set a timeout for the test, in milliseconds. The test will fail if the timeout is exceeded.
* The timeout is reset each time an assertion is made.
*/
(ms: number, message?: string): void;
/** Clear the timeout and restore the default behavior. */
clear(): void;
};
/** Declare a function to be run after the test has ended. */
export type TeardownFn = (fn: (() => Promise<void>) | (() => void)) => void;
export type ImplementationFn<Args extends unknown[], Context = unknown> =
((t: ExecutionContext<Context>, ...args: Args) => PromiseLike<void>) |
((t: ExecutionContext<Context>, ...args: Args) => Subscribable) |
((t: ExecutionContext<Context>, ...args: Args) => void);
export type TitleFn<Args extends unknown[]> = (providedTitle: string | undefined, ...args: Args) => string;
/** A reusable test or hook implementation. */
export type Macro<Args extends unknown[], Context = unknown> = {
/** The function that is executed when the macro is used. */
readonly exec: ImplementationFn<Args, Context>;
/** Generates a test title when this macro is used. */
readonly title?: TitleFn<Args>;
};
/** A test or hook implementation. */
export type Implementation<Args extends unknown[], Context = unknown> = ImplementationFn<Args, Context> | Macro<Args, Context>;
interface AvaContext {}
export type TestFn<Context = AvaContext> = {
/** Declare a concurrent test. Additional arguments are passed to the implementation or macro. */
<Args extends unknown[]>(title: string, implementation: Implementation<Args, Context>, ...args: Args): void;
/**
* Declare a concurrent test that uses a macro. Additional arguments are passed to the macro.
* The macro is responsible for generating a unique test title.
*/
<Args extends unknown[]>(macro: Macro<Args, Context>, ...args: Args): void;
after: AfterFn<Context>;
afterEach: AfterFn<Context>;
before: BeforeFn<Context>;
beforeEach: BeforeFn<Context>;
failing: FailingFn<Context>;
macro: MacroFn<Context>;
meta: Meta;
only: OnlyFn<Context>;
serial: SerialFn<Context>;
skip: SkipFn<Context>;
todo: TodoFn;
};
export type AfterFn<Context = unknown> = {
/**
* Declare a hook that is run once, after all tests have passed.
* Additional arguments are passed to the implementation or macro.
*/
<Args extends unknown[]>(title: string, implementation: Implementation<Args, Context>, ...args: Args): void;
/**
* Declare a hook that is run once, after all tests have passed.
* Additional arguments are passed to the implementation or macro.
*/
<Args extends unknown[]>(implementation: Implementation<Args, Context>, ...args: Args): void;
always: AlwaysInterface<Context>;
skip: HookSkipFn<Context>;
};
export type AlwaysInterface<Context = unknown> = {
/**
* Declare a hook that is run once, after all tests are done.
* Additional arguments are passed to the implementation or macro.
*/
<Args extends unknown[]>(title: string, implementation: Implementation<Args, Context>, ...args: Args): void;
/**
* Declare a hook that is run once, after all tests are done.
* Additional arguments are passed to the implementation or macro.
*/
<Args extends unknown[]>(implementation: Implementation<Args, Context>, ...args: Args): void;
skip: HookSkipFn<Context>;
};
export type BeforeFn<Context = unknown> = {
/**
* Declare a hook that is run once, before all tests.
* Additional arguments are passed to the implementation or macro.
*/
<Args extends unknown[]>(title: string, implementation: Implementation<Args, Context>, ...args: Args): void;
/**
* Declare a hook that is run once, before all tests.
* Additional arguments are passed to the implementation or macro.
*/
<Args extends unknown[]>(implementation: Implementation<Args, Context>, ...args: Args): void;
skip: HookSkipFn<Context>;
};
export type FailingFn<Context = unknown> = {
/**
* Declare a concurrent test that is expected to fail.
* Additional arguments are passed to the implementation or macro.
*/
<Args extends unknown[]>(title: string, implementation: Implementation<Args, Context>, ...args: Args): void;
/**
* Declare a concurrent test, using a macro, that is expected to fail.
* Additional arguments are passed to the macro. The macro is responsible for generating a unique test title.
*/
<Args extends unknown[]>(macro: Macro<Args, Context>, ...args: Args): void;
only: OnlyFn<Context>;
skip: SkipFn<Context>;
};
export type HookSkipFn<Context = unknown> = {
/** Skip this hook. */
<Args extends unknown[]>(title: string, implementation: Implementation<Args, Context>, ...args: Args): void;
/** Skip this hook. */
<Args extends unknown[]>(implementation: Implementation<Args, Context>, ...args: Args): void;
};
export type OnlyFn<Context = unknown> = {
/**
* Declare a test. Only this test and others declared with `.only()` are run.
* Additional arguments are passed to the implementation or macro.
*/
<Args extends unknown[]>(title: string, implementation: Implementation<Args, Context>, ...args: Args): void;
/**
* Declare a test that uses a macro. Only this test and others declared with `.only()` are run.
* Additional arguments are passed to the macro. The macro is responsible for generating a unique test title.
*/
<Args extends unknown[]>(macro: Macro<Args, Context>, ...args: Args): void;
};
export type SerialFn<Context = unknown> = {
/** Declare a serial test. Additional arguments are passed to the implementation or macro. */
<Args extends unknown[]>(title: string, implementation: Implementation<Args, Context>, ...args: Args): void;
/**
* Declare a serial test that uses a macro. The macro is responsible for generating a unique test title.
*/
<Args extends unknown[]>(macro: Macro<Args, Context>, ...args: Args): void;
after: AfterFn<Context>;
afterEach: AfterFn<Context>;
before: BeforeFn<Context>;
beforeEach: BeforeFn<Context>;
failing: FailingFn<Context>;
only: OnlyFn<Context>;
skip: SkipFn<Context>;
todo: TodoFn;
};
export type SkipFn<Context = unknown> = {
/** Skip this test. */
<Args extends unknown[]>(title: string, implementation: Implementation<Args, Context>, ...args: Args): void;
/** Skip this test. */
<Args extends unknown[]>(macro: Macro<Args, Context>, ...args: Args): void;
};
/** Declare a test that should be implemented later. */
export type TodoFn = (title: string) => void;
export type MacroDeclarationOptions<Args extends unknown[], Context = unknown> = {
/** The function that is executed when the macro is used. */
exec: ImplementationFn<Args, Context>;
/** The function responsible for generating a unique title when the macro is used. */
title: TitleFn<Args>;
};
export type MacroFn<Context = unknown> = {
/** Declare a reusable test implementation. */
<Args extends unknown[]>(/** The function that is executed when the macro is used. */ exec: ImplementationFn<Args, Context>): Macro<Args, Context>;
<Args extends unknown[]>(declaration: MacroDeclarationOptions<Args, Context>): Macro<Args, Context>;
};
export type Meta = {
/** Path to the test file being executed. */
file: string;
/** Directory where snapshots are stored. */
snapshotDirectory: string;
};