Skip to content

InModuleScope

Jeff Crosby edited this page Jun 29, 2019 · 2 revisions

The InModuleScope command allows you to perform white-box unit testing on the internal (non-exported) code of a Script Module.

Let's say you have code like this inside a script module named MyModule.psm1:

functionPublicFunction
{
# Does something
}
functionPrivateFunction
{
return$true
}
Export-ModuleMember-Function PublicFunction

Normally, you cannot call the PrivateFunction command after importing the module; only PublicFunction would be exposed to the rest of the PowerShell session. For example, this test would fail with an error of "The term 'PrivateFunction' is not recognized as the name of a cmdlet, function, script file, or operable program.":

Import-Module MyModule
Describe 'Testing MyModule' {
It 'Tests the Private function' {
PrivateFunction | Should Be $true
}
}

By using InModuleScope, you can execute test code inside the module, giving you access to its internal functions, variables, and aliases. For example:

Import-Module MyModule
InModuleScope MyModule {
Describe 'Testing MyModule' {
It 'Tests the Private function' {
PrivateFunction | Should Be $true
}
}
}

You may place an InModuleScope command anywhere inside a Pester test script. It can contain entire Describe blocks, as shown, or be limited to smaller groups of commands (including just the body of the It block).

Clone this wiki locally