pest icon indicating copy to clipboard operation
pest copied to clipboard

[Bug]: StateGenerator TypeError with AfterLastTestMethodErrored in parallel mode

Open cargix1 opened this issue 10 months ago • 3 comments

Pest Version

v4.3.2

PHP Version

8.4

Operation System

macOS / Linux

Description

When running tests in parallel mode (--parallel), if any test triggers an AfterLastTestMethodErrored event (e.g., errors during teardown/cleanup), Pest crashes with a TypeError:

TypeError: NunoMaduro\Collision\Adapters\Phpunit\TestResult::fromBeforeFirstTestMethodErrored(): 
Argument #1 ($event) must be of type PHPUnit\Event\Test\BeforeFirstTestMethodErrored, 
PHPUnit\Event\Test\AfterLastTestMethodErrored given, 
called in vendor/pestphp/pest/src/Support/StateGenerator.php on line 34

Root Cause

In src/Support/StateGenerator.php, the code assumes all non-Errored events from testErroredEvents() are BeforeFirstTestMethodErrored:

foreach ($testResult->testErroredEvents() as $testResultEvent) {
    if ($testResultEvent instanceof Errored) {
        // ... handle Errored
    } else {
        // @phpstan-ignore-next-line
        $state->add(TestResult::fromBeforeFirstTestMethodErrored($testResultEvent));
    }
}

However, testErroredEvents() can also return AfterLastTestMethodErrored events, which are then incorrectly passed to fromBeforeFirstTestMethodErrored().

Suggested Fix

Add an explicit instanceof check for BeforeFirstTestMethodErrored:

use PHPUnit\Event\Test\BeforeFirstTestMethodErrored;

foreach ($testResult->testErroredEvents() as $testResultEvent) {
    if ($testResultEvent instanceof Errored) {
        $state->add(TestResult::fromPestParallelTestCase(
            $testResultEvent->test(),
            TestResult::FAIL,
            $testResultEvent->throwable()
        ));
    } elseif ($testResultEvent instanceof BeforeFirstTestMethodErrored) {
        $state->add(TestResult::fromBeforeFirstTestMethodErrored($testResultEvent));
    }
    // AfterLastTestMethodErrored events can be skipped or handled separately
}

Steps to Reproduce

  1. Create a test that causes an error during teardown (e.g., database constraint violation in afterEach)
  2. Run tests with --parallel flag
  3. Observe the TypeError crash

Workaround

We've created a composer patch that fixes this issue:

--- a/src/Support/StateGenerator.php
+++ b/src/Support/StateGenerator.php
@@ -10,6 +10,7 @@
 use PHPUnit\Event\Code\TestDoxBuilder;
 use PHPUnit\Event\Code\TestMethod;
 use PHPUnit\Event\Code\ThrowableBuilder;
+use PHPUnit\Event\Test\BeforeFirstTestMethodErrored;
 use PHPUnit\Event\Test\Errored;
 use PHPUnit\Event\TestData\TestDataCollection;
 use PHPUnit\Framework\SkippedWithMessageException;
@@ -29,10 +30,11 @@
                     TestResult::FAIL,
                     $testResultEvent->throwable()
                 ));
-            } else {
-                // @phpstan-ignore-next-line
+            } elseif ($testResultEvent instanceof BeforeFirstTestMethodErrored) {
                 $state->add(TestResult::fromBeforeFirstTestMethodErrored($testResultEvent));
             }
+            // AfterLastTestMethodErrored events are intentionally skipped
         }

cargix1 avatar Feb 01 '26 07:02 cargix1

I had started a fix for this a while back, but because this is using code directly in nunomaduro/collision I started by adding the TestResult helper in Collision first. Never got a review over there so couldn't implement on this end.

https://github.com/nunomaduro/collision/pull/336

JHWelch avatar Feb 05 '26 14:02 JHWelch

I have the same issue using parallel and PHP 8.2

mariosvasiliou avatar Mar 26 '26 16:03 mariosvasiliou

this no longer applies to pest 5, the state generator now handles AfterLastTestMethodErrored properly. if you still see this on pest 5, feel free to open a new issue.

nunomaduro avatar Oct 20 '26 14:10 nunomaduro