Skip to content

Hint that -ExpectedMessage uses wildcards when the message looks identical - #2956

Merged
nohwnd merged 2 commits into
pester:mainfrom
nohwnd:fix-1793-throw-wildcard-hint
Aug 6, 2026
Merged

Hint that -ExpectedMessage uses wildcards when the message looks identical#2956
nohwnd merged 2 commits into
pester:mainfrom
nohwnd:fix-1793-throw-wildcard-hint

Conversation

@nohwnd

@nohwnd nohwnd commented Aug 6, 2026

Copy link
Copy Markdown
Member

Should -Throw -ExpectedMessage matches with -like, so [ ] * ? are wildcards. When the actual message is identical to the expected one except for those characters, the match fails, but the failure message printed the same text on both sides:

Expected an exception with message like 'value is [1]' to be thrown, but the message was 'value is [1]'.

Same text on both sides, no hint why it failed. This bites people migrating from v4, where the message match used .Contains, and it is the cause of a few issues and StackOverflow questions.

Now that case adds a note:

Note: -ExpectedMessage matches using wildcards (-like). The messages are identical except for the
wildcard characters [ ] * ? in -ExpectedMessage. Escape them with a backtick (`[) or use
[System.Management.Automation.WildcardPattern]::Escape() to match them literally.

The note only shows when the actual message equals the expected one treated literally, so it does not fire on genuine mismatches or on intentional wildcards that just did not match.

Fixes #1793

🤖

nohwnd added 2 commits August 6, 2026 20:54
…tical

Should -Throw -ExpectedMessage matches with -like, so [ ] * ? are wildcards.
When the actual message is identical to the expected one except for those
characters, the match fails but the failure message printed the same text on
both sides ("with message like 'X' ... but the message was 'X'"), which is
confusing and bit people migrating from v4 where matching used .Contains.

Add a note in that case pointing at wildcard matching and how to escape.

Fixes pester#1793

🤖
Spell out the whole expected failure message in the test so it is obvious we
are matching for the wildcard hint, and move the note before the 'from <path>'
location so it reads in order and the test can assert it cleanly.

🤖
@nohwnd
nohwnd merged commit 6f8c5f9 into pester:main Aug 6, 2026
11 of 12 checks passed

@fflaten fflaten left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need the same for Should-Throw. Maybe make Hint a optional parameter in Fail so we can override the default Get-AssertionGotcha in special cases like this.

$but = Join-And $buts
$failureMessage = "Expected an exception$(if($filter) { " with $filter" }) to be thrown,$(Format-Because $Because) but $but. $actualExceptionLine".Trim()
$wildcardHint = if ($messageFailedOnWildcard) {
"$([System.Environment]::NewLine) Note: -ExpectedMessage matches using wildcards (-like). The messages are identical except for the wildcard characters [ ] * ? in -ExpectedMessage. Escape them with a backtick (``[) or use [System.Management.Automation.WildcardPattern]::Escape() to match them literally."

@fflaten fflaten Aug 6, 2026

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use same format as hints in v6 assertions?

"$formattedMessage`n`nHint: $hint"

nohwnd added a commit that referenced this pull request Aug 11, 2026
* Hint at unescaped wildcards on Should-Throw -ExceptionMessage

Should-Throw filters -ExceptionMessage with -like, so [ ] * ? are wildcards.
When the actual message is identical to the expected one treated literally,
the match fails only because of unescaped wildcard characters, and the two
messages look the same in the failure. Detect that case and add a hint, same
as the v5 Should -Throw does since #2956.

To carry the hint I added an optional Hint key to the ShouldAssertion.Fail
data. When present it overrides the default Get-AssertionGotcha hint and is
appended with the standard "Hint: <text>" format the other v6 assertions use,
so no new formatting path is introduced.

Fixes #2968
Fixes #2966

🤖

* Update src/functions/assert/Common/New-ShouldAssertion.ps1

Co-authored-by: Frode Flaten <3436158+fflaten@users.noreply.github.com>

---------

Co-authored-by: Frode Flaten <3436158+fflaten@users.noreply.github.com>
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.

ExpectedMessage comparison fails

2 participants