diff --git a/src/Functions.php b/src/Functions.php index 0ed631f3..5bc916a2 100644 --- a/src/Functions.php +++ b/src/Functions.php @@ -48,7 +48,7 @@ function expect(mixed $value = null): Expectation function beforeAll(Closure $closure): void { if (DescribeCall::describing() !== []) { - $filename = Backtrace::file(); + $filename = Backtrace::testFile(); throw new BeforeAllWithinDescribe($filename); } @@ -67,7 +67,7 @@ function beforeAll(Closure $closure): void */ function beforeEach(?Closure $closure = null): BeforeEachCall { - $filename = Backtrace::file(); + $filename = Backtrace::testFile(); return new BeforeEachCall(TestSuite::getInstance(), $filename, $closure); } @@ -112,7 +112,7 @@ function describe(string $description, Closure $tests): DescribeCall */ function uses(string ...$classAndTraits): UsesCall { - $filename = Backtrace::file(); + $filename = Backtrace::testFile(); return new UsesCall($filename, array_values($classAndTraits)); } @@ -124,7 +124,7 @@ function uses(string ...$classAndTraits): UsesCall */ function pest(): Configuration { - return new Configuration(Backtrace::file()); + return new Configuration(Backtrace::testFile()); } } @@ -197,7 +197,7 @@ function todo(string $description): TestCall */ function afterEach(?Closure $closure = null): AfterEachCall { - $filename = Backtrace::file(); + $filename = Backtrace::testFile(); return new AfterEachCall(TestSuite::getInstance(), $filename, $closure); } @@ -210,7 +210,7 @@ function afterEach(?Closure $closure = null): AfterEachCall function afterAll(Closure $closure): void { if (DescribeCall::describing() !== []) { - $filename = Backtrace::file(); + $filename = Backtrace::testFile(); throw new AfterAllWithinDescribe($filename); } @@ -227,7 +227,7 @@ function afterAll(Closure $closure): void */ function covers(array|string ...$classesOrFunctions): void { - $filename = Backtrace::file(); + $filename = Backtrace::testFile(); $beforeEachCall = (new BeforeEachCall(TestSuite::getInstance(), $filename)); @@ -256,7 +256,7 @@ function covers(array|string ...$classesOrFunctions): void */ 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 08ebc15e..d96b4a3c 100644 --- a/src/PendingCalls/DescribeCall.php +++ b/src/PendingCalls/DescribeCall.php @@ -5,7 +5,6 @@ namespace Pest\PendingCalls; use Closure; -use Pest\Support\Backtrace; use Pest\Support\Description; use Pest\TestSuite; @@ -53,7 +52,11 @@ public static function describing(): array */ 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 @@ public function __destruct() */ public function __call(string $name, array $arguments): self { - $filename = Backtrace::file(); - if (! $this->currentBeforeEachCall instanceof \Pest\PendingCalls\BeforeEachCall) { - $this->currentBeforeEachCall = new BeforeEachCall(TestSuite::getInstance(), $filename); + $this->currentBeforeEachCall = new BeforeEachCall(TestSuite::getInstance(), $this->filename); - $this->currentBeforeEachCall->describing[] = $this->description; + $this->currentBeforeEachCall->describing = array_merge( + DescribeCall::describing(), + [$this->description] + ); } $this->currentBeforeEachCall->{$name}(...$arguments);