Skip to content

Commit

Permalink
Refactor makeTestResults function to use a stack for traversal
Browse files Browse the repository at this point in the history
  • Loading branch information
mbelsky committed Dec 27, 2023
1 parent 896573b commit b266bd3
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 additions & 7 deletions packages/jest-circus/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -391,17 +391,22 @@ export const makeSingleTestResult = (
const makeTestResults = (
describeBlock: Circus.DescribeBlock,
): Circus.TestResults => {
const testResults: Circus.TestResults = [];
const testResults = [];
const stack: [[Circus.DescribeBlock, number]] = [[describeBlock, 0]];

for (const child of describeBlock.children) {
switch (child.type) {
case 'describeBlock': {
testResults.push(...makeTestResults(child));
while (stack.length > 0) {
const [currentBlock, childIndex] = stack.pop()!;

for (let i = childIndex; i < currentBlock.children.length; i++) {
const child = currentBlock.children[i];

if (child.type === 'describeBlock') {
stack.push([currentBlock, i + 1]);
stack.push([child, 0]);
break;
}
case 'test': {
if (child.type === 'test') {
testResults.push(makeSingleTestResult(child));
break;
}
}
}
Expand Down

0 comments on commit b266bd3

Please sign in to comment.