Import-Module Automatically Runs Pester

699 views
Skip to first unread message

Dave Wiard

unread,
Oct 12, 2016, 5:59:42 PM10/12/16
to Pester
I'm in the process of converting a lot of our PowerShell code to a module. I'm adding Pester tests for both the module itself and the individual functions. I'm trying to understand the behavior I'm seeing. The module is nothing really special. It's a basic .psd1 and .psm1 with a handful of functions. The structure is this:

ModuleA\
   
ModuleA.psd1
   
ModuleA.psm1
    ModuleA.Tests.ps1
   
Functions\
       
FunctionA.ps1
       
FunctionB.ps1
   
Tests\
       
FunctionA.Tests.ps1
       
FunctionB.Tests.ps1

Every time I import the module I see Pester tests being invoked automatically.

The FunctionA.Tests.ps1 has a dependency on FunctionB so it must import the module before it can run the tests. This ends up, somehow, leading to a nesting effect where ModuleA tests are run, then run again, then again, until I start seeing this error:

New-Module : Cannot load the module 'Pester' because the module nesting limit has been exceeded. Modules can only be nested to 10
levels
. Evaluate and change the order in which you are loading modules to prevent exceeding the nesting limit, and then try
running your script again
.
At C:\Program Files\WindowsPowerShell\Modules\Pester\3.4.3\Functions\PesterState.ps1:31 char:5
+     & $SafeCommands['New-Module'] -Name Pester -AsCustomObject -Scrip ...
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   
+ CategoryInfo          : InvalidOperation: (Pester:String) [New-Module], InvalidOperationException
   
+ FullyQualifiedErrorId : Modules_ModuleTooDeeplyNested,Microsoft.PowerShell.Commands.NewModuleCommand


Nowhere in any of our files are we explicitly importing Pester. Due to the nesting effect it has become impossible to test FunctionA because it can never successfully import ModuleA. I have two questions.

  1. Why are Pester tests being run automatically when importing our module?
  2. How can I disable running Pester automatically upon import?

Chris Dent

unread,
Oct 13, 2016, 2:20:02 AM10/13/16
to Dave Wiard, Pester

Since you separate functions, how are those loaded?


--
You received this message because you are subscribed to the Google Groups "Pester" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pester+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Chris Dent

unread,
Oct 13, 2016, 3:58:07 AM10/13/16
to Dave Wiard, Pester
Now I'm sat at a desk...

Import-Module does not automatically run pester.

If you find tests are executing they're being called somehow. Places to check:

  • ModuleA.psm1 file to see how the separated functions are being called. For example, if it's as simple as "Get-ChildItem $psscriptroot -Filter *.ps1 -Recurse | ForEach-Object { . $_.FullName }" you will attempt to execute the pester tests.
  • Any "#requires -Module pester" statements in invoked files.
  • ModuleA.psd1 if it, some some reason, lists Pester as a required module.
You say FunctionA.tests.ps1 has a dependency on FunctionB (meant to be A?). How is that implemented? Your problem is best explained by invocation of the "tests" files on module load if those also either #require or explicitly import Pester itself (after requiring the source module).

Chris

Dave Wiard

unread,
Oct 13, 2016, 9:54:37 AM10/13/16
to Pester
Thanks, Chris. This was indeed exactly what was triggering the tests to run on import. I added a -Exclude to the Get-ChildItem to filter all '*.Tests.ps1' files.

Dave
To unsubscribe from this group and stop receiving emails from it, send an email to pester+un...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages