Search code examples
powershellpester

Pester with BeforeAll gives "Cannot process argument transformation on parameter 'Scriptblock'"


I have added a new unit test file for Pester 5 to my Powershell repo in VS code.

When I try to run/debug it, I get a prompt in Terminal:

cmdlet BeforeAll at command pipeline position 1
Supply values for the following parameters:
Scriptblock:

If I enter a random string "hgj", I get the error:

[-] Discovery in C:\XXX.Tests.ps1 failed with:
System.Management.Automation.ParameterBindingArgumentTransformationException: Cannot process argument transformation on parameter 'Scriptblock'. Cannot convert the "hgj" value of type "System.String" to type "System.Management.Automation.ScriptBlock". ---> System.Management.Automation.ArgumentTransformationMetadataException: Cannot convert the "hgj" value of type "System.String" to type "System.Management.Automation.ScriptBlock". ---> System.Management.Automation.PSInvalidCastException: Cannot convert the "hgj" value of type "System.String" to type "System.Management.Automation.ScriptBlock".

This is the unit test file:

BeforeAll 
{
}

Describe 'Start-Package564' 
{
    It 'Runs Start-Package' 
    {
        
    }
}

If I comment out Before-All, everything works.

If I add some code in Before-All, I still get the prompt and the error.

Why does Before-All cause the Terminal to prompt for a scriptblock?


Solution

  • BeforeAll is a command, not a keyword (same for Describe and It).

    For this reason, you need to supply the scriptblock literal on the same line so that PowerShell knows you meant to pass it as a parameter argument to BeforeAll - otherwise it just sees two separate statements: invocation of BeforeAll without any arguments, followed by a scriptblock literal.

    To fix:

    BeforeAll {
    }
    
    Describe 'Start-Package564' {
        It 'Runs Start-Package' {
            
        }
    }