V5: Can't not load functions in the memory
1. General summary of the issue
I'm new to pester. but I wrote some tests with v4 without issue. Now, with this version 5, I have difficulty to execute in the terminal window of VSCode. It seems it should load original functions in the memory, but it doesn't.
2. Describe Your Environment
Operating System, Pester version, and PowerShell version:
Pester version : 5.0.1 C:\Program Files\WindowsPowerShell\Modules\Pester\5.0.1\Pester.psd1
PowerShell version : 5.1.17134.858
OS version : Microsoft Windows NT 10.0.17134.0
3. Expected Behavior
With vscode, if I execute the script in memory by press F5, then it will load in the memory. You can see it from Get-ChildItem function:\ and then invoke-pester will run. otherwise, it encounter following error.
`PS C:\otherfolder\Powershell> Invoke-Pester .\temp.tests.ps1
Starting discovery in 1 files. Discovery finished in 317ms. [-] Test Get-TimesResult all functions.Test Get-TimesResult.Get-TimesResult test 665ms (658ms|6ms) CommandNotFoundException: The term 'Get-TimesResult' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.`
4.Current Behavior
Scripts: ` BeforeAll {
. $PSCommandPath.Replace('.Tests.ps1','.ps1') $Skip = $true }
Describe "Test Get-TimesResult all functions" { Context 'Test Get-TimesResult' {
#write-host "answer is $jsonResult"
it "Get-TimesResult test" {
$result = Get-TimesResult
$result | should -be "working?"
}
}
}`
5. Possible Solution
sorry, all what I'm saying it works in 4.10.1
6. Context
just a simple pester loaded from PowerShell console.
I don't see anything wrong with the above code. Could you put breakpoint on the $Skip = $true line, and look if the function is imported there by using Get-Command Get-TimesResult ?
Thank you for responding. I ran both in the terminal and with in VSCODE (press F5). They all got same result. `Invoke-Pester .\temp.tests.ps1
Starting discovery in 1 files. Discovery finished in 2.81s. Get-Command : The term 'Get-timeResult' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. At C:\xxx\Powershell\temp.tests.ps1:5 char:4
- Get-Command Get-timeResult
-
+ CategoryInfo : ObjectNotFound: (Get-timeResult:String) [Get-Command], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException,Microsoft.PowerShell.Commands.GetCommandCommand
[-] Test Get-TimesResult all functions.Test Get-TimesResult.Get-TimesResult test 2.11s (1.79s|319ms)
CommandNotFoundException: The term 'Get-TimesResult' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
at
Here is the function
`function set-Testvalue { write-host "I don't do anything" }
function Get-TimesResult {
Param ([int]$a,[int]$b)
$c = $a * $b
Write-Output $c } `
Remove 5.0.1 and go back to 4.10.1
`PS C:\xxx\Powershell> Invoke-Pester .\temp.tests.ps1
Pester v4.10.1 Executing all tests in '.\temp.tests.ps1'
Executing script .\temp.tests.ps1
Describing Test Get-TimesResult all functions
Context Test Get-TimesResult
[-] Get-TimesResult test 1.69s
Expected 'working?', but got 0.
18: $result | should -be "working?"
at <ScriptBlock>, C:\xxx\Powershell\temp.tests.ps1: line 18
Tests completed in 8.36s Tests Passed: 0, Failed: 1, Skipped: 0, Pending: 0, Inconclusive: 0 `
` <# BeforeAll { DON'T use $MyInvocation.MyCommand.Path . $PSCommandPath.Replace('.Tests.ps1','.ps1') Get-Command Get-timeResult $Skip = $true } #> $here = Split-Path -Parent $MyInvocation.MyCommand.Path $sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path) -replace '.Tests.', '.' . "$here$sut"
Describe "Test Get-TimesResult all functions" { Context 'Test Get-TimesResult' {
#write-host "answer is $jsonResult"
it "Get-TimesResult test" {
$result = Get-TimesResult
$result | should -be "working?"
}
}
}`
I also faced issues with . $PSCommandPath.Replace('.Tests.ps1', '.ps1'). I noticed that the replace function is actually case-sensitive.
This resolves my issue. thanks, @meCoding