pester / Pester

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

Should -Throw doesn't perform substring matching #2557

Closed jackmtpt closed 3 weeks ago

jackmtpt commented 3 weeks 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 weeks 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 weeks ago

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

johlju commented 3 weeks 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 weeks ago

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

fflaten commented 3 weeks ago

I just can't google good 😆

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