Merge pull request #1565 from louisbels/fix-dataset-method-chaining

fix: dataset inheritance with method chaining (beforeEach()->with(), describe()->with())
This commit is contained in:
nuno maduro
2026-04-10 17:05:25 +01:00
committed by GitHub
2 changed files with 19 additions and 15 deletions

View File

@ -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');

View File

@ -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);