fix: dataset inheritance with method chaining (beforeEach()->with(), describe()->with())#1565
Open
louisbels wants to merge 1 commit intopestphp:4.xfrom
Open
fix: dataset inheritance with method chaining (beforeEach()->with(), describe()->with())#1565louisbels wants to merge 1 commit intopestphp:4.xfrom
louisbels wants to merge 1 commit intopestphp:4.xfrom
Conversation
…describe()->with()) Fixes issue where datasets were not applied when using method chaining patterns like beforeEach()->with([...]) or describe()->with([...]) inside nested describe blocks. Root cause: Multiple functions were using Backtrace::file() which returns the immediate caller's filename. This breaks when called through method chaining because the backtrace returns internal Pest files instead of the test file. Solution: Use Backtrace::testFile() which walks the entire backtrace to find the actual test file being executed. This matches the pattern already used by test() and describe() functions. Changes in src/Functions.php: - beforeEach(): Use testFile() to fix beforeEach()->with() pattern - afterEach(): Use testFile() for consistency with beforeEach() - beforeAll(): Use testFile() for better error messages - afterAll(): Use testFile() for better error messages - pest(): Use testFile() to fix pest()->beforeEach() pattern - uses(): Use testFile() for consistency with pest() - covers(): Use testFile() for correct test file context - mutates(): Use testFile() for correct test file context Changes in src/PendingCalls/DescribeCall.php: - __destruct(): Force BeforeEachCall destructor before test creation - __call(): Use $this->filename instead of Backtrace, more efficient - __call(): Properly merge describing context for nested describe blocks Fixes patterns: - beforeEach()->with([...]) - describe()->with([...]) - pest()->beforeEach()->with([...] Tests passing: - tests/Features/Describe.php (all dataset tests) - tests/Hooks/BeforeEachTest.php (global hook execution) - tests/Features/Expect/toMatchSnapshot.php (28 tests)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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