-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
/
treeProcessor.ts
89 lines (79 loc) · 2.58 KB
/
treeProcessor.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
/**
* 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 type Suite from './jasmine/Suite';
type Options = {
nodeComplete: (suite: TreeNode) => void;
nodeStart: (suite: TreeNode) => void;
queueRunnerFactory: any;
runnableIds: Array<string>;
tree: TreeNode;
};
export type TreeNode = {
afterAllFns: Array<unknown>;
beforeAllFns: Array<unknown>;
disabled?: boolean;
execute: (onComplete: () => void, enabled: boolean) => void;
id: string;
onException: (error: Error) => void;
sharedUserContext: () => unknown;
children?: Array<TreeNode>;
} & Pick<Suite, 'getResult' | 'parentSuite' | 'result' | 'markedPending'>;
export default function treeProcessor(options: Options): void {
const {
nodeComplete,
nodeStart,
queueRunnerFactory,
runnableIds,
tree,
} = options;
function isEnabled(node: TreeNode, parentEnabled: boolean) {
return parentEnabled || runnableIds.indexOf(node.id) !== -1;
}
function getNodeHandler(node: TreeNode, parentEnabled: boolean) {
const enabled = isEnabled(node, parentEnabled);
return node.children
? getNodeWithChildrenHandler(node, enabled)
: getNodeWithoutChildrenHandler(node, enabled);
}
function getNodeWithoutChildrenHandler(node: TreeNode, enabled: boolean) {
return function fn(done: (error?: unknown) => void = () => {}) {
node.execute(done, enabled);
};
}
function getNodeWithChildrenHandler(node: TreeNode, enabled: boolean) {
return async function fn(done: (error?: unknown) => void = () => {}) {
nodeStart(node);
await queueRunnerFactory({
onException: (error: Error) => node.onException(error),
queueableFns: wrapChildren(node, enabled),
userContext: node.sharedUserContext(),
});
nodeComplete(node);
done();
};
}
function hasNoEnabledTest(node: TreeNode): boolean {
if (node.children) {
return node.children.every(hasNoEnabledTest);
}
return node.disabled || node.markedPending;
}
function wrapChildren(node: TreeNode, enabled: boolean) {
if (!node.children) {
throw new Error('`node.children` is not defined.');
}
const children = node.children.map(child => ({
fn: getNodeHandler(child, enabled),
}));
if (hasNoEnabledTest(node)) {
return children;
}
return node.beforeAllFns.concat(children).concat(node.afterAllFns);
}
const treeHandler = getNodeHandler(tree, false);
return treeHandler();
}