From 4e0c265ecc3d4ab36a8885a17b9ec10610da0082 Mon Sep 17 00:00:00 2001 From: Lazizbek Ergashev Date: Sat, 27 Jun 2026 02:29:05 +0500 Subject: [PATCH] fix: preserve the global afterAll hook until class teardown --- src/Concerns/Testable.php | 11 ++++++++-- .../GlobalAfterAll/GlobalAfterAllTest.php | 13 ++++++++++++ tests/Visual/AfterAll.php | 21 +++++++++++++++++++ 3 files changed, 43 insertions(+), 2 deletions(-) create mode 100644 tests/.tests/GlobalAfterAll/GlobalAfterAllTest.php create mode 100644 tests/Visual/AfterAll.php diff --git a/src/Concerns/Testable.php b/src/Concerns/Testable.php index b978139c2..c72330b24 100644 --- a/src/Concerns/Testable.php +++ b/src/Concerns/Testable.php @@ -117,6 +117,11 @@ trait Testable */ private static ?Closure $__afterAll = null; + /** + * The test's after all closure, preserved for the class teardown. + */ + private static ?Closure $__afterAllForClass = null; + /** * The list of snapshot changes, if any. */ @@ -214,6 +219,8 @@ public static function setUpBeforeClass(): void $beforeAll = ChainableClosure::boundStatically(self::$__beforeAll, $beforeAll); } + self::$__afterAllForClass = self::$__afterAll; + try { call_user_func(Closure::bind($beforeAll, null, self::class)); } catch (Throwable $e) { @@ -228,8 +235,8 @@ public static function tearDownAfterClass(): void { $afterAll = TestSuite::getInstance()->afterAll->get(self::$__filename); - if (self::$__afterAll instanceof Closure) { - $afterAll = ChainableClosure::boundStatically(self::$__afterAll, $afterAll); + if (self::$__afterAllForClass instanceof Closure) { + $afterAll = ChainableClosure::boundStatically(self::$__afterAllForClass, $afterAll); } call_user_func(Closure::bind($afterAll, null, self::class)); diff --git a/tests/.tests/GlobalAfterAll/GlobalAfterAllTest.php b/tests/.tests/GlobalAfterAll/GlobalAfterAllTest.php new file mode 100644 index 000000000..3ab92f118 --- /dev/null +++ b/tests/.tests/GlobalAfterAll/GlobalAfterAllTest.php @@ -0,0 +1,13 @@ +afterAll(function () { + $marker = getenv('PEST_AFTERALL_MARKER'); + + if (is_string($marker) && $marker !== '') { + file_put_contents($marker, '1'); + } +}); + +test('first', fn () => expect(true)->toBeTrue()); + +test('second', fn () => expect(true)->toBeTrue()); diff --git a/tests/Visual/AfterAll.php b/tests/Visual/AfterAll.php new file mode 100644 index 000000000..b4c687cb6 --- /dev/null +++ b/tests/Visual/AfterAll.php @@ -0,0 +1,21 @@ + $marker], + ); + + $process->run(); + + expect(file_exists($marker))->toBeTrue(); + + if (file_exists($marker)) { + unlink($marker); + } +})->skipOnWindows();