Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion src/functions/assertions/PesterThrow.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,18 @@
}

$filterOnMessage = -not [string]::IsNullOrWhitespace($ExpectedMessage)
$messageFailedOnWildcard = $false
if ($filterOnMessage) {
$unescapedExpectedMessage = [System.Management.Automation.WildcardPattern]::Unescape($ExpectedMessage)
$filters += "message like $(Format-Nicely $unescapedExpectedMessage)"
if ($actualExceptionWasThrown -and (-not (Get-DoValuesMatch $actualExceptionMessage $ExpectedMessage))) {
$buts += "the message was $(Format-Nicely $actualExceptionMessage)"
# -ExpectedMessage matches with -like. When the actual message is identical to the expected
# one treated literally, the only reason the match failed is unescaped wildcard characters
# ([ ] * ?) in -ExpectedMessage. Flag it so the failure message is not baffling (#1793).
if ($actualExceptionMessage -eq $unescapedExpectedMessage) {
$messageFailedOnWildcard = $true
}
}
}

Expand All @@ -120,7 +127,13 @@
if ($buts.Count -ne 0) {
$filter = Join-And $filters
$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"

}
else {
""
}
$failureMessage = "Expected an exception$(if($filter) { " with $filter" }) to be thrown,$(Format-Because $Because) but $but.$wildcardHint $actualExceptionLine".Trim()

$ActualValue = $actualExceptionMessage
$ExpectedValue = if ($filterOnExceptionType) {
Expand Down
25 changes: 25 additions & 0 deletions tst/functions/assertions/PesterThrow.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,31 @@ InPesterModuleScope {
$err.Exception.Message -replace "(`r|`n)" -replace '\s+', ' ' -replace '(char:).*$', '$1' | Verify-Equal $assertionMessage
}

It 'hints at wildcard matching when the message is identical except for unescaped wildcard characters' {
# #1793: -ExpectedMessage matches with -like, so [ ] * ? are wildcards. When the expected
# and actual messages look identical, the failure is baffling without a hint pointing at it.
$testDrive = (Get-PSDrive TestDrive).Root
$testScriptPath = Join-Path $testDrive test.ps1
Set-Content -Path $testScriptPath -Value "throw 'value is [1]'"

# spell out the whole expected message (the hint sits before the 'from <path>' location)
$assertionMessage = "Expected an exception with message like 'value is [1]' to be thrown, but the message was 'value is [1]'. 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."

$err = { { & $testScriptPath } | Should -Throw -ExpectedMessage 'value is [1]' } | Verify-AssertionFailed
$err.Exception.Message -replace "(`r|`n)", ' ' -replace '\s+', ' ' -replace ' from .*$', '' | Verify-Equal $assertionMessage
}

It 'does not hint at wildcard matching when the messages genuinely differ' {
$testDrive = (Get-PSDrive TestDrive).Root
$testScriptPath = Join-Path $testDrive test.ps1
Set-Content -Path $testScriptPath -Value "throw 'error1'"

$err = { { & $testScriptPath } | Should -Throw -ExpectedMessage 'error2' } | Verify-AssertionFailed
if ($err.Exception.Message -like '*matches using wildcards*') {
throw "Did not expect the wildcard hint, but got: $($err.Exception.Message)"
}
}

It 'returns the correct assertion message when exceptions messages differ' {
$testDrive = (Get-PSDrive TestDrive).Root
$testScriptPath = Join-Path $testDrive test.ps1
Expand Down
Loading