Apply value-assertion rules consistently to Should-* siblings#2707
Merged
Conversation
The Pester 6 assertion rules in docs/assertion-types.md say generic value assertions reject collections on -Expected, and value assertions unwrap pipeline input via Collect-Input -UnrollInput. A handful of Should-* didn't follow either rule, behaving differently from their siblings: - Should-NotBeString: missing Collect-Input. Multi-item pipeline silently used the last item; non-string Actual silently passed the assertion. Now mirrors Should-NotBeLikeString and throws ArgumentException when Actual isn't a string. - Should-BeAfter / Should-BeBefore: missing Collect-Input. Also missing the -Because parameter declaration entirely, so the documented switch was a no-op. Both fixed. - Should-BeSame / Should-NotBeSame: missing Ensure-ExpectedIsNotCollection, so passing @() to -Expected produced a misleading "not the same instance" failure instead of the standard collection-on-Expected error. Tests added to lock in each rule. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This was referenced Jun 11, 2026
nohwnd
added a commit
that referenced
this pull request
Jun 11, 2026
The doc example `(Get-Date).AddDays(1) | Should-BeAfter (Get-Date)` errored with "Cannot bind positional parameters because no names were given." The `Fluent` and `Expected` parameter sets both declared a position-0 parameter (`$Time` and `$Expected`), so a `DateTime` at position 0 matched both sets and PowerShell could not pick one. Split the `Fluent` parameter set into `FluentAgo` and `FluentFromNow`, each requiring its mandatory switch. With `-Ago`/`-FromNow` no longer optional within their set, neither fluent set is satisfiable from just a positional `[DateTime]`, so the `Expected` set wins unambiguously. Side effects: - `Should-BeAfter 10minutes -Ago -FromNow` now fails at binding (no parameter set can be resolved) instead of the runtime check. - The runtime "you must provide -Ago or -FromNow" check is gone, since binding enforces it. Added doc-style positional tests on both `Should-BeAfter` and `Should-BeBefore`. Verified the full Pester suite: 2171 passed, 0 failed. Follow-up to #2707. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The Pester 6 assertion rules in
docs/assertion-types.mdsay that generic value assertions reject collections on-Expected, and that value assertions unwrap pipeline input viaCollect-Input -UnrollInput. A handful ofShould-*didn't follow either rule and behaved differently from their siblings.Caught by auditing all
Should-*forCollect-Input/Ensure-ExpectedIsNotCollectionusage and reproducing the gaps with a built module:Should-NotBeString— missingCollect-Input. Multi-item pipeline silently used the last item; non-string-Actualsilently passed the assertion (Test-StringEqualreturned$false, no throw). Now mirrorsShould-NotBeLikeStringand throwsArgumentExceptionwhen-Actualisn't a string.Should-BeAfter/Should-BeBefore— missingCollect-Input. Also missing the-Becauseparameter declaration entirely, so the documented switch was a no-op (the throw path read$Becausefrom an unbound variable). Both fixed.Should-BeSame/Should-NotBeSame— missingEnsure-ExpectedIsNotCollection, so passing@()to-Expectedproduced a misleading "not the same instance" failure instead of the standard collection-on--ExpectedArgumentException.Tests added next to each fix lock in the rule.
All 653 tests under
tst/functions/assertpass on net8.0.