-
Hi there, I have a question regarding testing a script module with public (exported) and private (not exported) functions. I have a script module with quite some functions which are saved each in a separate .ps1 file. The files are saved either in the private or the public directory. So I was now searching for a way to also unit test the internal/private functions of the module. It was some time since I used Pester the last time. Think it was version 4 or even 3 back then. So I am aware of Currently I just dot source directly the .ps1 file with the function which I want to unit test, in a So what would be the recommended way to unit test also the private functions which are not exported by the module? Because the module is quite complex I thought also already to convert it to a binary module (at least partially). But as Microsoft recommends Pester also to test binary modules the question stays. Thanks! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 6 replies
-
One case where you should avoid If you are testing an internal function, then using InModuleScope is your only option. In that case the best way is to put InModuleScope into each It block. Instead of around the whole Describe. You can also quite safely put InModuleScope around the whole Describe, but in that case make sure to only import your module once. You should avoid Import-Module -Force on top of your every file. Or Remove-Module; Import-Module. This will leave you with tests that are bound to a module that is no longer imported and you will have problems that are very hard to debug. |
Beta Was this translation helpful? Give feedback.
InModuleScope
is a bit problematic in Pester 5. But it can still be used if you really need it and understand the limitations.One case where you should avoid
InModuleScope
is when you are testing a public function, that calls an internal function, and you need to mock that internal function. In that caseMock Some-InternalFunction -ModuleName <your module>
, should be used instead, because then only the Mock is inserted into the module, but your test code is invoked from the outside of the module. This allows you to inject mocks, without exposing all the module internals to the test = you are sure you publish only what you meant to publish.If you are testing an internal function, then us…