Skip to content

Commit 830ff9e

Browse files
alexander-turnerclaude
authored andcommitted
refactor: single-pass tree walk in collectTestsWithoutRunning
Replace two-pass approach (collect entries then map with getTestNamesPath walking back up the parent chain) with a single recursive walk that tracks ancestors as it descends. This also removes the need to export getTestNamesPath from utils.ts. https://claude.ai/code/session_018SbTXFCAujDRRjBFtMTFXU
1 parent 40aefb0 commit 830ff9e

File tree

2 files changed

+28
-39
lines changed

2 files changed

+28
-39
lines changed

packages/jest-circus/src/legacy-code-todo-rewrite/jestAdapterInit.ts

Lines changed: 27 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ import {
3333
} from '../state';
3434
import testCaseReportHandler from '../testCaseReportHandler';
3535
import {unhandledRejectionHandler} from '../unhandledRejectionHandler';
36-
import {getTestID, getTestNamesPath} from '../utils';
36+
import {getTestID} from '../utils';
3737

3838
interface RuntimeGlobals extends Global.TestFrameworkGlobals {
3939
expect: JestExpect;
@@ -149,52 +149,43 @@ export const collectTestsWithoutRunning = async ({
149149
}): Promise<TestResult> => {
150150
const {rootDescribeBlock, testNamePattern} = getRunnerState();
151151

152-
const collectEntries = (
152+
const assertionResults: Array<AssertionResult> = [];
153+
154+
const walk = (
153155
block: Circus.DescribeBlock,
154-
): Array<Circus.TestEntry> => {
155-
const entries: Array<Circus.TestEntry> = [];
156+
ancestors: Array<string>,
157+
): void => {
156158
for (const child of block.children) {
157-
if (child.type === 'test') {
159+
if (child.type === 'describeBlock') {
160+
walk(child, [...ancestors, child.name]);
161+
} else if (child.type === 'test') {
158162
if (!testNamePattern || testNamePattern.test(getTestID(child))) {
159-
entries.push(child);
163+
const title = child.name;
164+
assertionResults.push({
165+
ancestorTitles: [...ancestors],
166+
duration: null,
167+
failing: false,
168+
failureDetails: [],
169+
failureMessages: [],
170+
fullName: [...ancestors, title].join(' '),
171+
invocations: 0,
172+
location: null,
173+
numPassingAsserts: 0,
174+
retryReasons: [],
175+
startAt: null,
176+
status: 'pending' as Status,
177+
title,
178+
});
160179
}
161-
} else if (child.type === 'describeBlock') {
162-
entries.push(...collectEntries(child));
163180
}
164181
}
165-
return entries;
166182
};
167-
const testEntries = collectEntries(rootDescribeBlock);
168-
169-
const assertionResults: Array<AssertionResult> = testEntries.map(test => {
170-
const ancestorTitles = getTestNamesPath(test).filter(
171-
name => name !== ROOT_DESCRIBE_BLOCK_NAME,
172-
);
173-
const title = ancestorTitles.pop() ?? '';
174-
175-
return {
176-
ancestorTitles,
177-
duration: null,
178-
failing: false,
179-
failureDetails: [],
180-
failureMessages: [],
181-
fullName: [...ancestorTitles, title].join(' '),
182-
invocations: 0,
183-
location: null,
184-
numPassingAsserts: 0,
185-
retryReasons: [],
186-
startAt: null,
187-
status: 'pending' as Status,
188-
title,
189-
};
190-
});
183+
walk(rootDescribeBlock, []);
191184

192185
await dispatch({name: 'teardown'});
193186

194-
const emptyTestResult = createEmptyTestResult();
195-
196187
return {
197-
...emptyTestResult,
188+
...createEmptyTestResult(),
198189
displayName: config.displayName,
199190
numPendingTests: assertionResults.length,
200191
testFilePath: testPath,

packages/jest-circus/src/utils.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -323,9 +323,7 @@ export const makeRunResult = (
323323
unhandledErrors: unhandledErrors.map(_getError).map(getErrorStack),
324324
});
325325

326-
export const getTestNamesPath = (
327-
test: Circus.TestEntry,
328-
): Circus.TestNamesPath => {
326+
const getTestNamesPath = (test: Circus.TestEntry): Circus.TestNamesPath => {
329327
const titles = [];
330328
let parent: Circus.TestEntry | Circus.DescribeBlock | undefined = test;
331329
do {

0 commit comments

Comments
 (0)