0

I'm trying to run Pester to test PowerShell scripts, but I can't get VSCode to execute the tests.

My code

Implementation

I have the following code on a file ConvertTo-Nanosecond.ps1:

function ConvertTo-Nanosecond {
  [CmdletBinding()]
  param (
    [Parameter(Mandatory)]
    [TimeSpan]
    $TimeSpan
  )
}

Tests

And on the same directory I have a file ConvertTo-Nanosecond.Tests.ps1:

Describe 'ConvertTo-Nanosecond' {
  It 'Converts a time span to nanoseconds.' {
    ConvertTo-Nanosecond -TimeSpan (New-TimeSpan -Minutes 5) | Should Be 300000000000
  }
}

Error

Guiding myself with the documentation of Pester it should work when clicking the Run tests, but I get a CommandNotFoundException, specifically I get the following output:

PS ~> . "c:\...\.vscode-insiders\extensions\ms-vscode.powershell-preview-2022.10.0\modules\PowerShellEditorServices\InvokePesterStub.ps1" -ScriptPath 'Path\To\Script\ConvertTo-Nanosecond.Tests.ps1' -LineNumber 3 -Output 'FromPreference'


Starting discovery in 1 files.
Discovery found 4 tests in 169ms.
Running tests.
[-] ConvertTo-Nanosecond.Converts a time span to nanoseconds. 74ms (73ms|1ms)
 CommandNotFoundException: The term 'ConvertTo-Nanosecond' 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>, Path\To\Script\ConvertTo-Nanosecond.Tests.ps1:5
[-] ConvertTo-Nanosecond.Converts a time span to nanoseconds as a time span. 67ms (66ms|1ms)
 CommandNotFoundException: The term 'ConvertTo-Nanosecond' 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>, Path\To\Script\ConvertTo-Nanosecond.Tests.ps1:8
[-] ConvertTo-Nanosecond.Converts a time span to nanoseconds as a double. 82ms (81ms|1ms)
 CommandNotFoundException: The term 'ConvertTo-Nanosecond' 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>, Path\To\Script\ConvertTo-Nanosecond.Tests.ps1:12
[-] ConvertTo-Nanosecond.Converts a time span to nanoseconds as an integer. 69ms (68ms|1ms)
 CommandNotFoundException: The term 'ConvertTo-Nanosecond' 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>, Path\To\Script\ConvertTo-Nanosecond.Tests.ps1:15

What I've Tried

I tried adding:

. ".\ConvertTo-Nanosecond.ps1"
. ".\ConvertTo-Nanosecond"
& ".\ConvertTo-Nanosecond.ps1"
Import-Module "ConvertTo-Nanosecond.ps1"

but all those outputs an error.

If I run Pester from the terminal (Invoke-Pester) I also get the CommandNotFoundException.

The only way I can get it to work is by running the file as a script:

powershell -ExecutionPolicy ByPass -File "Path\To\Script\ConvertTo-Nanosecond.Tests.ps1"

but that does not show the color highlight.

Any idea what could be happening?

Roäc
  • 149
  • 19
  • 4
    You need to import the functions in a BeforeAll { ... } code block. https://pester.dev/docs/usage/importing-tested-functions – Daniel Oct 09 '22 at 20:28

0 Answers0