Skip to content

6.3.0

Latest

Choose a tag to compare

@nohwnd nohwnd released this 05 Dec 09:43
5f29e9d

Pester 6.3.0

🙋 Want to share feedback or report a bug? Open an issue or start a discussion.

New features

Say why a test is skipped, with -Because

-Skip said that a test was skipped and never why. Now It, Describe and Context take -Because:

It 'talks to the API' -Skip -Because 'the API is down' { }
Describe 'integration' -Skip -Because 'needs Azure' { }

The reason is in the output, on the result object, and in the NUnit3 and JUnit reports:

$r.Skipped[0].Reason   # the API is down

The parameter is -Because and not -Reason so that the whole module uses one word for why: Set-ItResult -Because and Should -Because already did. The result property is .Reason, which is the word NUnit and JUnit use for it.

Set-ItResult -Because fills the same property, so a test skipped from inside its own body reports its reason the same way a test skipped by -Skip does.

Mock a command that aggregates the pipeline, with -Aggregate

A command that takes pipeline input and returns once from its end block could not be mocked. MockWith runs once per piped item, so the mock returned one object per input where the real command returns one, and Should -Invoke counted one call per item.

function outer { 1..3 | inner }
function inner {
    param ([Parameter(ValueFromPipeline)] $InputObject)
    "RETURN"   # implicit end block, runs once
}

Mock inner { "FOO" }
@(outer).Count   # 3, the real inner returns 1

Mock takes -Aggregate now. The process block only collects the pipeline, and MockWith runs once from the end block over everything it collected.

Mock inner { "FOO" } -Aggregate
@(outer).Count   # 1

The collected input is piped into MockWith, so it is there as $input, and MockWith can have its own begin, process and end blocks, like the real command.

Mock Save-State { "saved $(@($input).Count) items" } -Aggregate
1..3 | Save-State   # "saved 3 items"

Should -Invoke counts one call per pipeline, and the parameter filter sees the whole collected input bound to the pipeline parameter, so you can assert on all of it at once:

Should -Invoke inner -Times 1 -Exactly -ParameterFilter { $InputObject -contains 2 }

All mocks for one command have to be the same kind. A per-call mock records one call per item and an -Aggregate mock records one call per pipeline, and mixing the two for the same command would put both granularities in the call history, which Should -Invoke cannot count coherently. It is rejected when the second mock is defined. The check looks at the current scope only, so a parent-scope -Aggregate mock and a child-scope per-call mock for the same command get past it, and then the count under Should -Invoke -Scope Describe is confusing.

Piping to a mocked native application cannot be asserted on by parameter name, a native application has no ValueFromPipeline parameter so nothing binds by name. The input is still collected and piped into MockWith.

Stricter checks and clearer failures

A bad configuration is an error, and it names every problem

6.2.0 started checking for configuration mistakes, but it treated the two kinds differently. A value an option cannot use threw. A key the configuration has no option for only warned, and only from Invoke-Pester, so New-PesterConfiguration -Hashtable and a plain [PesterConfiguration] cast said nothing at all. A warning scrolls past in CI and the run continues on the default, which is the silent failure the checks were added to stop.

Both kinds are errors now, both when the hashtable is converted, and one conversion reports every problem:

Invoke-Pester -Configuration @{ Run = @{ Paralel = $true; Exit = "yes"; Shufle = $true } }
# 6.2.0
WARNING: Ignoring configuration key 'Run.Paralel', there is no such option.
# the run continues on the defaults, and only Run.Exit is ever mentioned

# 6.3.0
The configuration has 3 problems:
  - Run.Paralel: no such option, did you mean 'Run.Parallel'?
  - Run.Shufle: no such option, did you mean 'Run.Shuffle'?
  - Run.Exit expects a bool, but got the string 'yes'.
'Get-Help about_PesterConfiguration' lists all the options.

The suggestion is the closest known name within one edit per four characters, by Levenshtein distance, so Paralel finds Parallel, Runn finds Run, and Zzz finds nothing and says so. A single problem stays the sentence it already was, with no list around it, so every message that exists today is unchanged.

There is no opt-out. The only real argument for not throwing is forward compatibility, a config written for a newer Pester running on an older 6.x, and a configuration hashtable has nothing else it should be carrying. Say the word if you need one.

Assertions stop passing on input they cannot judge

An assertion that finishes without asserting anything is worse than one that fails, because the test goes green and nobody looks at it again. I went through all 41 assertions in src/functions/assert looking for that one shape, then called every one of them with input it cannot judge and looked at which ones passed.

An empty filter passed:

1, 2, 3 | Should-All { }    # 6.2.0: passed
1, 2, 3 | Should-Any { }    # 6.2.0: passed

An empty scriptblock produces no output, and no output was taken to mean the filter asserted on the item and did not throw. Those are not the same thing, and a scriptblock with no statements cannot say anything about any item.

A falsy result of another type passed:

1, 2, 3 | Should-All { 0 }     # 6.2.0: passed
1, 2, 3 | Should-All { '' }    # 6.2.0: passed

The result counted as a failure only when it was exactly the boolean $false. The documented contract is that the filter returns true or does not throw, so the result is judged on whether it is true, whatever its type. A truthy value of another type still passes.

A date assertion compared strings:

'a string'      | Should-BeAfter (Get-Date)      # 6.2.0: passed
[datetime]::Now | Should-BeAfter -Expected 42    # 6.2.0: passed

Should-BeAfter and Should-BeBefore have no dispatch on type. They compare with a relational operator and let PowerShell decide what that means, and with a string on the left PowerShell compares as strings, where 'a string' sorts after a formatted date. -Expected 42 went wrong one level up, in the binder: the parameter was declared [DateTime], so 42 became 42 ticks, which is the year 1, and every real date is after the year 1. Both parameters are checked by the assertion itself now. A [datetimeoffset] still works, it is not a [datetime] but compares with one.

And an expected value that is neither a type nor a type name passed:

1 | Should-NotHaveType 42    # 6.2.0: passed, PSTypeNames does not contain $null
1 | Should-HaveType 42       # 6.2.0: failed saying it expected the type $null

Both say what is actually wrong now.

A mock failure points at the call site

The "Performed invocations" list in a Should -Invoke failure printed from <file>:<line> for every recorded call, and that line was the one the Mock was defined on, not the one that made the call. Every entry showed the same line, so the location could not tell the listed calls apart, which is the one thing it is there for. The 6.0.0 release notes example has it too.

Before:

Performed invocations:
  [*] SendEmail -To 'alice@example.com' -Subject 'Welcome' from demo.Tests.ps1:4
  [ ] SendEmail -To 'bob@example.com' -Subject 'Welcome' from demo.Tests.ps1:4

and demo.Tests.ps1:4 is the Mock line. After, the lines are the two calls.

Fixes in the experimental parallel runner

Every parallel worker gets its own session state

Run.Parallel built its workers with RunspaceFactory.CreateRunspacePool, which takes one InitialSessionState and hands that same instance to every runspace in the pool. Import-Module adds a module's FormatsToProcess entries to that collection and then enumerates it, so one worker enumerated while another added and List<T> threw Collection was modified; enumeration operation may not execute. Pester declares FormatsToProcess itself and every worker imports Pester, which is why a suite whose own modules have no format files hit it too.

RunspacePool has no way to give each runspace a session state of its own, so Invoke-InRunspacePool keeps its own pool now, one runspace per slot, each from its own session state, up to ThrottleLimit.

A parallel run prints what a sequential run prints

Two things made Run.Parallel print something other than a sequential run of the same files.

Every warning was printed twice, and three times for the workers that started first. The runspace pool is created with the caller's $Host, so a worker's Write-Warning already reaches the console while the worker runs, and the loop that re-emitted Streams.Warning after EndInvoke printed it again. Errors keep their re-emit, they only land in Streams.Error and never reach the host on their own.

And a worker ran with default preference variables. A fresh runspace has WarningPreference = 'Continue' and VerbosePreference = 'SilentlyContinue' whatever the caller was running with, so a caller who had silenced warnings still got them and a caller who had asked for verbose output got none. The worker runspaces take the caller's preferences now.

Potentially breaking changes

Every entry here can turn a green suite red, or change what a report consumer reads, when the suite relied on the old behavior. Most of them come from the assertion audit above. Each one of those is a case where something passed, or reported a value, without being able to judge the input, so a test that relied on it was not testing what it looks like it was testing. The entries at the end of the list are not from the audit.

  • Should-BeCollection compares the items in order. It passed when both collections held the same items in a different order:

    1, 3, 2 | Should-BeCollection @(1, 2, 3)   # 6.2.0: passed

    The whole comparison sat inside if (-Not $InOrder), and $InOrder was not a parameter and was never assigned, so the any-order branch always ran and the in-order branch was never written. In order is the default now and -IgnoreOrder opts out, the way Should-ContainCollection already works.

  • A configuration key that is not an option throws. It used to warn, from Invoke-Pester only. GetUnknownKeys() is gone with the warning that was its only caller.

  • Should-All and Should-Any reject an empty filter, and judge the result on truthiness. { }, { 0 } and { '' } fail now.

  • Should-HaveType and Should-NotHaveType reject an expected value that is neither a type nor a type name.

  • Should-BeAfter and Should-BeBefore need a [datetime] or a [datetimeoffset] on both sides. A string, a hashtable, or a number for -Expected fails instead of being compared as text or converted to ticks.

  • A fluent time string has to be the whole string, and the unit has to be spelled out. The pattern had no anchor at the end and matched the unit by its first letters:

    string 6.2.0 6.3.0
    1month 1 minute throws
    1months 1 minute throws
    1 week ago 7 days throws
    1sfoo 1 second throws
    1 s junk 1 second throws

    1month is the one that matters: month starts with m, so a bound of one month was read as one minute and nothing was said about it. Everything that parsed before still parses.

  • Should-BeFasterThan and Should-BeSlowerThan no longer write the measured scriptblock's output. & $Actual put whatever the scriptblock wrote into the assertion's own output, so $out = { 1, 2, 3 } | Should-BeFasterThan 30s gave $out.Count of 3. The [timespan] branch wrote nothing, and an assertion should write nothing.

  • The default NUnitXml report wraps a loose test in a suite when its block also has child blocks. resultsType in the NUnit 2.5 schema is an xs:choice, so results holds either test-suite children or test-case children and never both. A Describe holding a Context plus a stray It produced both and the report stopped validating. Those reports were already invalid and the schema leaves no narrower fix, but a consumer reading //test-suite/results/test-case will no longer find those tests. NUnit2.5 is an alias for NUnitXml, which is the default TestResult.OutputFormat, so this is the default report.

  • The JUnit classname is the path of the blocks, not the path of the file. CI reports show classname as the suite name, so the suite name used to be an absolute path including whatever folders the CI runner happens to use. The file is in a new file attribute, relative to Run.RepoRoot and with forward slashes:

    <!-- 6.2.0 -->
    <testcase name="Calculator.addition.adds two numbers" classname="/builds/me/repo/tests/unit/Thing.Tests.ps1" />
    
    <!-- 6.3.0 -->
    <testcase name="Calculator.addition.adds two numbers" classname="Calculator.addition" file="tests/unit/Thing.Tests.ps1" />

    name keeps the full path of the test, because CI reports do not show a tree view.

  • Run.ShuffleSeed reproduces one file's order on its own. One [System.Random] was built from the seed for the whole run and consumed in sequence, the file order first and then each file's blocks and tests, so a file's order depended on how many values had already been drawn. Rerunning one failing file with the seed from CI, which is the point of having a seed, gave a different order and the failure did not have to come back. Each file has its own generator now, which means the order a given seed produces is not the order it produced in 6.2.0.

  • A template keeps its text when there is no variable to put in it. Describe "<Foo>" with no $Foo printed Describing $null, and an invalid template like <Foo/Bar> printed a ParseException showing Pester's own generated expression and the test did not run at all. Both print the template as written now, and the test runs.

  • Pester 6 refuses to load when Pester 4 or older is already loaded. It used to load next to it, and the run that followed printed two summaries that disagreed, wrote TestGroups stack corrupted as a non-terminating error, and left Pester 4's result object reporting zero tests. A pipeline that decides from $r.FailedCount -eq 0 calls that green. Nobody imports both on purpose: a Pester 4 run reaches a file that calls a Pester 5 command, such as BeforeDiscovery, and PowerShell autoloads this version from PSModulePath in the middle of the run. The import throws now, and the message names that cause.

  • A mock assertion in a Describe or Context body fails discovery. Should -InvokeVerifiable placed directly in a block body runs during discovery, where no mock is set up and nothing has been called, so the list of verifiable mocks was empty, "all verifiable mocks were called" was true, and the run was green with the assertion never asserting anything. Pester 4 ran that body after the tests in the block, so a file moved from Pester 4 has the assertion there by construction. Move it into an AfterAll, an AfterEach, or the It that calls the mock. Should -Invoke in the same position already failed, but on the command name, which pointed at the wrong thing.

  • Inside a mock body, the command being mocked means the real command. It used to call the mock again, and recursed until PowerShell's call depth limit, which took about twelve seconds and ended in ScriptCallDepthException. Calling the real command from inside the mock body is how you replace the mock fall-through that Pester 6 removed, and that recipe works as written now, with or without -ModuleName:

    Mock Get-Command { & (Get-Command Get-Command -CommandType Cmdlet) @PesterBoundParameters }

    Every other mock still applies inside the body and still counts. The body's call to the command it mocks is not counted as a call on that mock.

  • Should-Throw -AllowNonTerminatingError no longer returns the errors it ignores. The redirect sat on an assignment, where errors escape it, so the ignored errors landed in the assertion's own output and came back next to the error record it reports:

    $err = { Write-Error 'ignored'; throw 'real' } | Should-Throw -AllowNonTerminatingError
    $err.Exception.Message   # was @('ignored', 'real'), now 'real'

    The help for the switch said the assertion passes if a non-terminating error is thrown, which is the opposite of what the switch does. It runs the script block with $ErrorActionPreference = 'Continue', so non-terminating errors are ignored and only a terminating error satisfies the assertion. The help says that now, and says which preference the script block runs with.

  • A <token> in a test name renders the value, not the type. The name is written by the same formatter that writes assertion messages, and for a value it does not walk it prints the type, which is the same string for every value of that type. Three versions came out with one name three times. A type that has a short string form of its own is asked for that form first:

    value 6.2.0 6.3.0
    [version]'1.2.3' [version] 1.2.3
    [uri]'https://example.com/a' [uri] https://example.com/a
    [pscustomobject]@{A=1;B='two'} [PSObject] @{A=1; B=two}

    Test names change with it, so a report compared against 6.2.0 shows those tests as renamed.

Other improvements and fixes

  • Invoke-Pester no longer overwrites the caller's $p and $d. Invoke-InNewScriptScope dot-sourced its wrapper, which is bound to the caller's session state, so the wrapper's parameters were bound in the caller's scope and private: did not prevent it. A script that kept its own path in $p and wrote to it after running tests wrote into Pester's installation instead. The wrapper is called with & now, the way Invoke-File already called its own.
  • A relative Run.RepoRoot is resolved against the current location. Only the code coverage plugin made it absolute, so without coverage it reached the setup chain as it was, GetFullPath resolved it against the process working directory, and every Pester.BeforeContainer.ps1 silently did not apply.
  • The repository root is found when .git is a file instead of a directory. In a git worktree, in a submodule, and in a clone made with --separate-git-dir, .git holds the path to the real git directory, so the walk went past the actual root and fell back to the location the run started from. That moved the Pester.BeforeContainer.ps1 cascade and the prefix the coverage report strips off its paths, and nothing reported it.
  • A verifiable mock in an outer scope that an inner scope overrides is no longer required. A mock marked -Verifiable in a Describe that you then override in an It could never pass Should -InvokeVerifiable: the inner mock handled the call, but the outer behavior was still collected and reported as never invoked. A verifiable mock that is not shadowed by a closer default mock is still required.
  • Should-Throw reports the exception the test threw under a non-English UI culture. It recognized the InvokeWithContext wrapper by matching InvokeWithContext in the exception message, and PowerShell quotes the method name with the quotation marks of the current UI culture, so under de-DE the match failed and the assertion reported the wrapper instead.
  • A strict Should-BeEquivalent on a FileInfo or a DirectoryInfo fails in the usual milliseconds instead of 12 to 30 seconds. The time went into one verbose message: listing the properties the actual object has and the expected one does not formatted each PSPropertyInfo, which reads its Value, so it walked Directory, Parent, Root, PSDrive and PSProvider and formatted the object graph behind each one.
  • Hashtables are pretty-printed in a Should-BeEquivalent failure, and only the leaf differences are reported. Format-Hashtable2 did not take -Pretty at all, so it joined its entries on one line while objects and collections were indented, and the hashtable comparer recorded a difference at every level it walked through, prepending the whole thing to the differences below it.
  • A discovery failure reads like a user error. An empty -ForEach is a mistake in the test data and the output made it look like a crash in Pester, with an internal stack trace that ignored Output.StackTraceVerbosity.
  • A nested Invoke-Pester inside a parallel worker writes to its own caller instead of to the worker's output tape.
  • about_PesterConfiguration says how Run.RepoRoot affects the paths in the reports.
  • New-MockObject -Properties keeps its properties when the mock is read through Select-Object -ExpandProperty. The properties were added with PSObject.Properties.Add(), and passing a value to a .NET method unwraps its PSObject, so Select-Object wrapped the bare value again and the new wrapper did not see them. A nested mock read through two -ExpandProperty steps returned $null.
  • Should written without dashes says what is wrong. Pester 3 and 4 wrote the operator as Should Be, and a suite pinned to Pester 4 that goes straight to 6 got ParameterBindingException: Parameter set cannot be resolved with nothing about the syntax. Pester 5 had this message and it was removed on the assumption that a Pester 6 user arrives from Pester 5 and has seen it once already. The message is back, it names the operator you wrote, and it links the migration guide.
  • -ForEach says whether it got $null or an empty array. The two are different mistakes with different fixes. $null is usually a variable assigned in BeforeAll, which does not exist yet while -ForEach is evaluated, because that happens during discovery, so the fix is BeforeDiscovery. The old message suggested -AllowNullOrEmptyForEach, which keeps those tests from ever running.
  • The assembly version check runs when Pester is imported by its root module. It read RequiredAssemblyVersion from the manifest PrivateData, which PowerShell fills in only for an import through the manifest, so PrivateData was empty, the minimum was $null, and [version]"5.9.1.0" -lt $null is $false. The guard silently did nothing, which is the VSCode case its own message describes.
  • A data-driven block whose own setup fails is reported under the name the run used. The name was expanded after BeforeAll, so three failing iterations of the same block all read Test <_> with nothing to tell them apart.
  • The tests a failed setup never ran are printed, at Detailed and Diagnostic. A setup that throws fails every test under it, those tests never run, so nothing printed them, while the result object and the reports had them as failed all along.
  • Debug.ShowStartMarkers prints the expanded name of a data-driven test. The marker is written before the setup that expands the name, so it printed [|] Iteration <_>... for every iteration.

Full Changelog: 6.2.0...6.3.0

Thank you

Thank you to everyone who filed issues and sent fixes.

@GioAbq for the $p and $d fix, @TohruSu for the relative Run.RepoRoot fix, @fflaten for migrating the test suite to the v6 assertions, @dirtyvanity for the New-MockObject fix, @dleontev for the Run.RepoRoot documentation, and @corbob, whose work on the skip reason this release finally finished.

@AlonGvili for asking for the skip reason in the first place, @fsackur for the mock that could not aggregate the pipeline, @rcarpenter79 for the invalid NUnit report, @SESEric for the shadowed verifiable mock, @kborowinski for the duplicated parallel warnings, @ReneRebsdorf and @jazzdelightsme for the parallel import failure, @iRon7 for the templates, @LethiferousMoose for Should-BeCollection ignoring item order, @briantist for the JUnit classname, @frottke for the Should-Throw culture bug, @chrisdent-de for the mock object losing its properties, @indented-automation for the -AllowNonTerminatingError help saying the opposite of what the switch does, @davidmatson for the start markers and for the tests nobody printed, and @crlgb for the data-driven block name.

Questions?

Open an issue or start a discussion.