Jakub Jareš

Pester5 - importing your ps1 files

Read more

In Pester 5 you should put your script setup inside of a BeforeAll block. If you are still using this historical approach, then it will no longer work:

BeforeAll {
    $here = Split-Path -Parent $MyInvocation.MyCommand.Path
    $sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path).Replace(".Tests.", ".")
    . "$here\$sut"
}

Describe "Get-Emoji" {
    It "Gets cactus" {
        # your test code
    }
}

Pester5 - discovery and script setup

Read more

As described in the v5 readme, the fundamental change in Pester5 is that Pester now runs in two phases: Discovery and Run. During Discovery Pester finds all your tests, groups them together and filters them based on your filters. Then in the Run phase it will actually run them.

Splitting the work into two distinct phases, powers many of the features in this release, and enables many others to be implemented in the future.

For Discovery to work correctly, there are new rules to follow:

Put all your code into It, BeforeAll, BeforeEach, AfterAll or AfterEach. Put no code directly into Describe, Context or on the top of your file, without wrapping it in one of these blocks, unless you have a good reason to do so.

All misplaced code will run during Discovery, and its results won’t be available during Run.

This article offers more guidance and shows examples of what it means.