🐛(vitest) Fix runtime exception with test.each#6804
Conversation
…nction When building the enriched test function, the each method was extracted from testFnExtended (created by createTaskCollector) without proper this binding. This caused a 'Cannot read properties of undefined (reading withContext)' error when calling test.each(). Fix: use the original vitest testFn's each method with proper binding, since each is a standard vitest feature that doesn't need fast-check wrapping. Fixes #6798 Co-authored-by: Nicolas DUBIEN <[email protected]>
🦋 Changeset detectedLatest commit: e2e0f70 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
test.each
@fast-check/ava
fast-check
@fast-check/jest
@fast-check/packaged
@fast-check/poisoning
@fast-check/vitest
@fast-check/worker
commit: |
Apply the same .bind(testFn) fix to the non-each branch of the loop, ensuring methods like skip, only, concurrent etc. also retain proper this context when accessed through the enriched test function. Co-authored-by: Nicolas DUBIEN <[email protected]>
| ? buildTest((testFn as any)[key], testFnExtended[key] as any, fc, new Set([...ancestors, key])) | ||
| : testFnExtended[key]; | ||
| ? buildTest(((testFn as any)[key] as (...args: unknown[]) => unknown).bind(testFn), testFnExtended[key] as any, fc, new Set([...ancestors, key])) | ||
| : ((testFn as any)[key] as (...args: unknown[]) => unknown).bind(testFn); |
There was a problem hiding this comment.
Claude finished @dubzzz's task in 40s —— View job
- Read the code around line 149 to understand what needs binding
- Implement the fix
- Push the change
Added .bind(testFnExtended) to the testFnExtended[key] argument in the recursive buildTest call on line 148, so the extended fn also retains proper this context.
Change: b809fde
Co-authored-by: Nicolas DUBIEN <[email protected]>
Applied by GitHub Action workflow: PR Format Run ID: 23822582479 Target PR: 6804
This comment has been minimized.
This comment has been minimized.
|
Claude finished @dubzzz's task in 11m 2s —— View job
Root causeThe previous commits had two issues:
Fix
Change: | |
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]>
Co-authored-by: Nicolas DUBIEN <[email protected]>
Bind testFnExtended[key] to preserve proper this context, using Object.defineProperties to copy own property descriptors back onto the bound function so that Object.getOwnPropertyNames still finds sub-properties for recursion. Re-add null check on testFn since it can be undefined at deeper recursion levels. Co-authored-by: Nicolas DUBIEN <[email protected]> Co-authored-by: Claude Opus 4.6 <[email protected]>
Drop Object.defineProperties wrapper and use simple .bind() matching the pattern used for testFn binding. Co-authored-by: Nicolas DUBIEN <[email protected]>
Applied by GitHub Action workflow: PR Format Run ID: 23925298609 Target PR: 6804
…er (#6910) Fixes PR #6804. The `each` method (e.g. `it.each` / `test.each`) in Vitest requires a valid `this` context, as it uses `this` internally to access class methods. When assigning via `testFn[key]`, the context was lost, leading to errors when calling `it.each(...)`. Fix: explicitly bind the method to `testFn` via `.bind(testFn)`, ensuring the correct context is used when calling.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #6804 +/- ##
==========================================
- Coverage 94.82% 94.75% -0.08%
==========================================
Files 209 211 +2
Lines 5665 5778 +113
Branches 1489 1526 +37
==========================================
+ Hits 5372 5475 +103
- Misses 285 295 +10
Partials 8 8
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
When building the enriched test function, the each method was extracted from testFnExtended (created by createTaskCollector) without proper this binding. This caused a 'Cannot read properties of undefined (reading withContext)' error when calling test.each().
Fix: use the original vitest testFn's each method with proper binding, since each is a standard vitest feature that doesn't need fast-check wrapping.
Fixes #6798
Description
Fixes #issue-number
Checklist
— Don't delete this checklist and make sure you do the following before opening the PR
pnpm run bumpor by following the instructions from the changeset bot🐛(vitest) Something...) when the change targets a package other thanfast-check