Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor makeTestResults function to use a stack for traversal #14760

Merged
merged 5 commits into from
Dec 27, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Refactor makeTestResults function to use a stack for traversal
  • Loading branch information
mbelsky committed Dec 27, 2023
commit b266bd30549f4c776af02c87b0f0af9596e340c5
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