[Bug]: beforeAll and afterAll global hooks are not working
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 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
In the meantime I've moved all my beforeAll/afterAll from global to local test class hooks, which seems to work.
Looks like to be still a problem in 3.8.2, pretty strange it has not been fixed yet.
I just encounter this problem too. Hopefully will be fixed in v4?
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();
}
}
yeah; this is fixed on 4.x, but afraid of fixing it on pest 3.