Skip to content

Commit

Permalink
refactor(jest-jasmine2): Clear the meaning of treeProcessor functions…
Browse files Browse the repository at this point in the history
… and match new Env.execute behavior

refactor(jest-jasmine2): Clear the meaning of treeProcessor functions and match new Env.execute behavior

fix flow

a
  • Loading branch information
niieani committed Mar 27, 2018
1 parent a55e7b4 commit 12bbefa
Showing 1 changed file with 28 additions and 26 deletions.
54 changes: 28 additions & 26 deletions packages/jest-jasmine2/src/tree_processor.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,40 +43,42 @@ 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();
};
}

function wrapChildren(node: TreeNode, enabled: boolean) {
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();
}

0 comments on commit 12bbefa

Please sign in to comment.