diff --git a/packages/jest-jasmine2/src/tree_processor.js b/packages/jest-jasmine2/src/tree_processor.js index 8f5d88835ca9..0ebcc01a59da 100644 --- a/packages/jest-jasmine2/src/tree_processor.js +++ b/packages/jest-jasmine2/src/tree_processor.js @@ -43,32 +43,29 @@ export default function treeProcessor(options: Options) { return parentEnabled || runnableIds.indexOf(node.id) !== -1; } - return queueRunnerFactory({ - onException: error => tree.onException(error), - queueableFns: wrapChildren(tree, isEnabled(tree, false)), - userContext: tree.sharedUserContext(), - }); - - function executeNode(node, parentEnabled) { + function getNodeHandler(node: TreeNode, parentEnabled: boolean) { const enabled = isEnabled(node, parentEnabled); - if (!node.children) { - return { - fn(done) { - node.execute(done, enabled); - }, - }; - } - return { - async fn(done) { - nodeStart(node); - await queueRunnerFactory({ - onException: error => node.onException(error), - queueableFns: wrapChildren(node, enabled), - userContext: node.sharedUserContext(), - }); - nodeComplete(node); - done(); - }, + return node.children + ? getNodeWithChildrenHandler(node, enabled) + : getNodeWithoutChildrenHandler(node, enabled); + } + + function getNodeWithoutChildrenHandler(node: TreeNode, enabled: boolean) { + return function fn(done: (error?: any) => void = () => {}) { + node.execute(done, enabled); + }; + } + + function getNodeWithChildrenHandler(node: TreeNode, enabled: boolean) { + return async function fn(done: (error?: any) => void = () => {}) { + nodeStart(node); + await queueRunnerFactory({ + onException: error => node.onException(error), + queueableFns: wrapChildren(node, enabled), + userContext: node.sharedUserContext(), + }); + nodeComplete(node); + done(); }; } @@ -76,7 +73,12 @@ export default function treeProcessor(options: Options) { if (!node.children) { throw new Error('`node.children` is not defined.'); } - const children = node.children.map(child => executeNode(child, enabled)); + const children = node.children.map(child => ({ + fn: getNodeHandler(child, enabled), + })); return node.beforeAllFns.concat(children).concat(node.afterAllFns); } + + const treeHandler = getNodeHandler(tree, false); + return treeHandler(); }