UNPKG

2.01 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, '__esModule', {
4 value: true
5});
6exports.default = treeProcessor;
7/**
8 * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
9 *
10 * This source code is licensed under the MIT license found in the
11 * LICENSE file in the root directory of this source tree.
12 */
13
14// eslint-disable-next-line @typescript-eslint/no-empty-function
15const noop = () => {};
16function treeProcessor(options) {
17 const {nodeComplete, nodeStart, queueRunnerFactory, runnableIds, tree} =
18 options;
19 function isEnabled(node, parentEnabled) {
20 return parentEnabled || runnableIds.indexOf(node.id) !== -1;
21 }
22 function getNodeHandler(node, parentEnabled) {
23 const enabled = isEnabled(node, parentEnabled);
24 return node.children
25 ? getNodeWithChildrenHandler(node, enabled)
26 : getNodeWithoutChildrenHandler(node, enabled);
27 }
28 function getNodeWithoutChildrenHandler(node, enabled) {
29 return function fn(done = noop) {
30 node.execute(done, enabled);
31 };
32 }
33 function getNodeWithChildrenHandler(node, enabled) {
34 return async function fn(done = noop) {
35 nodeStart(node);
36 await queueRunnerFactory({
37 onException: error => node.onException(error),
38 queueableFns: wrapChildren(node, enabled),
39 userContext: node.sharedUserContext()
40 });
41 nodeComplete(node);
42 done();
43 };
44 }
45 function hasNoEnabledTest(node) {
46 return (
47 node.disabled ||
48 node.markedPending ||
49 (node.children?.every(hasNoEnabledTest) ?? false)
50 );
51 }
52 function wrapChildren(node, enabled) {
53 if (!node.children) {
54 throw new Error('`node.children` is not defined.');
55 }
56 const children = node.children.map(child => ({
57 fn: getNodeHandler(child, enabled)
58 }));
59 if (hasNoEnabledTest(node)) {
60 return children;
61 }
62 return node.beforeAllFns.concat(children).concat(node.afterAllFns);
63 }
64 const treeHandler = getNodeHandler(tree, false);
65 return treeHandler();
66}