Pester icon indicating copy to clipboard operation
Pester copied to clipboard

New Should Parameter: -And which would behave like -PassThru (and have -PassThru as alias)

Open JustinGrote opened this issue 4 years ago • 5 comments

Summary of the feature request

A common flow I have when doing multiple "Should" in a test looks like:

$myresult = Invoke-MyCommand
$myresult | Should -BeOfType [String]
$myresult | Should -Be 'myresult'

I would like to simply chain these statements together, especially using Powershell 7 Kusto-style pipeline syntax.

How should it work? (optional)

A new parameter -And or -Passthru which simply emits the object back to the pipeline, enabling this style of test declaration:

Invoke-MyCommand
| Should -BeOfType [String] -And
| Should -Be 'MyResult'

Or alternatively:

Invoke-MyCommand
| Should -BeOfType [String] -PassThru
| Should -Be 'MyResult'

The parameter should be implemented as -And but have an alias to -PassThru since that is a common parameter usage familiar to Powershell users.

Willing to attempt a PR if labeled up-for-grabs.

JustinGrote avatar Apr 15 '21 23:04 JustinGrote

Have you seen the continue on error option for should?

nohwnd avatar Apr 16 '21 16:04 nohwnd

https://pester.dev/docs/assertions/should-command#collect-all-should-failures

nohwnd avatar Apr 16 '21 16:04 nohwnd

@nohwnd The issue is not using multiple should in a function, that works fine with the first example.

The issue is to be able to use the truncated syntax shown in the second example, right now I cannot do that because Should doesn't output anything, so I have to keep operating against an existing resultant variable.

JustinGrote avatar Apr 16 '21 16:04 JustinGrote

I think there is a proposal for this somewhere here, probably one of the issues in Better Should milestone. I was considering it, and am not against implementing it, but it was a bit limited, because only one assertion can fail. Granted you could use both this chaining and the error collecting, but when I tried it, it turned out to be rarely practical because you don't have an easy way of selecting a property from a bigger object so you will end up using variables anyway.

nohwnd avatar Apr 17 '21 12:04 nohwnd

I think there is a proposal for this somewhere here, probably one of the issues in Better Should milestone.

Probably this https://github.com/pester/Pester/issues/1319#issuecomment-499069698 Stumbled over it when removing an invalid example in the Should-help. The example was the one used to illustrate the compound assertion-concept 🙂

fflaten avatar Jun 26 '22 16:06 fflaten