pester / Pester

Pester is the ubiquitous test and mock framework for PowerShell.
https://pester.dev/
Other
3.11k stars 473 forks source link

Should -Throw doesn't perform substring matching #2557

Closed jackmtpt closed 3 months ago

jackmtpt commented 3 months ago

Checklist

What is the issue?

The documentation (https://pester.dev/docs/v4/usage/assertions#throw) says that the following code should work:

{throw "foo bar baz"} | Should -Throw "bar" # Test will pass

It does not; the assertion fails.

Expected Behavior

The test should pass, instead, the following error is generated:

InvalidResult: Expected an exception with message like 'foo' to be thrown, but the message was 'foo bar baz'. from line:1 char:2     + {throw "foo bar baz"} |
Should -Throw "foo" # Test will pass     +  ~~~~~~~~~~~~~~~~~~~

Steps To Reproduce

{throw "foo bar baz"} | Should -Throw "bar"

Describe your environment

Pester 5.6.1, Win11, pwsh 7.4.4

Possible Solution?

No response

johlju commented 3 months ago

In Pester v4 that might have worked but in Pester v5 it is not a wildcard by default. It works now by adding *.

Describe 'Something' {
    It 'Should throw' {
        { throw 'foo bar baz' } | Should -Throw '*bar*'
    }
}

Not sure if new behavior is document somewhere in the v5 documentation. But it is mentioned here in breaking changes: https://github.com/pester/Pester/releases/tag/5.0.0

jackmtpt commented 3 months ago

yeah i realised that after i posted this issue. i'd delete it but apparently i can't.

johlju commented 3 months ago

No need to delete, it is good knowledge for others that hit the same issue. I think it could also been kept open so it could be tracked to get the documentation updated. 🙂

jackmtpt commented 3 months ago

https://pester.dev/docs/assertions/#throw the docs were updated, I just can't google good 😆

fflaten commented 3 months ago

I just can't google good 😆

Or... ideally v5 docs should rank higher. If anyone knows a SEO trick, let us know :)