Pester icon indicating copy to clipboard operation
Pester copied to clipboard

Mocked functions aren't properly identified

Open iRon7 opened this issue 3 months ago • 3 comments

Checklist

What is the issue?

use case

The Build-Module I create allows for importing the current stable module and test for a any new prototype cmdlet (see: Cmdlet Prototype Pester testing) by mocking the prototype on top of the actually module.

This works fine, except that it doesn't mention the mocked function in the pester messages. Meaning that I get messages like:

[-] Test-Object.References.[V] Buyer 53ms (49ms|4ms)
 PropertyNotFoundException: The property 'CaseMatters' cannot be found on this object. Verify that the property exists.
 at GetReference, <No file>:300
 at TestNode, <No file>:591
 at QueryChildNodes, <No file>:789
 at TestNode, <No file>:725
 at <ScriptBlock><Process>, <No file>:813

Expected Behavior

For e.g. the Steps To Reproduce below, I would expect a more descriptive location like at <Get-Date>, .\Mock.ps1:4 or at <mocked Get-Date>, .\Mock.ps1:4 rather than a general <ScriptBlock>, .\Mock.ps1:4 identification. And possibly a beter definition for <No file> and <Process (as shown above).

Steps To Reproduce

Describe 'Test' {

    BeforeAll {
        Mock Get-Date { 1 / 0 }
    }

    Context 'Context' {
        It 'It' {
            Get-Date | Should -be Something
        }
    }
}

Produces:

Starting discovery in 1 files.
Discovery found 1 tests in 42ms.
Running tests.
[-] Test.Context.It 123ms (123ms|1ms)
 DivideByZeroException: Attempted to divide by zero.
 RuntimeException: Attempted to divide by zero.
 at <ScriptBlock>, .\Mock.ps1:4
Tests completed in 291ms
Tests Passed: 0, Failed: 1, Skipped: 0 NotRun: 0

Describe your environment

Pester version : 5.5.0 C:\Users\Gebruiker\Documents\PowerShell\Modules\Pester\5.5.0\Pester.psm1 PowerShell version : 7.5.3 OS version : Microsoft Windows NT 10.0.26100.0

Possible Solution?

No response

iRon7 avatar Oct 08 '25 09:10 iRon7

This is probably result of what you do, saving the code into a file will recognize the code as coming from a file correctly.

Image

However if I run it as scriptblock container (not saved to a file) I will get a less verbose message:

Image

In either case I don't see the result you are seeing. But in the second case where we run from script block I see the same output as you show in your original problem:

 at GetReference, <No file>:300
 at TestNode, <No file>:591
 at QueryChildNodes, <No file>:789
 at TestNode, <No file>:725
 at <ScriptBlock><Process>, <No file>:813

I cannot look too deep into your code, but I see that you do: Mock $CommandName ([ScriptBlock]::Create($Content)), this will read the content of the file and make a script block that is not tied to the original file. This is how we did it in Pester 4, but that prevented mock bodies from being debugged. We don't do that anymore, and instead we use the the scriptblock directly as we receive it from the mock function.

In your case you probably want to do something like { . $Content } instead of creating a new scriptblock from the text. This will make a scriptblock bound to your module though, so it is probably better to also get session state from the PSCmdlet and make sure the scriptblock is bound to the caller session state, if that makes sense for your usecase.

nohwnd avatar Oct 08 '25 14:10 nohwnd

@nohwnd,

Just with the minimum reproducible example:

Image

It states <ScriptBlock> where it could probably state something like Get-Date...

iRon7 avatar Oct 08 '25 14:10 iRon7

Okay I don't think this is within our control, best we could do it take the standard stack trace, and rewrite it when error falls within the mock. But we also cannot recognize that always. Because it did not fail in Get-Date, it did really fail in the scriptblock that is executed.

Right now I cannot think about a way to trick PowerShell into doing this, but maybe they could add a descriptor to the ScriptBlock that would be read by the stack trace writer to allow us customizing this.

nohwnd avatar Oct 08 '25 14:10 nohwnd