pest icon indicating copy to clipboard operation
pest copied to clipboard

[Bug]: Nothing is displayed if AfterAll throws an error

Open Nolikein opened this issue 11 months ago • 2 comments

Good morning,

OS (Docker)

Linux under Docker (base image : ubuntu:22.04) Xdebug enabled.

Versions

PHP : 8.3.17 Pest : 3.7.4 Collision : 8.6.1

What Happened

Short story

When a TypeError is thrown inside the afterAll hook, nothing is displayed but the process return as well as expected the error code 2. We cannot determine the error caused since no message is displayed.

Long story

My repository is private, but i can tell you the story. It may concern Pest or Collision, but i had a problem in the AfterAll() hook. I work in my tests with php resources, so i created a static service to open and get then manually close all the created resources by calling a flush() method inside the afterAll hook. Apparently, some of the resources was already closed when calling fclose() so i got a TypeError without knowing it because all the tests passed. In Pest, this means having an "ErroredEvents", this also blocked my "code coverage", and Pest return in the shell an exit code 2 that is blocking in my CI/CD.

In order to determine the error, I had to go in the vendor / Pest code to dump the error that is called "ErroredEvents" but that is not displayed.

As examples, here is how i dumped the errors :

See vendor/pestphp/pest/src/Kernel.php

// Method handle()
// Code begin from line 114
$result = Facade::result();

$result->hasTestErroredEvents();
$result->numberOfTestErroredEvents();
$result->testErroredEvents();

The same thing can be find in the vendor/nunomaduro/collision/src/Adapters/Phpunit/Style.php file where the writeRecap() method was the perfect place for me to display these errors without denaturing the rest of the code.

How to Reproduce

Inside the AfterAll() hook, open a resource with tmpfile(), then close it two times with fclose() in order to get my php error.

All the tests will pass, but the shell will return an exit code 2.

Sample Repository

No response

Pest Version

3.7.4

PHP Version

8.3.17

Operation System

Linux

Notes

How to solve the problem ?

I need your expertise. I cannot do what is wrote in the CONTRIBUTING.md file because i need to have your expertise. What is the best thing to do about these errors ? How to improve Pest, by displaying or is there another way to catch these errors ?

Here is what i have done, which is rudimentary but it works without denaturing the rest of the code : See vendor/nunomaduro/collision/src/Adapters/Phpunit/Style.php line 246

        if ($result->hasTestErroredEvents()) {
            $this->output->writeln([
                '',
                sprintf(
                    '  <fg=red>The %s test thrown %d error(s)</>',
                    $state->testCaseName,
                    $result->numberOfTestErroredEvents(),
                ),
            ]);

            foreach ($result->testErroredEvents() as $errored) {
                $this->writeError($errored->throwable());
            }
        }

Nolikein avatar Mar 04 '25 09:03 Nolikein

I have the same issue, an incorrect line in 'afterAll' hook generated the exit code 2 but no error was displayed, it took a long time to identify the cause.

this simple test will exit with code 2 but no message will appear:

afterAll(function () {
	throw new Exception('test');
});

test('test', function () {
	expect(1)->toBe(1);
});

taishar avatar Aug 14 '25 09:08 taishar

for me afterAll hook doesn't seem to be working at all. I'm using pestphp v4.1.

I did this:

pest()->extend(TestCase::class)
    ->use(RefreshDatabase::class)
    ->beforeEach(function (): void {
        Http::preventStrayRequests();
        $this->freezeTime();
    })
    ->afterAll(function (): void {
        dump('After all tests');
    })
    ->in('Feature', 'Unit');

But I don't see any output in the console.

raz-iacob avatar Sep 26 '25 22:09 raz-iacob