@@ -45,7 +45,7 @@ BeforeAll {
4545Describe ' PSModule - Module tests' {
4646 Context ' Module' {
4747 It ' The module should be importable' {
48- { Import-Module - Name $moduleName - Force } | Should -Not - Throw
48+ { Import-Module - Name $moduleManifestPath - Force } | Should -Not - Throw
4949 }
5050 }
5151
@@ -63,16 +63,22 @@ Describe 'PSModule - Module tests' {
6363 }
6464
6565 Context ' Framework - IsWindows compatibility shim' {
66+ BeforeAll {
67+ $script :moduleRef = Import-Module - Name $moduleManifestPath - Force - PassThru
68+ }
6669 It ' Should have $IsWindows defined in the module scope' {
6770 # The framework injects "$IsWindows = $true" for PowerShell 5.1 (Desktop edition).
6871 # On PS 7+ (Core), $IsWindows is a built-in automatic variable.
6972 # The variable is set inside the module scope and is not exported, so we must check from within the module.
70- $isWindowsDefined = & ( Get-Module $moduleName ) { Get-Variable - Name ' IsWindows' - ErrorAction SilentlyContinue }
73+ $isWindowsDefined = & $ script :moduleRef { Get-Variable - Name ' IsWindows' - ErrorAction SilentlyContinue }
7174 $isWindowsDefined | Should -Not - BeNullOrEmpty - Because ' the framework injects a compatibility shim for PS 5.1'
7275 }
7376 }
7477
7578 Context ' Framework - Type accelerator registration' - Skip:(-not $hasClassExporter ) {
79+ BeforeAll {
80+ Import-Module - Name $moduleManifestPath - Force
81+ }
7682 It ' Should register public enum [<_>] as a type accelerator' - ForEach $expectedEnumNames {
7783 $registered = [psobject ].Assembly.GetType(' System.Management.Automation.TypeAccelerators' )::Get
7884 $registered.Keys | Should - Contain $_ - Because ' the framework registers public enums as type accelerators'
@@ -85,22 +91,27 @@ Describe 'PSModule - Module tests' {
8591 }
8692
8793 Context ' Framework - Module OnRemove cleanup' - Skip:(-not $hasClassExporter ) {
94+ BeforeAll {
95+ Import-Module - Name $moduleManifestPath - Force
96+ }
8897 It ' Should clean up type accelerators when the module is removed' {
8998 # Capture type names before removal
9099 $typeNames = @ (@ ($expectedEnumNames ) + @ ($expectedClassNames ) | Where-Object { $_ })
91100 $typeNames | Should -Not - BeNullOrEmpty - Because ' there should be types to verify cleanup for'
92101
93- # Remove the module to trigger the OnRemove hook
94- Remove-Module - Name $moduleName - Force
102+ try {
103+ # Remove the module to trigger the OnRemove hook
104+ Remove-Module - Name $moduleName - Force
95105
96- # Verify type accelerators are cleaned up
97- $typeAccelerators = [psobject ].Assembly.GetType(' System.Management.Automation.TypeAccelerators' )::Get
98- foreach ($typeName in $typeNames ) {
99- $typeAccelerators.Keys | Should -Not - Contain $typeName - Because " the OnRemove hook should remove type accelerator [$typeName ]"
106+ # Verify type accelerators are cleaned up
107+ $typeAccelerators = [psobject ].Assembly.GetType(' System.Management.Automation.TypeAccelerators' )::Get
108+ foreach ($typeName in $typeNames ) {
109+ $typeAccelerators.Keys | Should -Not - Contain $typeName - Because " the OnRemove hook should remove type accelerator [$typeName ]"
110+ }
111+ } finally {
112+ # Re-import the module for any subsequent tests
113+ Import-Module - Name $moduleManifestPath - Force
100114 }
101-
102- # Re-import the module for any subsequent tests
103- Import-Module - Name $moduleName - Force
104115 }
105116 }
106117}
0 commit comments