Errors while running Invoke-Pester (BeforeAll)

In the PowerShell training module I’m getting the following error when running “Invoke-Pester”.


    Directory: C:\Users\username\Exercism\powershell\hello-world


Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
d-----        12/14/2023   4:36 PM                .exercism
-a----        12/14/2023   4:56 PM            204 HelloWorld.ps1
-a----        12/14/2023   5:22 PM            147 HelloWorld.tests.ps1
-a----        12/14/2023   4:36 PM           1252 HELP.md
-a----        12/14/2023   4:36 PM            976 README.md


> hello-world > Invoke-Pester





 [-] Error occurred in test script 'C:\Users\username\Exercism\powershell\hello-world\HelloWorld.tests.ps1' 183ms
   RuntimeException: The BeforeAll command may only be used inside a Describe block.
   at Assert-DescribeInProgress, C:\Program Files\WindowsPowerShell\Modules\Pester\3.4.0\Functions\Describe.ps1: line 125
   at BeforeAll, C:\Program Files\WindowsPowerShell\Modules\Pester\3.4.0\Functions\SetupTeardown.ps1: line 56
   at <ScriptBlock>, C:\Users\username\Exercism\powershell\hello-world\HelloWorld.tests.ps1: line 1
   at <ScriptBlock>, C:\Program Files\WindowsPowerShell\Modules\Pester\3.4.0\Pester.psm1: line 297
   at Invoke-Pester, C:\Program Files\WindowsPowerShell\Modules\Pester\3.4.0\Pester.psm1: line 310
   at <ScriptBlock>, <No file>: line 1
Tests completed in 183ms
Passed: 0 Failed: 1 Skipped: 0 Pending: 0 Inconclusive: 0


> hello-world > $PSVersionTable

Name                           Value
----                           -----
PSVersion                      5.1.22621.2506
PSEdition                      Desktop
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
BuildVersion                   10.0.22621.2506
CLRVersion                     4.0.30319.42000
WSManStackVersion              3.0
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1

This is on Windows 11 and I am running the 3.2 version of the cli.

PSVersion                      5.1.22621.2506

I think this is just a matter of outdated tooling.

If possible could you run

Get-Module -Name Pester -ListAvailable

as well.
For reference currently the platform is running: PowerShell 7.3.9 and Pester 5.5.0

Interesting. I just attempted to upgrade PowerShell to the most recent version a few days ago. It’s been over 5 years since running Windows so I’m kind of rusty, sorry.

> hello-world >Get-Module -Name Pester -ListAvailable


    Directory: C:\Program Files\WindowsPowerShell\Modules


ModuleType Version    Name                                ExportedCommands
---------- -------    ----                                ----------------
Script     3.4.0      Pester                              {Describe, Context, It, Should...}

No problem.
I think I can see what’s going on here based on your previous post.
You are running the shipped powershell version with windows, hence the PSEdition Desktop.

That one is no longer being supported and I think they gonna let it stay there at version 5+. It also included pester, but that one also stuck at 3.4.0.
The modern and current one that getting update is cross platform and called PowerShellCore.

This is my info when i call $PSVersionTable

Name                           Value
----                           -----
PSVersion                      7.4.0
PSEdition                      Core
GitCommitId                    7.4.0
OS                             Microsoft Windows 10.0.22000
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

It is a bit annoying, but you can just go to their github page
PS download and download the installer
or even better using winget (kinda like apt for linux), winget is auto included since win 10
winget

Afterward you can just launch the new powershell terminal, run:

Install-Module -Name Pester

to get the latest, then

Import-Module -Name Pester -Version 5.5.0

to start using this specific version.

Yep, your initial post sent me on my way to figuring this out, thanks.

Apt is specifically for Debian (and Debian derivatives), sorry for being pedantic as everyone would know what you mean. I’m really happy that there is command line package management for Windows and a shell I can work in to get my job done.

Thanks again for pointing me in the right direction.

Nevermind. I had too many terminals open and had not imported the pester module in the current window.

New error but may be related. If it’s a problem with my code, just say it’s expected. I thought the function is necessary for Pester to test the code.

> hello-world > Invoke-Pester

Starting discovery in 1 files.
Discovery found 1 tests in 101ms.
Running tests.
[-] HelloWorldTest.Outputs: 'Hello, World!' 61ms (47ms|14ms)
 CommandNotFoundException: The term 'Get-HelloWorld' is not recognized as a name of a cmdlet, function, script file, or executable program.
 Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
 at <ScriptBlock>, C:\Users\jdowning\Exercism\powershell\hello-world\HelloWorld.tests.ps1:7
Tests completed in 403ms
Tests Passed: 0, Failed: 1, Skipped: 0 NotRun: 0

HelloWorld.ps1

Function Get-HelloWorld {
    <#
    .SYNOPSIS
    Outputs "Hello, World!"
    
    .DESCRIPTION
    Output "Hello, World!".
    
    .EXAMPLE
    Get-HelloWorld
    #>	
    
    Return "Hello, World!"
}

HelloWorld.tests.ps1

BeforeAll {
	".\HelloWorld.ps1"
}

Describe "HelloWorldTest" {
	It "Outputs: 'Hello, World!'" {
		Get-HelloWorld | Should -Be 'Hello, World!'
	}
}