Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions src/Functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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);
}
Expand Down Expand Up @@ -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));
}
Expand All @@ -124,7 +124,7 @@ function uses(string ...$classAndTraits): UsesCall
*/
function pest(): Configuration
{
return new Configuration(Backtrace::file());
return new Configuration(Backtrace::testFile());
}
}

Expand Down Expand Up @@ -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);
}
Expand All @@ -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);
}
Expand All @@ -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));

Expand Down Expand Up @@ -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');
Expand Down
16 changes: 10 additions & 6 deletions src/PendingCalls/DescribeCall.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace Pest\PendingCalls;

use Closure;
use Pest\Support\Backtrace;
use Pest\Support\Description;
use Pest\TestSuite;

Expand Down Expand Up @@ -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;

Expand All @@ -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);
Expand Down