Pester
Pester copied to clipboard
`Should` throws exception for legacy syntax when parameters are broken across lines
General summary of the issue
When Should parameters (with dashes!) are split across lines, a runtime exception gets thrown, stating "RuntimeException: Legacy Should syntax (without dashes) is not supported in Pester 5. Please refer to migration guide at: https://pester.dev/docs/migrations/v3-to-v4".
Describe your environment
Pester version : 5.3.1 C:\Users\jlove\Documents\WindowsPowerShell\Modules\Pester\5.3.1\Pester.psm1
PowerShell version : 5.1.19041.1320
OS version : Microsoft Windows NT 10.0.19044.0
Steps to reproduce
Describe ShouldLegacySyntax {
It "does not complain about syntax" {
1 + 1 | Should -Be 2
}
It "complains about syntax" {
1 + 1 | Should `
-Be 2
}
}
Expected Behavior
Both of the above tests should be treated equally and pass.
Current Behavior
The second above test fails with the syntax exception.
Possible Solution? (optional)
I ran into this issue when I tried splitting a long command across lines. Consider this line - when indented, it overran my editor's vertical ruler (120 chars):
Should -CommandName Install-Module -Exactly -Invoke -ModuleName $ModuleName -ParameterFilter $ParameterFilter1 -Times 2
# gets formatted to...
Should `
-CommandName Install-Module `
-Exactly `
-Invoke `
-ModuleName $ModuleName `
-ParameterFilter $ParameterFilter1 `
-Times 2
Yikes, that rule is nothing but trouble. :/
Not parameters in general, just the operator (Invoke\Be...). I'd recommend always using the operator as your first parameter for readability. As a bonus that would let you keep it on the first line and avoid this issue. Try:
Should -Invoke `
-CommandName Install-Module `
-Exactly `
-ModuleName $ModuleName `
-ParameterFilter $ParameterFilter1 `
-Times 2
Upvote on what Frode said, that makes it way easier to understand what that Should does and avoids the issue as well.
@nohwnd That seems like a bad workaround, it's much cleaner to only have a single parameter per line and either all on newlines or none.
A proper fix would be great, I don't this issue is 'solved', until the next version that removes them (and the messages) entirely lands.
Ok let's just remove the warning.