-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
/
Global.ts
183 lines (154 loc) · 4.89 KB
/
Global.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
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
import type {CoverageMapData} from 'istanbul-lib-coverage';
export type ValidTestReturnValues = void | undefined;
type TestReturnValuePromise = Promise<unknown>;
type TestReturnValueGenerator = Generator<void, unknown, void>;
export type TestReturnValue = ValidTestReturnValues | TestReturnValuePromise;
export type TestContext = Record<string, unknown>;
export type DoneFn = (reason?: string | Error) => void;
export type DoneTakingTestFn = (
this: TestContext,
done: DoneFn,
) => ValidTestReturnValues;
export type PromiseReturningTestFn = (this: TestContext) => TestReturnValue;
export type GeneratorReturningTestFn = (
this: TestContext,
) => TestReturnValueGenerator;
// eslint-disable-next-line @typescript-eslint/ban-types
export type NameLike = number | Function;
export type TestName = string;
export type TestNameLike = TestName | NameLike;
export type TestFn =
| PromiseReturningTestFn
| GeneratorReturningTestFn
| DoneTakingTestFn;
export type ConcurrentTestFn = () => TestReturnValuePromise;
export type BlockFn = () => void;
export type BlockName = string;
export type BlockNameLike = BlockName | NameLike;
export type HookFn = TestFn;
export type Col = unknown;
export type Row = ReadonlyArray<Col>;
export type Table = ReadonlyArray<Row>;
export type ArrayTable = Table | Row;
export type TemplateTable = TemplateStringsArray;
export type TemplateData = ReadonlyArray<unknown>;
export type EachTable = ArrayTable | TemplateTable;
export type TestCallback = BlockFn | TestFn | ConcurrentTestFn;
export type EachTestFn<EachCallback extends TestCallback> = (
...args: ReadonlyArray<any>
) => ReturnType<EachCallback>;
interface Each<EachFn extends TestFn | BlockFn> {
// when the table is an array of object literals
<T extends Record<string, unknown>>(
table: ReadonlyArray<T>,
): (
name: string | NameLike,
fn: (arg: T, done: DoneFn) => ReturnType<EachFn>,
timeout?: number,
) => void;
// when the table is an array of tuples
<T extends readonly [unknown, ...Array<unknown>]>(
table: ReadonlyArray<T>,
): (
name: string | NameLike,
fn: (...args: [...T]) => ReturnType<EachFn>,
timeout?: number,
) => void;
// when the table is an array of arrays
<T extends ReadonlyArray<unknown>>(
table: ReadonlyArray<T>,
): (
name: string | NameLike,
fn: (...args: T) => ReturnType<EachFn>,
timeout?: number,
) => void;
// when the table is a tuple or array
<T>(
table: ReadonlyArray<T>,
): (
name: string | NameLike,
fn: (arg: T, done: DoneFn) => ReturnType<EachFn>,
timeout?: number,
) => void;
// when the table is a template literal
<T extends Array<unknown>>(
strings: TemplateStringsArray,
...expressions: T
): (
name: string | NameLike,
fn: (arg: Record<string, T[number]>, done: DoneFn) => ReturnType<EachFn>,
timeout?: number,
) => void;
// when the table is a template literal with a type argument
<T extends Record<string, unknown>>(
strings: TemplateStringsArray,
...expressions: Array<unknown>
): (
name: string | NameLike,
fn: (arg: T, done: DoneFn) => ReturnType<EachFn>,
timeout?: number,
) => void;
}
export type HookBase = (fn: HookFn, timeout?: number) => void;
export interface Failing<T extends TestFn> {
(testName: TestNameLike, fn: T, timeout?: number): void;
each: Each<T>;
}
export interface ItBase {
(testName: TestNameLike, fn: TestFn, timeout?: number): void;
each: Each<TestFn>;
failing: Failing<TestFn>;
}
export interface It extends ItBase {
only: ItBase;
skip: ItBase;
todo: (testName: TestNameLike) => void;
}
export interface ItConcurrentBase {
(testName: TestNameLike, testFn: ConcurrentTestFn, timeout?: number): void;
each: Each<ConcurrentTestFn>;
failing: Failing<ConcurrentTestFn>;
}
export interface ItConcurrentExtended extends ItConcurrentBase {
only: ItConcurrentBase;
skip: ItConcurrentBase;
}
export interface ItConcurrent extends It {
concurrent: ItConcurrentExtended;
}
export interface DescribeBase {
(blockName: BlockNameLike, blockFn: BlockFn): void;
each: Each<BlockFn>;
}
export interface Describe extends DescribeBase {
only: DescribeBase;
skip: DescribeBase;
}
export interface TestFrameworkGlobals {
it: ItConcurrent;
test: ItConcurrent;
fit: ItBase & {concurrent?: ItConcurrentBase};
xit: ItBase;
xtest: ItBase;
describe: Describe;
xdescribe: DescribeBase;
fdescribe: DescribeBase;
beforeAll: HookBase;
beforeEach: HookBase;
afterEach: HookBase;
afterAll: HookBase;
}
export interface GlobalAdditions extends TestFrameworkGlobals {
__coverage__: CoverageMapData;
}
export interface Global
extends GlobalAdditions,
Omit<typeof globalThis, keyof GlobalAdditions> {
[extras: PropertyKey]: unknown;
}