-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
/
Circus.ts
274 lines (254 loc) · 7.09 KB
/
Circus.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
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
/**
* 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 * as Global from './Global';
type Process = NodeJS.Process;
export type DoneFn = Global.DoneFn;
export type BlockFn = Global.BlockFn;
export type BlockName = Global.BlockName;
export type BlockNameLike = Global.BlockNameLike;
export type BlockMode = void | 'skip' | 'only' | 'todo';
export type TestMode = BlockMode;
export type TestName = Global.TestName;
export type TestNameLike = Global.TestNameLike;
export type TestFn = Global.TestFn;
export type ConcurrentTestFn = Global.ConcurrentTestFn;
export type HookFn = Global.HookFn;
export type AsyncFn = TestFn | HookFn;
export type SharedHookType = 'afterAll' | 'beforeAll';
export type HookType = SharedHookType | 'afterEach' | 'beforeEach';
export type TestContext = Global.TestContext;
export type Exception = any; // Since in JS anything can be thrown as an error.
export type FormattedError = string; // String representation of error.
export type Hook = {
asyncError: Error;
fn: HookFn;
type: HookType;
parent: DescribeBlock;
seenDone: boolean;
timeout: number | undefined | null;
};
export interface EventHandler {
(event: AsyncEvent, state: State): void | Promise<void>;
(event: SyncEvent, state: State): void;
}
export type Event = SyncEvent | AsyncEvent;
interface JestGlobals extends Global.TestFrameworkGlobals {
// we cannot type `expect` properly as it'd create circular dependencies
expect: unknown;
}
export type SyncEvent =
| {
asyncError: Error;
mode: BlockMode;
name: 'start_describe_definition';
blockName: BlockName;
}
| {
mode: BlockMode;
name: 'finish_describe_definition';
blockName: BlockName;
}
| {
asyncError: Error;
name: 'add_hook';
hookType: HookType;
fn: HookFn;
timeout: number | undefined;
}
| {
asyncError: Error;
name: 'add_test';
testName: TestName;
fn: TestFn;
mode?: TestMode;
concurrent: boolean;
timeout: number | undefined;
failing: boolean;
}
| {
// Any unhandled error that happened outside of test/hooks (unless it is
// an `afterAll` hook)
name: 'error';
error: Exception;
};
export type AsyncEvent =
| {
// first action to dispatch. Good time to initialize all settings
name: 'setup';
testNamePattern?: string;
runtimeGlobals: JestGlobals;
parentProcess: Process;
}
| {
name: 'include_test_location_in_result';
}
| {
name: 'hook_start';
hook: Hook;
}
| {
name: 'hook_success';
describeBlock?: DescribeBlock;
test?: TestEntry;
hook: Hook;
}
| {
name: 'hook_failure';
error: string | Exception;
describeBlock?: DescribeBlock;
test?: TestEntry;
hook: Hook;
}
| {
name: 'test_fn_start';
test: TestEntry;
}
| {
name: 'test_fn_success';
test: TestEntry;
}
| {
name: 'test_fn_failure';
error: Exception;
test: TestEntry;
}
| {
name: 'test_retry';
test: TestEntry;
}
| {
// the `test` in this case is all hooks + it/test function, not just the
// function passed to `it/test`
name: 'test_start';
test: TestEntry;
}
| {
name: 'test_skip';
test: TestEntry;
}
| {
name: 'test_todo';
test: TestEntry;
}
| {
name: 'test_started';
test: TestEntry;
}
| {
// test failure is defined by presence of errors in `test.errors`,
// `test_done` indicates that the test and all its hooks were run,
// and nothing else will change it's state in the future. (except third
// party extentions/plugins)
name: 'test_done';
test: TestEntry;
}
| {
name: 'run_describe_start';
describeBlock: DescribeBlock;
}
| {
name: 'run_describe_finish';
describeBlock: DescribeBlock;
}
| {
name: 'run_start';
}
| {
name: 'run_finish';
}
| {
// Action dispatched after everything is finished and we're about to wrap
// things up and return test results to the parent process (caller).
name: 'teardown';
};
export type MatcherResults = {
actual: unknown;
expected: unknown;
name: string;
pass: boolean;
};
export type TestStatus = 'skip' | 'done' | 'todo';
export type TestNamesPath = Array<TestName | BlockName>;
export type TestCaseStartInfo = {
ancestorTitles: Array<string>;
fullName: string;
mode: TestMode;
title: string;
startedAt?: number | null;
};
export type TestResult = {
duration?: number | null;
errors: Array<FormattedError>;
errorsDetailed: Array<MatcherResults | unknown>;
invocations: number;
status: TestStatus;
location?: {column: number; line: number} | null;
numPassingAsserts: number;
retryReasons: Array<FormattedError>;
testPath: TestNamesPath;
};
export type RunResult = {
unhandledErrors: Array<FormattedError>;
testResults: TestResults;
};
export type TestResults = Array<TestResult>;
export type GlobalErrorHandlers = {
uncaughtException: Array<(exception: Exception) => void>;
unhandledRejection: Array<
(exception: Exception, promise: Promise<unknown>) => void
>;
};
export type State = {
currentDescribeBlock: DescribeBlock;
currentlyRunningTest?: TestEntry | null; // including when hooks are being executed
expand?: boolean; // expand error messages
hasFocusedTests: boolean; // that are defined using test.only
hasStarted: boolean; // whether the rootDescribeBlock has started running
// Store process error handlers. During the run we inject our own
// handlers (so we could fail tests on unhandled errors) and later restore
// the original ones.
originalGlobalErrorHandlers?: GlobalErrorHandlers;
parentProcess: Process | null; // process object from the outer scope
randomize?: boolean;
rootDescribeBlock: DescribeBlock;
seed: number;
testNamePattern?: RegExp | null;
testTimeout: number;
unhandledErrors: Array<Exception>;
includeTestLocationInResult: boolean;
maxConcurrency: number;
};
export type DescribeBlock = {
type: 'describeBlock';
children: Array<DescribeBlock | TestEntry>;
hooks: Array<Hook>;
mode: BlockMode;
name: BlockName;
parent?: DescribeBlock;
/** @deprecated Please get from `children` array instead */
tests: Array<TestEntry>;
};
export type TestError = Exception | [Exception | undefined, Exception]; // the error from the test, as well as a backup error for async
export type TestEntry = {
type: 'test';
asyncError: Exception; // Used if the test failure contains no usable stack trace
errors: Array<TestError>;
retryReasons: Array<TestError>;
fn: TestFn;
invocations: number;
mode: TestMode;
concurrent: boolean;
name: TestName;
numPassingAsserts: number;
parent: DescribeBlock;
startedAt?: number | null;
duration?: number | null;
seenDone: boolean;
status?: TestStatus | null; // whether the test has been skipped or run already
timeout?: number;
failing: boolean;
};