Given a PowerShell script (.ps1) with functions and nested functions. The function 'Inner' should not be moved to the outer scope and thus not be exported. How can one define an 'It' test for the 'Inner' function (hopefully without modifying the code)?
Using: PS core 7.1.4:
Function Outer {
Function Inner {
# ...
}
# ...
}
Using: Pester 5.3.0:
Describe "A" {
It "A" { Outer } | Should -be $null # OK
It "B" { Inner } | Should -be $null # ERROR
}
If you really must do this, there is a way but it involves searching through the abstract syntax tree. This might be a bit complex, but it does the job.
So you have the functions defined as such:
You can find the
scriptblock
of theInner
function as follows:Now, in the
$innerScriptBlock
variable you have the definition of theInner
function. You can invoke it using the call operator or simply$innerScriptBlock.Invoke()
It will be a little trickier to pass parameters and to test it.