pest icon indicating copy to clipboard operation
pest copied to clipboard

[Bug]: beforeAll and afterAll global hooks are not working

Open HassanDomeDenea opened this issue 11 months ago • 3 comments

What Happened

When I add global hooks beforeAll or afterAll inside tests/Pest.php, they do not work, while beforeEach and afterEach are being called properly.

How to Reproduce

composer global require laravel/installer

laravel new example-app

choosing pest for testing.

in tests/Pest.php, I added:

// tests/Pest.php

pest()
    ->beforeAll(function () {
        echo "Before All\n";
    })
    ->beforeEach(function () {
        echo "Before Each\n";
    })
    ->in('Unit');

running: php artisan test

I see BeforeEach in output, but I do not see BeforeAll.

Sample Repository

No response

Pest Version

3.7.4

PHP Version

8.4.2

Operation System

Windows

Notes

No response

HassanDomeDenea avatar Jan 24 '25 12:01 HassanDomeDenea

@HassanDomeDenea I have pr with fix for it waiting for 2 months or so

https://github.com/pestphp/pest/pull/1322

these hooks don't work since 3.0.4

downgrade to 3.0.3 for time being

laylatichy avatar Jan 30 '25 16:01 laylatichy

In the meantime I've moved all my beforeAll/afterAll from global to local test class hooks, which seems to work.

arukompas avatar Feb 21 '25 08:02 arukompas

Looks like to be still a problem in 3.8.2, pretty strange it has not been fixed yet.

widoz avatar Jun 14 '25 18:06 widoz

I just encounter this problem too. Hopefully will be fixed in v4?

raz-iacob avatar Jul 23 '25 04:07 raz-iacob

It seems it will been fixed in version 4 See https://github.com/pestphp/pest/pull/1322#issuecomment-3121058378

In the meantime or if you cannot upgrade to 4 later on, you can extend Pest for your testsuite and make use of the setUpBeforeClass method.

In your Pest.php

pest()->extends(MyTestCase::class)
    ->in('functional'); // or other suite

and the MyTestCase class

use PHPUnit\Framework\TestCase;

class MyTestCase extends TestCase
{
    public static function setUpBeforeClass(): void
    {
        parent::setUpBeforeClass();
    }

    public static function tearDownAfterClass(): void
    {
       // Not strictly necessary but maybe you want to have consistency
       parent::tearDownAfterClass();
    }
}

widoz avatar Jul 29 '25 20:07 widoz

yeah; this is fixed on 4.x, but afraid of fixing it on pest 3.

nunomaduro avatar Jul 30 '25 21:07 nunomaduro