Skip to content

Allow InModuleScope to not require the module to be loaded during discovery #1885

@JustinGrote

Description

@JustinGrote

General summary of the issue

There are times when you only want the discovery phase (for instance, Tyler's Pester Text Explorer Extension) to determine the test structure. However, currently if you want to use InModuleScope, the module must be loaded during discovery, producing extra overhead especially in repeated test discovery scenarios. I don't see a strict reason this is necessary, and the check for module should be done later.

Describe your environment

Pester version     : 5.1.1 C:\Users\JGrote\OneDrive - Allied Digital Services LLC\Documents\PowerShell\Modules\Pester\5.1.1\Pester.psm1
PowerShell version : 7.2.0-preview.4
OS version         : Microsoft Windows NT 10.0.19043.0

Steps to reproduce

    $mymodulename = Get-MyDynamicModuleName
    InModuleScope $mymoduleName {
      ...
    }

Expected Behavior

Module would not be tested if it is loaded until the actual test phase

Current Behavior

Pester fails at the discovery phase

ystem.Management.Automation.RuntimeException: No modules named 'mymagicmodule' are currently loaded. 
at Get-ScriptModule, C:\Users\JGrote\OneDrive - Allied Digital Services LLC\Documents\PowerShell\Modules\Pester\5.1.1\Pester.psm1: line 8964
at InModuleScope, C:\Users\JGrote\OneDrive - Allied Digital Services LLC\Documents\PowerShell\Modules\Pester\5.1.1\Pester.psm1: line 8927at <ScriptBlock>, C:\Users\JGrote\Projects\Press\Source\Public\Get-Version.Tests.ps1: line 2
at <ScriptBlock>, C:\Users\JGrote\OneDrive - Allied Digital Services LLC\Documents\PowerShell\Modules\Pester\5.1.1\Pester.psm1: line 2903at Invoke-File, C:\Users\JGrote\OneDrive - Allied Digital Services LLC\Documents\PowerShell\Modules\Pester\5.1.1\Pester.psm1: line 2912  
at Invoke-BlockContainer, C:\Users\JGrote\OneDrive - Allied Digital Services LLC\Documents\PowerShell\Modules\Pester\5.1.1\Pester.psm1: line 2837
at Discover-Test, C:\Users\JGrote\OneDrive - Allied Digital Services LLC\Documents\PowerShell\Modules\Pester\5.1.1\Pester.psm1: line 1411at Invoke-Test, C:\Users\JGrote\OneDrive - Allied Digital Services LLC\Documents\PowerShell\Modules\Pester\5.1.1\Pester.psm1: line 2356  
at Invoke-Pester<End>, C:\Users\JGrote\OneDrive - Allied Digital Services LLC\Documents\PowerShell\Modules\Pester\5.1.1\Pester.psm1: line 4839
at <ScriptBlock>, <No file>: line 1

Possible Solution? (optional)

Don't check the module is loaded until testing phase.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions