fix: beforeEach/afterEach leaking into sibling describe blocks with same nested name (#1850)

* fix: beforeEach/afterEach leaking into sibling describe blocks with same nested name

* fix: resolve afterEach test ordering issue

* chore: update snapshots and parallel memory limit after rebase onto upstream/5.x

* chore: update snapshots and parallel memory limit after rebase onto upstream/5.x

---------

Co-authored-by: Adam Goulding <adam.goulding@ethixbase360.com>
Co-authored-by: Punyapal Shah <mrpunyapal@gmail.com>
This commit is contained in:
Adam Goulding
2026-11-20 13:44:17 +00:00
committed by GitHub
parent 41af35a3f2
commit f5c3687a68
7 changed files with 80 additions and 12 deletions
+1 -2
View File
@@ -6,7 +6,6 @@ namespace Pest\PendingCalls;
use Closure;
use Pest\PendingCalls\Concerns\Describable;
use Pest\Support\Arr;
use Pest\Support\Backtrace;
use Pest\Support\ChainableClosure;
use Pest\Support\HigherOrderMessageCollection;
@@ -43,7 +42,7 @@ final class AfterEachCall
$proxies = $this->proxies;
$afterEachTestCase = ChainableClosure::boundWhen(
fn (): bool => $describing === [] || in_array(Arr::last($describing), $this->__describing, true),
fn (): bool => $describing === [] || (count($describing) <= count($this->__describing) && array_slice($this->__describing, 0, count($describing)) === $describing),
ChainableClosure::bound(fn () => $proxies->chain($this), $this->closure)->bindTo($this, self::class),
)->bindTo($this, self::class);
+4 -7
View File
@@ -7,7 +7,6 @@ namespace Pest\PendingCalls;
use Closure;
use Pest\Exceptions\AfterBeforeTestFunction;
use Pest\PendingCalls\Concerns\Describable;
use Pest\Support\Arr;
use Pest\Support\Backtrace;
use Pest\Support\ChainableClosure;
use Pest\Support\HigherOrderMessageCollection;
@@ -50,11 +49,9 @@ final class BeforeEachCall
$beforeEachTestCall = function (TestCall $testCall) use ($describing): void {
if ($this->describing !== []) {
if (Arr::last($describing) !== Arr::last($this->describing)) {
return;
}
if (! in_array(Arr::last($describing), $testCall->describing, true)) {
$testCallDescribing = $testCall->describing;
if (count($describing) > count($testCallDescribing) ||
array_slice($testCallDescribing, 0, count($describing)) !== $describing) {
return;
}
}
@@ -63,7 +60,7 @@ final class BeforeEachCall
};
$beforeEachTestCase = ChainableClosure::boundWhen(
fn (): bool => $describing === [] || in_array(Arr::last($describing), $this->__describing, true),
fn (): bool => $describing === [] || (count($describing) <= count($this->__describing) && array_slice($this->__describing, 0, count($describing)) === $describing),
ChainableClosure::bound(fn () => $testCaseProxies->chain($this), $this->closure)->bindTo($this, self::class),
)->bindTo($this, self::class);
+7 -1
View File
@@ -33,6 +33,8 @@
✓ matching describe block names → outer → middle → it should not call afterEach functions for sibling describe blocks with the same name
✓ matching describe block names → outer → inner → it does not get executed before the test
✓ matching describe block names → outer → inner → it should not call afterEach functions for descendent of sibling describe blocks with the same name
✓ hierarchical test naming → block one → the same name → it does not call afterEach from sibling describe with same name
✓ hierarchical test naming → block two → the same name → it correctly calls afterEach from own describe hierarchy
PASS Tests\Features\Assignee
✓ it may be associated with an assignee [@nunomaduro, @taylorotwell]
@@ -56,6 +58,8 @@
✓ matching name → it should not call the before each on the describe block with the same name
✓ called on all tests → beforeEach should be called
✓ called on all tests → beforeEach should be called for all tests
✓ hierarchical test naming → block one → the same name → it does not call beforeEach from sibling describe with same name
✓ hierarchical test naming → block two → the same name → it correctly calls beforeEach from own describe hierarchy
PASS Tests\Features\BeforeEachProxiesToTestCallWithExpectations
✓ runs 1
@@ -335,6 +339,8 @@
✓ depends on describe using with → foo with (3)
✓ depends on describe using with → bar with (3)
✓ with test after describe → it should run the before each
✓ sibling describes may share the same it() description → block one → it can be created
✓ sibling describes may share the same it() description → block two → it can be created
PASS Tests\Features\DescriptionLess
✓ get 'foo'
@@ -2586,4 +2592,4 @@
✓ pass with dataset with ('my-datas-set-value')
✓ within describe → pass with dataset with ('my-datas-set-value')
Tests: 2 deprecated, 4 warnings, 5 incomplete, 2 notices, 40 todos, 38 skipped, 1880 passed (4316 assertions)
Tests: 2 deprecated, 4 warnings, 5 incomplete, 2 notices, 40 todos, 38 skipped, 1886 passed (4325 assertions)
+26
View File
@@ -100,3 +100,29 @@ describe('matching describe block names', function (): void {
});
});
});
describe('hierarchical test naming', function (): void {
describe('block one', function (): void {
describe('the same name', function (): void {
it('does not call afterEach from sibling describe with same name', function (): void {
expect($this)->not->toHaveProperty('example');
});
});
});
describe('block two', function (): void {
describe('the same name', function (): void {
beforeEach(function (): void {
$this->example = false;
});
afterEach(function (): void {
expect($this->example)->toBeFalse();
});
it('correctly calls afterEach from own describe hierarchy', function (): void {
expect($this->example)->toBeFalse();
});
});
});
});
+26
View File
@@ -126,3 +126,29 @@ describe('called on all tests', function (): void {
expect($this->baz)->toBe(2);
});
});
describe('hierarchical test naming', function (): void {
describe('block one', function (): void {
describe('the same name', function (): void {
it('does not call beforeEach from sibling describe with same name', function (): void {
expect($this)->not->toHaveProperty('example');
});
});
});
describe('block two', function (): void {
beforeEach(function (): void {
$this->example = false;
});
describe('the same name', function (): void {
beforeEach(function (): void {
$this->result = $this->example;
});
it('correctly calls beforeEach from own describe hierarchy', function (): void {
expect($this->result)->toBeFalse();
});
});
});
});
+14
View File
@@ -108,3 +108,17 @@ describe('with test after describe', function (): void {
expect($this->count)->toBe(2);
});
});
describe('sibling describes may share the same it() description', function (): void {
describe('block one', function (): void {
it('can be created', function (): void {
expect(true)->toBeTrue();
});
});
describe('block two', function (): void {
it('can be created', function (): void {
expect(true)->toBeTrue();
});
});
});
+2 -2
View File
@@ -4,7 +4,7 @@ use Symfony\Component\Process\Process;
$run = function (): ?string {
$process = new Process(
array_merge(['php', 'bin/pest', '--parallel', '--processes=3'], func_get_args()),
array_merge(['php', '-d', 'memory_limit=512M', 'bin/pest', '--parallel', '--processes=3', '--passthru-php=-d memory_limit=512M'], func_get_args()),
dirname(__DIR__, 2),
['COLLISION_PRINTER' => 'DefaultPrinter', 'COLLISION_IGNORE_DURATION' => 'true', 'PAO_DISABLE' => '1'],
);
@@ -21,7 +21,7 @@ test('parallel', function () use ($run): void {
$output = implode("\n", array_slice(explode("\n", (string) $output), -10));
$profileOutput = $run('tests/Fixtures/Suites/SuccessOnly.php', '--profile');
$expected = '2 deprecated, 4 warnings, 5 incomplete, 3 notices, 40 todos, 30 skipped, 1753 passed (3791 assertions)';
$expected = '2 deprecated, 4 warnings, 5 incomplete, 3 notices, 40 todos, 30 skipped, 1759 passed (3800 assertions)';
expect($output)
->toContain("Tests: {$expected}")