[Bug]: afterEach Hook Execution Order Is Inverted Compared to Documentation · Issue #1400 · pestphp/pest · GitHub
More Web Proxy on the site http://driver.im/
You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When using afterEach hooks at different levels (global in Pest.php and in test files), the execution order is completely reversed compared to what's stated in the documentation.
Any before* hooks defined in the Pest.php configuration file will be executed prior to hooks defined in individual test files. Similarly, any after* hooks specified in the Pest.php configuration file will be executed after any hooks defined in individual test files.
However, I've observed that the afterEach hooks are executed in the reverse order from what's documented:
The global hook runs first
Then the outermost test file hook
Then any intermediate nested hooks
Finally the innermost hook
How to Reproduce
I've created a minimal test case to demonstrate this issue:
Set up a fresh Laravel project with Pest:
composer global require laravel/installer
laravel new example-app
(Selected Pest for testing during installation)
Created a nested test structure in tests/Unit/ExampleTest.php:
pest()->beforeEach(function ()
{
dump('0 global beforeEach()');
});
pest()->afterEach(function ()
{
dump('7 global afterEach()');
});
Test Output
$ php artisan test
^ "0 global beforeEach()"
^ "1"
^ "Outer Test"
^ "7 global afterEach()"
^ "6"
^ "0 global beforeEach()"
^ "1"
^ "2"
^ "Middle Test"
^ "7 global afterEach()"
^ "6"
^ "5"
^ "0 global beforeEach()"
^ "1"
^ "2"
^ "3"
^ "Inner test"
^ "7 global afterEach()"<-- Should be last, runs first
^ "6"<-- Should be second-to-last, runs second
^ "5"<-- Should be third-to-last, runs third
^ "4"<-- Should be first, runs last
PASS Tests\Unit\ExampleTest
✓ Outer Test
✓ Group 1 → Middle Test
✓ Group 1 → Group 2 → Inner test
Tests: 3 passed (3 assertions)
Duration: 0.03s
According to the documentation, for a test in the innermost describe block:
The beforeEach hooks should execute in order: global → outer → middle → inner (this works correctly)
The test runs
The afterEach hooks should execute in the reverse order: inner → middle → outer → global
However, the actual output shows the afterEach hooks running in the exact opposite order: global → outer → middle → inner.
Additional observations
The afterEach hooks are completely reversed from what would be expected. This behavior is counterintuitive and makes it difficult to properly clean up resources in a consistent manner. Since beforeEach hooks set up state in a specific order, it would be logical for afterEach hooks to clean up in the reverse order.
The text was updated successfully, but these errors were encountered:
When using
afterEach
hooks at different levels (global in Pest.php and in test files), the execution order is completely reversed compared to what's stated in the documentation.According to the Global Hooks documentation, it states:
However, I've observed that the
afterEach
hooks are executed in the reverse order from what's documented:How to Reproduce
I've created a minimal test case to demonstrate this issue:
(Selected Pest for testing during installation)
tests/Unit/ExampleTest.php
:tests/Pest.php
:Test Output
Sample Repository
pest-bug-example-repo
Pest Version
3.8.2
PHP Version
8.4.6
Operation System
Linux
Expected Behavior
According to the documentation, for a test in the innermost describe block:
beforeEach
hooks should execute in order: global → outer → middle → inner (this works correctly)afterEach
hooks should execute in the reverse order: inner → middle → outer → globalHowever, the actual output shows the afterEach hooks running in the exact opposite order: global → outer → middle → inner.
Additional observations
The
afterEach
hooks are completely reversed from what would be expected. This behavior is counterintuitive and makes it difficult to properly clean up resources in a consistent manner. SincebeforeEach
hooks set up state in a specific order, it would be logical forafterEach
hooks to clean up in the reverse order.The text was updated successfully, but these errors were encountered: