Files
pest/tests/Features/AfterEach.php
T
Adam Goulding f5c3687a68 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>
2026-11-20 19:14:17 +05:30

129 lines
3.7 KiB
PHP

<?php
$state = new stdClass;
beforeEach(function () use ($state): void {
$this->state = $state;
});
afterEach(function (): void {
$this->state->bar = 1;
});
afterEach(function (): void {
unset($this->state->bar);
});
it('does not get executed before the test', function (): void {
expect($this->state)->not->toHaveProperty('bar');
});
it('gets executed after the test', function (): void {
expect($this->state)->toHaveProperty('bar')
->and($this->state->bar)->toBe(2);
});
afterEach(function (): void {
$this->state->bar = 2;
});
describe('outer', function (): void {
afterEach(function (): void {
$this->state->bar++;
});
describe('inner', function (): void {
afterEach(function (): void {
$this->state->bar++;
});
it('does not get executed before the test', function (): void {
expect($this->state)->toHaveProperty('bar')
->and($this->state->bar)->toBe(2);
});
it('should call all parent afterEach functions', function (): void {
expect($this->state)->toHaveProperty('bar')
->and($this->state->bar)->toBe(4);
});
});
});
describe('matching describe block names', function (): void {
afterEach(function (): void {
$this->state->foo = 1;
});
describe('outer', function (): void {
afterEach(function (): void {
$this->state->foo++;
});
describe('middle', function (): void {
afterEach(function (): void {
$this->state->foo++;
});
describe('inner', function (): void {
afterEach(function (): void {
$this->state->foo++;
});
it('does not get executed before the test', function (): void {
expect($this)->not->toHaveProperty('foo');
});
it('should call all parent afterEach functions', function (): void {
expect($this->state->foo)->toBe(4);
});
});
});
describe('middle', function (): void {
it('does not get executed before the test', function (): void {
expect($this)->not->toHaveProperty('foo');
});
it('should not call afterEach functions for sibling describe blocks with the same name', function (): void {
expect($this)->not->toHaveProperty('foo');
});
});
describe('inner', function (): void {
it('does not get executed before the test', function (): void {
expect($this)->not->toHaveProperty('foo');
});
it('should not call afterEach functions for descendent of sibling describe blocks with the same name', function (): void {
expect($this)->not->toHaveProperty('foo');
});
});
});
});
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();
});
});
});
});