Weird behaviour - mock hangs when being called twice

Hi all

I’m assigned to write a bunch of tests on an existing codebase and a number of my tests hang.

The code, simplified to show the issue, is as follows:

File MySharepointModule.ps1 :

function Update-SharepointList {
  # do some stuff
  Set-PnPListItem -someparams
}

File MyScript.ps1 :

Import-Module -Name  '.\Modules\MySharepointModule.ps1'
# do lots of stuff
Set-PnPListItem -someparams

# do lots of stuff
Update-SharepointList

In order to avoid setting up a Sharepoint site, I’m mocking Set-PnPListItem.

Mock Set-PnPListItem { 
    # store args into a variable
}

It works for the first call. But on the second call, where it’s being called from the module, Set-PnPListItem hangs. It doesn’t even throw an error -just hangs. And if I need to run the tests again, I have to kill the terminal or restart VSCode.

I’ve tried different variations, even leaving the mock empty. Nothing works, it always hangs. The only way I can get it to work is if I mock Update-SharepointList itself, but that means leaving this code untested.

Is this a known issue, behaviour or bug? Are there any workarounds?

Many thanks in advance

Jim

I asked the same question in Github and got an answer, so I’m adding it here:

Link to answer:

1 Like