Skip to content

Commit

Permalink
Fix an issue where beforeEach/afterEach functions were called on othe…
Browse files Browse the repository at this point in the history
…r describe blocks with the same name
  • Loading branch information
jshayes committed Oct 22, 2024
1 parent cf57ea1 commit ae15b7c
Show file tree
Hide file tree
Showing 18 changed files with 395 additions and 19 deletions.
2 changes: 1 addition & 1 deletion src/Factories/TestCaseMethodFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ final class TestCaseMethodFactory
/**
* The test's describing, if any.
*
* @var array<int, string>
* @var array<int, \Pest\Support\Description>
*/
public array $describing = [];

Expand Down
4 changes: 2 additions & 2 deletions src/PendingCalls/Concerns/Describable.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ trait Describable
/**
* Note: this is property is not used; however, it gets added automatically by rector php.
*
* @var array<int, string>
* @var array<int, \Pest\Support\Description>
*/
public array $__describing;

/**
* The describing of the test case.
*
* @var array<int, string>
* @var array<int, \Pest\Support\Description>
*/
public array $describing = [];
}
14 changes: 10 additions & 4 deletions src/PendingCalls/DescribeCall.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

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

/**
Expand All @@ -16,7 +17,7 @@ final class DescribeCall
/**
* The current describe call.
*
* @var array<int, string>
* @var array<int, Description>
*/
private static array $describing = [];

Expand All @@ -25,22 +26,27 @@ final class DescribeCall
*/
private ?BeforeEachCall $currentBeforeEachCall = null;

/**
* The unique description for this describe block
*/
private readonly Description $description;

/**
* Creates a new Pending Call.
*/
public function __construct(
public readonly TestSuite $testSuite,
public readonly string $filename,
public readonly string $description,
string $description,
public readonly Closure $tests
) {
//
$this->description = new Description($description);
}

/**
* What is the current describing.
*
* @return array<int, string>
* @return array<int, Description>
*/
public static function describing(): array
{
Expand Down
15 changes: 15 additions & 0 deletions src/Support/Description.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace Pest\Support;

final readonly class Description implements \Stringable
{
public function __construct(private string $description) {}

public function __toString(): string
{
return $this->description;
}
}
2 changes: 1 addition & 1 deletion src/Support/Str.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public static function isUuid(string $value): bool
/**
* Creates a describe block as `$describeDescription` → `$testDescription` format.
*
* @param array<int, string> $describeDescriptions
* @param array<int, Description> $describeDescriptions
*/
public static function describe(array $describeDescriptions, string $testDescription): string
{
Expand Down
5 changes: 3 additions & 2 deletions tests/.pest/snapshots/Visual/Todo/todo.snap
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
todo on describeshould not fail
todo on describeshould run

TODO Tests\Features\Todo - 28 todos
TODO Tests\Features\Todo - 29 todos
something todo later
something todo later chained
something todo later chained and with function body
Expand Down Expand Up @@ -45,6 +45,7 @@
// nested describe note
// test note
↓ todo on describe → todo block → it should not execute
↓ todo on describe with matching name → describe block → it should not execute
↓ todo on test after describe block
↓ todo with note on test after describe block
// test note
Expand Down Expand Up @@ -80,6 +81,6 @@
PASS Tests\CustomTestCase\ParentTest
✓ override method

Tests: 38 todos, 3 passed (20 assertions)
Tests: 39 todos, 3 passed (21 assertions)
Duration: x.xxs

5 changes: 3 additions & 2 deletions tests/.pest/snapshots/Visual/Todo/todo_in_parallel.snap
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
todo on describeshould not fail
todo on describeshould run

TODO Tests\Features\Todo - 28 todos
TODO Tests\Features\Todo - 29 todos
something todo later
something todo later chained
something todo later chained and with function body
Expand Down Expand Up @@ -45,6 +45,7 @@
// nested describe note
// test note
↓ todo on describe → todo block → it should not execute
↓ todo on describe with matching name → describe block → it should not execute
↓ todo on test after describe block
↓ todo with note on test after describe block
// test note
Expand Down Expand Up @@ -80,6 +81,6 @@
PASS Tests\CustomTestCase\ParentTest
✓ override method

Tests: 38 todos, 3 passed (20 assertions)
Tests: 39 todos, 3 passed (21 assertions)
Duration: x.xxs

5 changes: 3 additions & 2 deletions tests/.pest/snapshots/Visual/Todo/todos.snap
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
todo on describeshould not fail
todo on describeshould run

TODO Tests\Features\Todo - 28 todos
TODO Tests\Features\Todo - 29 todos
something todo later
something todo later chained
something todo later chained and with function body
Expand Down Expand Up @@ -45,6 +45,7 @@
// nested describe note
// test note
↓ todo on describe → todo block → it should not execute
↓ todo on describe with matching name → describe block → it should not execute
↓ todo on test after describe block
↓ todo with note on test after describe block
// test note
Expand Down Expand Up @@ -80,6 +81,6 @@
PASS Tests\CustomTestCase\ParentTest
✓ override method

Tests: 38 todos, 3 passed (20 assertions)
Tests: 39 todos, 3 passed (21 assertions)
Duration: x.xxs

5 changes: 3 additions & 2 deletions tests/.pest/snapshots/Visual/Todo/todos_in_parallel.snap
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
todo on describeshould not fail
todo on describeshould run

TODO Tests\Features\Todo - 28 todos
TODO Tests\Features\Todo - 29 todos
something todo later
something todo later chained
something todo later chained and with function body
Expand Down Expand Up @@ -45,6 +45,7 @@
// nested describe note
// test note
↓ todo on describe → todo block → it should not execute
↓ todo on describe with matching name → describe block → it should not execute
↓ todo on test after describe block
↓ todo with note on test after describe block
// test note
Expand Down Expand Up @@ -80,6 +81,6 @@
PASS Tests\CustomTestCase\ParentTest
✓ override method

Tests: 38 todos, 3 passed (20 assertions)
Tests: 39 todos, 3 passed (21 assertions)
Duration: x.xxs

54 changes: 52 additions & 2 deletions tests/.snapshots/success.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@
✓ it gets executed after the test
✓ outer → inner → it does not get executed before the test
✓ outer → inner → it should call all parent afterEach functions
✓ matching describe block names → outer → middle → inner → it does not get executed before the test
✓ matching describe block names → outer → middle → inner → it should call all parent afterEach functions
✓ matching describe block names → outer → middle → it does not get executed before the test
✓ 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

PASS Tests\Features\Assignee
✓ it may be associated with an assignee [@nunomaduro, @taylorotwell]
Expand All @@ -45,6 +51,13 @@
✓ outer → inner → it should call all parent beforeEach functions
✓ with expectations → nested block → test
✓ with expectations → test
✓ matching describe block names → outer → middle → inner → it should call all parent beforeEach functions
✓ matching describe block names → outer → middle → it should not call beforeEach functions for sibling describe blocks with the same name
✓ matching describe block names → outer → inner → it should not call beforeEach functions for descendent of sibling describe blocks with the same name
✓ matching name → it should call the before each
✓ 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

PASS Tests\Features\BeforeEachProxiesToTestCallWithExpectations
✓ runs 1
Expand Down Expand Up @@ -187,6 +200,11 @@
✓ with on nested describe → nested → describe → it should include the with value from all parent describe blocks with (1) / (2)
✓ with on nested describe → nested → describe → should include the with value from all parent describe blocks and the test with (1) / (2) / (3)
✓ with on nested describe → nested → after inner describe block with (1)
✓ matching describe block names → outer → before inner describe block with (1)
✓ matching describe block names → outer → inner → it should include the with value from all parent describe blocks with (1) / (2)
✓ matching describe block names → outer → inner → should include the with value from all parent describe blocks and the test with (1) / (2) / (3)
✓ matching describe block names → outer → inner → it should not include the value from the other describe block with the same name with (1)
✓ matching describe block names → outer → after inner describe block with (1)
✓ after describe block with (5)
✓ it may be used with high order after describe block with dataset "formal"
✓ it may be used with high order after describe block with dataset "informal"
Expand Down Expand Up @@ -1104,6 +1122,25 @@
// This is before each describe runtime note
// This is before each nested describe runtime note
// This is a runtime note within a nested describe
✓ matching describe names → describe block → it may have a static note and runtime note
// This is before each static note
// This is before each matching describe static note
// This is a nested matching static note
// This is a static note within a matching describe
// This is before each runtime note
// This is before each matching describe runtime note
// This is before each matching describe runtime note
// This is a runtime note within a matching describe
✓ matching describe names → describe block → it may have a static note and runtime note, that are different than the matching describe block
// This is before each static note
// This is before each matching describe static note
// This is a nested matching static note, and should not contain the matching describe notes
// This is before each matching describe static note, and should not contain the matching describe notes
// This is a static note within a matching describe, and should not contain the matching describe notes
// This is before each runtime note
// This is before each matching describe runtime note
// This is before each matching describe runtime note, and should not contain the matching describe notes
// This is a runtime note within a matching describe, and should not contain the matching describe notes
✓ multiple notes
// This is before each static note
// This is before each runtime note
Expand Down Expand Up @@ -1250,6 +1287,10 @@
✓ describe blocks → describe with repeat → nested describe with repeat → test with no repeat should repeat the number of times specified in the parent describe block @ repetition 2 of 2
✓ describe blocks → describe with repeat → nested describe with repeat → test with repeat should repeat the number of times specified in the test @ repetition 1 of 2
✓ describe blocks → describe with repeat → nested describe with repeat → test with repeat should repeat the number of times specified in the test @ repetition 2 of 2
✓ matching describe blocks → describe block → it should repeat the number of times specified in the parent describe block @ repetition 1 of 3
✓ matching describe blocks → describe block → it should repeat the number of times specified in the parent describe block @ repetition 2 of 3
✓ matching describe blocks → describe block → it should repeat the number of times specified in the parent describe block @ repetition 3 of 3
✓ matching describe blocks → describe block → should not repeat the number of times of the describe block with the same name

PASS Tests\Features\ScopedDatasets\Directory\NestedDirectory1\TestFileInNestedDirectoryWithDatasetsFile
✓ uses dataset with (1)
Expand Down Expand Up @@ -1312,6 +1353,12 @@
- skip on beforeEach → skipped tests → nested inside skipped block → it should not execute
- skip on beforeEach → skipped tests → it should not execute
✓ skip on beforeEach → it should execute
- matching describe with skip → describe block → it should not execute
✓ matching describe with skip → describe block → it should execute a test in a describe block with the same name as a skipped describe block
✓ matching describe with skip → it should execute
- matching describe with skip on beforeEach → describe block → it should not execute
✓ matching describe with skip on beforeEach → describe block → it should execute a test in a describe block with the same name as a skipped describe block
✓ matching describe with skip on beforeEach → it should execute
✓ it does not skip after the describe block
- it can skip after the describe block

Expand All @@ -1334,7 +1381,7 @@
✓ nested → it may be associated with an ticket #1, #4, #5, #6, #3
// an note between an the ticket

PASS Tests\Features\Todo - 28 todos
PASS Tests\Features\Todo - 29 todos
↓ something todo later
↓ something todo later chained
↓ something todo later chained and with function body
Expand Down Expand Up @@ -1366,6 +1413,9 @@
// test note
↓ todo on describe → todo block → it should not execute
✓ todo on describe → it should execute
↓ todo on describe with matching name → describe block → it should not execute
✓ todo on describe with matching name → describe block → it should execute a test in a describe block with the same name as a todo describe block
✓ todo on describe with matching name → it should execute
↓ todo on test after describe block
↓ todo with note on test after describe block
// test note
Expand Down Expand Up @@ -1698,4 +1748,4 @@
WARN Tests\Visual\Version
- visual snapshot of help command output

Tests: 2 deprecated, 4 warnings, 5 incomplete, 2 notices, 38 todos, 33 skipped, 1144 passed (2736 assertions)
Tests: 2 deprecated, 4 warnings, 5 incomplete, 2 notices, 39 todos, 35 skipped, 1174 passed (2777 assertions)
52 changes: 52 additions & 0 deletions tests/Features/AfterEach.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,55 @@
});
});
});

describe('matching describe block names', function () {
afterEach(function () {
$this->state->foo = 1;
});

describe('outer', function () {
afterEach(function () {
$this->state->foo++;
});

describe('middle', function () {
afterEach(function () {
$this->state->foo++;
});

describe('inner', function () {
afterEach(function () {
$this->state->foo++;
});

it('does not get executed before the test', function () {
expect($this)->not->toHaveProperty('foo');
});

it('should call all parent afterEach functions', function () {
expect($this->state->foo)->toBe(4);
});
});
});

describe('middle', function () {
it('does not get executed before the test', function () {
expect($this)->not->toHaveProperty('foo');
});

it('should not call afterEach functions for sibling describe blocks with the same name', function () {
expect($this)->not->toHaveProperty('foo');
});
});

describe('inner', function () {
it('does not get executed before the test', function () {
expect($this)->not->toHaveProperty('foo');
});

it('should not call afterEach functions for descendent of sibling describe blocks with the same name', function () {
expect($this)->not->toHaveProperty('foo');
});
});
});
});
Loading

0 comments on commit ae15b7c

Please sign in to comment.