fix: dataset inheritance with method chaining (beforeEach()->with(), describe()->with()) #1565
+18
−14
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
What:
Description
Datasets are not applied when using method chaining patterns with hooks and describe blocks:
Error:
Root Cause
Multiple Pest functions were using
Backtrace::file()to determine the current test file. This method returns the immediate caller's filename, which breaks with method chaining:The callback was stored with the wrong filename key, so tests couldn't find their datasets.
Solution
Use
Backtrace::testFile()instead, which walks the entire backtrace to find the actual test file being executed. This matches the pattern already used by Pest'stest()anddescribe()functions.Changes
src/Functions.php - 8 functions updated:
beforeEach()- fixesbeforeEach()->with()patternafterEach()- consistency with beforeEachbeforeAll()- better error messagesafterAll()- better error messagespest()- fixespest()->beforeEach()patternuses()- consistency with pest()covers()- correct test file contextmutates()- correct test file contextsrc/PendingCalls/DescribeCall.php - 3 fixes:
BeforeEachCalldestructor to run before test creation (timing fix)$this->filenameinstead of backtrace (optimization)Fixed Patterns
beforeEach()->with([...])describe()->with([...])pest()->beforeEach()->with([...])Testing
Consistency with Pest Design
This change extends an existing pattern in Pest's codebase:
Related
PR (Dockerfile): https://github.com/louisbels/pest/pull/new/fix-dockerfile-php83-build