Skip to content

fix: handle AfterLastTestMethodErrored in StateGenerator for parallel mode#1627

Open
QDenka wants to merge 167 commits intopestphp:3.xfrom
QDenka:fix/state-generator-after-last-test-method-errored
Open

fix: handle AfterLastTestMethodErrored in StateGenerator for parallel mode#1627
QDenka wants to merge 167 commits intopestphp:3.xfrom
QDenka:fix/state-generator-after-last-test-method-errored

Conversation

@QDenka
Copy link

@QDenka QDenka commented Feb 8, 2026

Problem

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: TestResult::fromBeforeFirstTestMethodErrored(): Argument #1 ($event) must be of type
BeforeFirstTestMethodErrored, AfterLastTestMethodErrored given

This happens because StateGenerator assumes all non-Errored events from testErroredEvents() are BeforeFirstTestMethodErrored, but AfterLastTestMethodErrored events can also be present.

Solution

Added explicit instanceof checks for both BeforeFirstTestMethodErrored and AfterLastTestMethodErrored. Since both event types share the same interface (testClassName(), calledMethod(), throwable(), telemetryInfo()), AfterLastTestMethodErrored is converted to a BeforeFirstTestMethodErrored for display purposes via Collision's TestResult::fromBeforeFirstTestMethodErrored().

Changes

  • src/Support/StateGenerator.php: Added instanceof checks for BeforeFirstTestMethodErrored and AfterLastTestMethodErrored instead of a catch-all else branch
  • tests/Unit/Support/StateGenerator.php: Added tests verifying both event types are handled correctly without TypeError

Fixes #1623

mertasan and others added 30 commits September 12, 2024 02:45
Co-authored-by: Owen Voke <development@voke.dev>
Co-authored-by: Owen Voke <development@voke.dev>
The functions toMatchArray and toMatchObject indicate that the wrong field is mismatching from the second loop on because the 'message' is overwritten and taken into the following loop. This patch creates a $second_message for the second test (value test) to keep the error message correct.
Remove remaining } in toMatchObject.
Reset approach was not the right one.
pindab0ter and others added 30 commits October 29, 2025 11:20
…ning

Fix Undefined array key "testdox-columns" warning
…-help

feat: add --dirty documentation in --help
feat: show more useful exception when `intl` extension not found
…y-file-level

feat: add pest only function to mark each test in a file as only
Removed and updated sponsor links in the README.
…ultibyte string compatibility and correct TeamCity test names for datasets in "describe" blocks
…-tests-with-dataset

fix: replace `substr` with `mb_substr` in Str::beforeLast to ensure multibyte string compatibility and correct TeamCity test names for datasets in "describe" blocks
… mode

Previously, StateGenerator assumed all non-Errored events from
testErroredEvents() were BeforeFirstTestMethodErrored. However,
AfterLastTestMethodErrored events can also appear (e.g., when errors
occur during teardown), causing a TypeError crash in parallel mode.

This adds explicit instanceof checks for both BeforeFirstTestMethodErrored
and AfterLastTestMethodErrored, converting the latter to a
BeforeFirstTestMethodErrored for display purposes since both share the
same interface.

Fixes pestphp#1623
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.