How to use PowerShell multithreading and still unit test with Pester Mocks How to use PowerShell multithreading and still unit test with Pester Mocks powershell powershell

How to use PowerShell multithreading and still unit test with Pester Mocks


I don't consider this a full answer, and I don't work on the Pester project, but I would say that this is simply not a supported scenario for Pester. This might change when/if concurrent programming becomes part of PowerShell proper (or it may not).

If you're willing to change your implementation you might be able to write around this limitation to support some sort of testing.

For example, maybe your function doesn't use an RSJob when it only has 1 thing to do (which conveniently might be the case when testing).

Or maybe you implement a -Serial or -NoParallel or -SingleRunspace switch (or a -ConcurrencyFactor which you set to 1 in tests), wherein you don't use a runspace for those conditions.

Based on your example it's difficult to tell if that kind of test adequately tests what you want, but it seems like it does.