diff --git a/src/Functions.php b/src/Functions.php index 4b5d992b..cce5566b 100644 --- a/src/Functions.php +++ b/src/Functions.php @@ -47,7 +47,7 @@ if (! function_exists('beforeAll')) { function beforeAll(Closure $closure): void { if (DescribeCall::describing() !== []) { - $filename = Backtrace::file(); + $filename = Backtrace::testFile(); throw new BeforeAllWithinDescribe($filename); } @@ -64,7 +64,7 @@ if (! function_exists('beforeEach')) { */ function beforeEach(?Closure $closure = null): BeforeEachCall { - $filename = Backtrace::file(); + $filename = Backtrace::testFile(); return new BeforeEachCall(TestSuite::getInstance(), $filename, $closure); } @@ -107,7 +107,7 @@ if (! function_exists('uses')) { */ function uses(string ...$classAndTraits): UsesCall { - $filename = Backtrace::file(); + $filename = Backtrace::testFile(); return new UsesCall($filename, array_values($classAndTraits)); } @@ -119,7 +119,7 @@ if (! function_exists('pest')) { */ function pest(): Configuration { - return new Configuration(Backtrace::file()); + return new Configuration(Backtrace::testFile()); } } @@ -179,7 +179,7 @@ if (! function_exists('afterEach')) { */ function afterEach(?Closure $closure = null): AfterEachCall { - $filename = Backtrace::file(); + $filename = Backtrace::testFile(); return new AfterEachCall(TestSuite::getInstance(), $filename, $closure); } @@ -192,7 +192,7 @@ if (! function_exists('afterAll')) { function afterAll(Closure $closure): void { if (DescribeCall::describing() !== []) { - $filename = Backtrace::file(); + $filename = Backtrace::testFile(); throw new AfterAllWithinDescribe($filename); } @@ -209,7 +209,7 @@ if (! function_exists('covers')) { */ function covers(array|string ...$classesOrFunctions): void { - $filename = Backtrace::file(); + $filename = Backtrace::testFile(); $beforeEachCall = (new BeforeEachCall(TestSuite::getInstance(), $filename)); @@ -238,7 +238,7 @@ if (! function_exists('mutates')) { */ function mutates(array|string ...$targets): void { - $filename = Backtrace::file(); + $filename = Backtrace::testFile(); $beforeEachCall = (new BeforeEachCall(TestSuite::getInstance(), $filename)); $beforeEachCall->group('__pest_mutate_only'); diff --git a/src/PendingCalls/DescribeCall.php b/src/PendingCalls/DescribeCall.php index 712aedcb..d96b4a3c 100644 --- a/src/PendingCalls/DescribeCall.php +++ b/src/PendingCalls/DescribeCall.php @@ -5,7 +5,6 @@ declare(strict_types=1); namespace Pest\PendingCalls; use Closure; -use Pest\Support\Backtrace; use Pest\Support\Description; use Pest\TestSuite; @@ -53,7 +52,11 @@ final class DescribeCall */ public function __destruct() { - unset($this->currentBeforeEachCall); + // Ensure BeforeEachCall destructs before creating tests + // by moving to local scope and clearing the reference + $beforeEach = $this->currentBeforeEachCall; + $this->currentBeforeEachCall = null; + unset($beforeEach); // Trigger destructor immediately self::$describing[] = $this->description; @@ -71,12 +74,13 @@ final class DescribeCall */ public function __call(string $name, array $arguments): self { - $filename = Backtrace::file(); + if (! $this->currentBeforeEachCall instanceof \Pest\PendingCalls\BeforeEachCall) { + $this->currentBeforeEachCall = new BeforeEachCall(TestSuite::getInstance(), $this->filename); - if (! $this->currentBeforeEachCall instanceof BeforeEachCall) { - $this->currentBeforeEachCall = new BeforeEachCall(TestSuite::getInstance(), $filename); - - $this->currentBeforeEachCall->describing[] = $this->description; + $this->currentBeforeEachCall->describing = array_merge( + DescribeCall::describing(), + [$this->description] + ); } $this->currentBeforeEachCall->{$name}(...$arguments);