Skip to content

feat: give each check an optional message for the reason of a failure - #3

Merged
sorge-it merged 1 commit into
mainfrom
feat/assertion-messages
Oct 6, 2026
Merged

sorge-it merged 1 commit into
mainfrom
feat/assertion-messages

Conversation

@sorge-it

@sorge-it sorge-it commented Oct 6, 2026

Copy link
Copy Markdown
Owner

What

Each check takes a last, optional parameter string $message = '', as the checks of PHPUnit and Pest do. A failure shows it first:

expect($response)->toHaveSelectorCount('[data-cart] li', 3, 'The cart keeps the items of the last visit.');
The cart keeps the items of the last visit.
Failed asserting that (page) has 3 nodes matching "[data-cart] li".
  • 23 check methods of AssertsHtml, 22 expectations of Pest and eachMatch.
  • After the optional $value of toHaveSelectorAttribute(), the message goes by name: message: '…'.
  • Without a message, each failure keeps its text byte for byte. A test compares the full text.

Fixed

A wrong input or a missing region failed with an AssertionFailedError. Pest's ->not catches that and turned the check into a pass. These now stop the test with a LogicException, also under ->not:

  • NotAPage: a value that is not a page, and a frame without srcdoc.
  • NotOneNode: within() and frame() where the selector finds no node or more than one.
  • NoMatch: eachMatch() where no node matches.
  • ->not->eachMatch() stops with a LogicException: it would pass where one match fails.

The message of the test comes first in each of them.

Upgrade

  • A call needs no change for the message.
  • A subclass that overrides a method of AssertsHtml adds string $message = ''.
  • A test that expected a failure for a wrong value or a missing region expects the exceptions above. PHPUnit counts them as errors.

Test

  • composer test on PHP 8.5: 214 tests, PHPStan max, type coverage 100 %.
  • Each new test was red once: against v1.2.0 or under a mutation.
  • Five review rounds. No finding is open.

The 23 methods of AssertsHtml, the 22 expectations of Pest and eachMatch
take a last parameter $message. A failure shows it first. A value that is
not a page and a missing region now stop the test with NotAPage,
NotOneNode or NoMatch, so ->not cannot pass on them.
@sorge-it
sorge-it force-pushed the feat/assertion-messages branch from e080b27 to 42e54c7 Compare October 6, 2026 10:07
@sorge-it
sorge-it merged commit c24be93 into main Oct 6, 2026
3 checks passed
@sorge-it
sorge-it deleted the feat/assertion-messages branch October 6, 2026 10:47
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.

1 participant