-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
/
eventHandler.ts
203 lines (193 loc) · 6.28 KB
/
eventHandler.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
/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
import {EventHandler, TEST_TIMEOUT_SYMBOL} from './types';
import {
addErrorToEachTestUnderDescribe,
makeDescribe,
getTestDuration,
invariant,
makeTest,
describeBlockHasTests,
} from './utils';
import {
injectGlobalErrorHandlers,
restoreGlobalErrorHandlers,
} from './globalErrorHandlers';
const eventHandler: EventHandler = (event, state): void => {
switch (event.name) {
case 'include_test_location_in_result': {
state.includeTestLocationInResult = true;
break;
}
case 'hook_start': {
break;
}
case 'start_describe_definition': {
const {blockName, mode} = event;
const {currentDescribeBlock} = state;
const describeBlock = makeDescribe(blockName, currentDescribeBlock, mode);
currentDescribeBlock.children.push(describeBlock);
state.currentDescribeBlock = describeBlock;
break;
}
case 'finish_describe_definition': {
const {currentDescribeBlock} = state;
invariant(currentDescribeBlock, `currentDescribeBlock must be there`);
if (!describeBlockHasTests(currentDescribeBlock)) {
currentDescribeBlock.hooks.forEach(hook => {
hook.asyncError.message = `Invalid: ${
hook.type
}() may not be used in a describe block containing no tests.`;
state.unhandledErrors.push(hook.asyncError);
});
}
// inherit mode from its parent describe but
// do not inherit "only" mode when there is already tests with "only" mode
const shouldInheritMode = !(
currentDescribeBlock.mode === 'only' &&
currentDescribeBlock.tests.find(test => test.mode === 'only')
);
if (shouldInheritMode) {
currentDescribeBlock.tests.forEach(test => {
if (!test.mode) {
test.mode = currentDescribeBlock.mode;
}
});
}
if (
!state.hasFocusedTests &&
currentDescribeBlock.tests.some(test => test.mode === 'only')
) {
state.hasFocusedTests = true;
}
if (currentDescribeBlock.parent) {
state.currentDescribeBlock = currentDescribeBlock.parent;
}
break;
}
case 'add_hook': {
const {currentDescribeBlock} = state;
const {asyncError, fn, hookType: type, timeout} = event;
const parent = currentDescribeBlock;
currentDescribeBlock.hooks.push({asyncError, fn, parent, timeout, type});
break;
}
case 'add_test': {
const {currentDescribeBlock} = state;
const {asyncError, fn, mode, testName: name, timeout} = event;
const test = makeTest(
fn,
mode,
name,
currentDescribeBlock,
timeout,
asyncError,
);
if (test.mode === 'only') {
state.hasFocusedTests = true;
}
currentDescribeBlock.tests.push(test);
break;
}
case 'hook_failure': {
const {test, describeBlock, error, hook} = event;
const {asyncError, type} = hook;
if (type === 'beforeAll') {
invariant(describeBlock, 'always present for `*All` hooks');
addErrorToEachTestUnderDescribe(describeBlock!, error, asyncError);
} else if (type === 'afterAll') {
// Attaching `afterAll` errors to each test makes execution flow
// too complicated, so we'll consider them to be global.
state.unhandledErrors.push([error, asyncError]);
} else {
invariant(test, 'always present for `*Each` hooks');
test!.errors.push([error, asyncError]);
}
break;
}
case 'test_skip': {
event.test.status = 'skip';
break;
}
case 'test_todo': {
event.test.status = 'todo';
break;
}
case 'test_done': {
event.test.duration = getTestDuration(event.test);
event.test.status = 'done';
state.currentlyRunningTest = null;
break;
}
case 'test_start': {
state.currentlyRunningTest = event.test;
event.test.startedAt = Date.now();
event.test.invocations += 1;
break;
}
case 'test_fn_failure': {
const {
error,
test: {asyncError},
} = event;
event.test.errors.push([error, asyncError]);
break;
}
case 'test_retry': {
event.test.errors = [];
break;
}
case 'run_start': {
global[TEST_TIMEOUT_SYMBOL] &&
(state.testTimeout = global[TEST_TIMEOUT_SYMBOL]);
break;
}
case 'run_finish': {
break;
}
case 'setup': {
// Uncaught exception handlers should be defined on the parent process
// object. If defined on the VM's process object they just no op and let
// the parent process crash. It might make sense to return a `dispatch`
// function to the parent process and register handlers there instead, but
// i'm not sure if this is works. For now i just replicated whatever
// jasmine was doing -- dabramov
state.parentProcess = event.parentProcess;
invariant(state.parentProcess);
state.originalGlobalErrorHandlers = injectGlobalErrorHandlers(
state.parentProcess,
);
if (event.testNamePattern) {
state.testNamePattern = new RegExp(event.testNamePattern, 'i');
}
break;
}
case 'teardown': {
invariant(state.originalGlobalErrorHandlers);
invariant(state.parentProcess);
restoreGlobalErrorHandlers(
state.parentProcess!,
state.originalGlobalErrorHandlers!,
);
break;
}
case 'error': {
// It's very likely for long-running async tests to throw errors. In this
// case we want to catch them and fail the current test. At the same time
// there's a possibility that one test sets a long timeout, that will
// eventually throw after this test finishes but during some other test
// execution, which will result in one test's error failing another test.
// In any way, it should be possible to track where the error was thrown
// from.
state.currentlyRunningTest
? state.currentlyRunningTest.errors.push(event.error)
: state.unhandledErrors.push(event.error);
break;
}
}
};
export default eventHandler;