diff --git a/src/PendingCalls/AfterEachCall.php b/src/PendingCalls/AfterEachCall.php index 9f7f0b55..6c0ed4ea 100644 --- a/src/PendingCalls/AfterEachCall.php +++ b/src/PendingCalls/AfterEachCall.php @@ -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); diff --git a/src/PendingCalls/BeforeEachCall.php b/src/PendingCalls/BeforeEachCall.php index 852a1aad..34f08f3f 100644 --- a/src/PendingCalls/BeforeEachCall.php +++ b/src/PendingCalls/BeforeEachCall.php @@ -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); diff --git a/tests/.snapshots/success.txt b/tests/.snapshots/success.txt index 1e298acb..02b8ab63 100644 --- a/tests/.snapshots/success.txt +++ b/tests/.snapshots/success.txt @@ -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) \ No newline at end of file + Tests: 2 deprecated, 4 warnings, 5 incomplete, 2 notices, 40 todos, 38 skipped, 1886 passed (4325 assertions) \ No newline at end of file diff --git a/tests/Features/AfterEach.php b/tests/Features/AfterEach.php index 21b975eb..6449f244 100644 --- a/tests/Features/AfterEach.php +++ b/tests/Features/AfterEach.php @@ -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(); + }); + }); + }); +}); diff --git a/tests/Features/BeforeEach.php b/tests/Features/BeforeEach.php index ef78194e..e4832d97 100644 --- a/tests/Features/BeforeEach.php +++ b/tests/Features/BeforeEach.php @@ -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(); + }); + }); + }); +}); diff --git a/tests/Features/Describe.php b/tests/Features/Describe.php index f3fc130d..ed5de20d 100644 --- a/tests/Features/Describe.php +++ b/tests/Features/Describe.php @@ -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(); + }); + }); +}); diff --git a/tests/Visual/Parallel.php b/tests/Visual/Parallel.php index 4f7cacdc..fc707af1 100644 --- a/tests/Visual/Parallel.php +++ b/tests/Visual/Parallel.php @@ -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}")