pest icon indicating copy to clipboard operation
pest copied to clipboard

[Bug]: Exception assertion does not match full Exception-Message

Open jackmorizo opened this issue 10 months ago • 6 comments

What Happened

When I use it()->throws() then only the first part of the expected Exception Message gets evaluated.

How to Reproduce

// helpers.php
function shouldFail(){
    throw new Exception("abcdef");
}

// helpersTest.php
it("Passes but should fail", function() {
    shouldFail();
})->throws(\Exception::class,"a");
it("Passes but should fail", function() {
    shouldFail();
})->throws(\Exception::class,"ab");
it("fails correctly", function() {
    shouldFail();
})->throws(\Exception::class,"abcdefghijk");

Sample Repository

No response

Pest Version

3.7.4

PHP Version

8.3.15

Operation System

Linux

Notes

I am not sure if this is a big issue but it could cause problems if people require the exception-messages to be exact.

jackmorizo avatar Feb 28 '25 11:02 jackmorizo

Hi @jackmorizo, This is not a bug. throws(\Exception::class,"your-sub-string"))

This checks if the exception message contains "your-sub-string".

If you want to match the exception message exactly you can do it like follows:

function shouldFail(){
    throw new Exception("abcdef");
}

it("Fails correctly", function() {
	try {
		shouldFail();
	}
	catch(Exception $e){
		expect( $e->getMessage())->toBe('a');
	}
    
});

it("Passes correctly", function() {
	try {
		shouldFail();
	}
	catch(Exception $e){
		expect( $e->getMessage())->toBe('abcdef');
	}
});

alihaiderx avatar Mar 08 '25 15:03 alihaiderx

Hey @maintainers, after investigating this issue, it seems to be expected behavior. I have mentioned the solution to @jackmorizo query. I believe this issue can be closed. What do you think?

alihaiderx avatar Mar 08 '25 15:03 alihaiderx

Thanks @alihaiderx and sorry for the late response. Are you sure this is expected behaviour or just an undefined/undocumented edge-case?

I have no opinion towards what the expected behaviour should be. However I think it would be good to write it down somewhere

I do not see where it is defined that the $exceptionMessage is a sub-string https://github.com/pestphp/pest/blob/ed70c9dc2b6ed2ded11a2784f3a80963ddf9b87d/src/PendingCalls/TestCall.php#L149

also the Documentation does not imply that this is in fact a substring https://pestphp.com/docs/exceptions.

jackmorizo avatar Mar 17 '25 23:03 jackmorizo

@jackmorizo you are right it is not documented yet. The document needs update.

alihaiderx avatar Mar 20 '25 22:03 alihaiderx

@alihaiderx should I close the ticket or wait until the documentation has been updated?

jackmorizo avatar Mar 29 '25 08:03 jackmorizo

@jackmorizo We should keep it open, so the documentation update is not forgotten.

alihaiderx avatar Mar 30 '25 11:03 alihaiderx

Just to clarify: This is standard behavior in PHPUnit as well.

At this point, pest simply uses PHPUnit here, as far as I can tell:

src/PendingCalls/TestCall.php -> https://github.com/sebastianbergmann/phpunit/blob/main/src/Framework/TestCase.php#L1059 -> https://github.com/sebastianbergmann/phpunit/blob/main/src/Framework/TestCase.php#L2200

tpraxl avatar Jul 07 '25 15:07 tpraxl