Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for nested describe blocks #1295

Merged
merged 6 commits into from
Oct 22, 2024
Merged

Conversation

jshayes
Copy link
Contributor

@jshayes jshayes commented Oct 12, 2024

What:

  • Bug Fix
  • New Feature

Description:

Calls parent beforeEach and afterEach functions in nested describe blocks. Also updates the test names to handle multiple nested describe blocks.

Given the following test, here is the output before and after this change

describe('outer', function () {
    beforeEach(function () {
        var_dump('outer');
    });

    describe('middle', function () {
        beforeEach(function () {
            var_dump('middle');
        });

        describe('inner', function () {
            beforeEach(function () {
                var_dump('inner');
            });

            test('test', function () {
                expect(1)->toBe(1);
            });
        });
    });
});

Before

string(5) "inner"

   PASS  Test
  ✓ inner → test → This test printed output: string(5) "inner"

After

string(5) "outer"
string(6) "middle"
string(5) "inner"

   PASS  Test
  ✓ outer → middle → inner → test

This change could cause breaking changes, so I'm not sure what you think of this. Since this project follows semver, I suppose this should go towards version 4.

Related:

#1023
#1138

This change was based off #1294, so it includes those changes as well, since they were required for this change.

@nunomaduro
Copy link
Member

@jshayes honestly, looks solid. do you mind to add a few more tests? specially around stuff like chaining methods after describe blocks, hooks, etc...

@jshayes
Copy link
Contributor Author

jshayes commented Oct 13, 2024

@nunomaduro I added some more test. Let me know if there are any cases you think I missed that you would like tested.

@zormandi-byborg
Copy link

I would LOVE this. Having no support for nested describe and beforeEach blocks was the main blocker why we hesitated to switch to Pest. This would finally remove the last roadblock. ❤️

@@ -54,7 +54,7 @@ public function __destruct()
$proxies = $this->proxies;

$afterEachTestCase = ChainableClosure::boundWhen(
fn (): bool => is_null($describing) || $this->__describing === $describing,
fn (): bool => $describing === [] || in_array(end($describing), $this->__describing, true),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

end() advances array's internal pointer to the last element, and returns its value.

Because this function actually mutates the given value, i rather not use it.

@@ -11,11 +11,15 @@ trait Describable
{
/**
* Note: this is property is not used; however, it gets added automatically by rector php.
*
* @var string[]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We use the format array<int, string> instead.

@@ -24,6 +24,10 @@
↓ it may have an associated PR #1
↓ it may have an associated note
// a note
↓ todo on describe → todo block → nested inside todo block → it should not execute
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add some tests too in the middle of the test file?

@@ -24,6 +24,10 @@
↓ it may have an associated PR #1
↓ it may have an associated note
// a note
↓ todo on describe → todo block → nested inside todo block → it should not execute
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add some tests too in the middle of the test file?

@@ -178,6 +183,10 @@
✓ it may be used with high order with dataset "informal"
✓ it may be used with high order even when bound with dataset "formal"
✓ it may be used with high order even when bound with dataset "informal"
✓ with on nested describe → nested → before inner describe block with (1)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add some tests too in the middle of the test file?

@@ -188,6 +197,12 @@
✓ depends run test only once
✓ it asserts true is true
✓ depends works with the correct test name
✓ describe block → first in describe
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add some tests too in the middle of the test file?

// This is a runtime note within describe
✓ nested → describe nested within describe → it may have a static note and runtime note
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add some tests too in the middle of the test file?

Copy link
Contributor Author

@jshayes jshayes Oct 13, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a test after this one: multiple notes, let me know if you want other tests here as well

@@ -1255,6 +1301,12 @@
- it skips when skip after assertion
- it can use something in the test case as a condition → This test was skipped
- it can user higher order callables and skip
- skip on describe → skipped tests → nested inside skipped block → it should not execute
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add some tests too in the middle of the test file?

@@ -1285,6 +1337,12 @@
↓ it may have an associated PR #1
↓ it may have an associated note
// a note
↓ todo on describe → todo block → nested inside todo block → it should not execute
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add some tests too in the middle of the test file?

@jshayes
Copy link
Contributor Author

jshayes commented Oct 13, 2024

I added some more tests to the end of the test files, after the describe blocks. Is this what you had in mind?

@jshayes
Copy link
Contributor Author

jshayes commented Oct 13, 2024

Also, I noticed another issue with describe blocks that I have a separate PR for, but I think the issue is a bigger deal due do to nested describe blocks in this PR.

For instance, the following test

describe('same-name', function () {
    beforeEach(function () {
        var_dump('before each');
    });
});

describe('same-name', function () {
    test('test', function () {
        expect(true)->toBeTrue();
    });
});

Will produce the following output

string(11) "before each"

   WARN  Tests\Features\BeforeEach
  ! same-name → test → This test printed output: string(11) "before each"                                               0.01s  

Since it is just checking the name of the describe block, it will match the before each from one block into another block. I do have a PR where I address this issue, however I figured I'd bring it up here because this could have some implications on nested describe blocks until the issue is resolved.

For instance, the following test

describe('outer block 1', function () {
    describe('same-name', function () {
        beforeEach(function () {
            var_dump('before each');
        });
    });
});

describe('outer block 2', function () {
    describe('same-name', function () {
        test('test', function () {
            expect(true)->toBeTrue();
        });
    });
});

will produce the following output

string(11) "before each"

   WARN  Tests\Features\BeforeEach
  ! same-name → test → This test printed output: string(11) "before each"                                               0.01s  

This is the current behaviour, before my changes here, but I think this is a fairly common use case to have describe blocks with the same name when they are nested in different describe blocks.

Anyway, let me know if you are okay with this being a separate PR, or if you want me to add those changes here.

@jshayes jshayes requested a review from nunomaduro October 14, 2024 20:41
@nunomaduro nunomaduro merged commit cf57ea1 into pestphp:3.x Oct 22, 2024
@hafezdivandari
Copy link

Does this also fix #1010? And #984?

@jshayes
Copy link
Contributor Author

jshayes commented Oct 22, 2024

Does this also fix #1010? And #984?

No, this PR doesn't address those issues. For #1010, the calling with works on a describe block, however, the value is not passed in as an argument to the describe callback. It is only passed in to the test callback

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants