Skip to content

Commit cb7a99d

Browse files
🐛(vitest) Fix binding in recursive buildTest losing properties
Don't bind testFnExtended[key] as .bind() creates a new function without own properties, breaking property enumeration in recursive calls. Guard testFn[key] access against testFn being undefined/null. Co-authored-by: Nicolas DUBIEN <[email protected]>
1 parent 7d88e02 commit cb7a99d

1 file changed

Lines changed: 5 additions & 7 deletions

File tree

packages/vitest/src/internals/TestBuilder.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -143,15 +143,13 @@ export function buildTest<T extends (...args: any[]) => any>(
143143
const key = unsafeKey as keyof typeof testFnExtended & string;
144144
if (!ancestors.has(key) && typeof testFnExtended[key] === 'function') {
145145
atLeastOneExtra = true;
146+
const testFnChild = testFn != null ? (testFn as any)[key] : undefined;
147+
const testFnChildBound =
148+
typeof testFnChild === 'function' ? (testFnChild as (...args: unknown[]) => unknown).bind(testFn) : testFnChild;
146149
extraKeys[key] =
147150
key !== 'each'
148-
? buildTest(
149-
((testFn as any)[key] as (...args: unknown[]) => unknown).bind(testFn),
150-
(testFnExtended[key] as (...args: unknown[]) => unknown).bind(testFnExtended) as any,
151-
fc,
152-
new Set([...ancestors, key]),
153-
)
154-
: ((testFn as any)[key] as (...args: unknown[]) => unknown).bind(testFn);
151+
? buildTest(testFnChildBound, testFnExtended[key] as any, fc, new Set([...ancestors, key]))
152+
: testFnChildBound;
155153
}
156154
}
157155
if (!atLeastOneExtra) {

0 commit comments

Comments
 (0)