Skip to content

Hint at unescaped wildcards on Should-Throw -ExceptionMessage - #2970

Merged
nohwnd merged 2 commits into
pester:mainfrom
nohwnd:should-throw-wildcard-hint
Aug 11, 2026
Merged

Hint at unescaped wildcards on Should-Throw -ExceptionMessage#2970
nohwnd merged 2 commits into
pester:mainfrom
nohwnd:should-throw-wildcard-hint

Conversation

@nohwnd

@nohwnd nohwnd commented Aug 10, 2026

Copy link
Copy Markdown
Member

Should-Throw matches -ExceptionMessage with -like, so [ ] * ? are wildcards. When the actual message is identical to the expected one treated literally, the match fails only because those characters were not escaped, and the failure shows two messages that look the same. This is the v6 counterpart of #2956, which added the same hint to the v5 Should -Throw.

To carry the hint I added an optional Hint key to the ShouldAssertion.Fail data (issue #2968 suggested making Hint overridable). When present it overrides the default Get-AssertionGotcha hint and is appended with the standard Hint: <text> format the other v6 assertions already use, so #2966 is covered without a new formatting path.

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

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

Fixes #2968
Fixes #2966

🤖

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 pester#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 pester#2968
Fixes pester#2966

🤖
Comment thread src/functions/assert/Common/New-ShouldAssertion.ps1 Outdated
Co-authored-by: Frode Flaten <3436158+fflaten@users.noreply.github.com>
@nohwnd
nohwnd enabled auto-merge (squash) August 11, 2026 17:18
@nohwnd
nohwnd disabled auto-merge August 11, 2026 17:53
@nohwnd
nohwnd merged commit f3e9149 into pester:main Aug 11, 2026
10 of 12 checks passed
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.

Expand ExpectedMessage wildcard hint to Should-Throw Use same format as hints in v6 assertions?

2 participants