Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions src/Concerns/Testable.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down Expand Up @@ -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) {
Expand All @@ -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));
Expand Down
13 changes: 13 additions & 0 deletions tests/.tests/GlobalAfterAll/GlobalAfterAllTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

pest()->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());
21 changes: 21 additions & 0 deletions tests/Visual/AfterAll.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

use Symfony\Component\Process\Process;

test('global afterAll hook runs after the suite', function () {
$marker = sys_get_temp_dir().DIRECTORY_SEPARATOR.'pest-afterall-'.uniqid();

$process = new Process(
['php', 'bin/pest', 'tests/.tests/GlobalAfterAll'],
dirname(__DIR__, 2),
['PEST_AFTERALL_MARKER' => $marker],
);

$process->run();

expect(file_exists($marker))->toBeTrue();

if (file_exists($marker)) {
unlink($marker);
}
})->skipOnWindows();