Skip to content

Commit f62b2ac

Browse files
fix: move discovery variables out of BeforeAll for Pester 5 compatibility
Pester v5 evaluates -Skip and -ForEach during the Discovery phase, but BeforeAll only runs during the Run phase. This caused hasClassExporter, expectedClassNames, and expectedEnumNames to be null during discovery, skipping all framework type-accelerator tests. Move the module content reading and regex extraction to the top-level script scope so the variables are available when Pester discovers tests.
1 parent b038dc1 commit f62b2ac

File tree

1 file changed

+23
-25
lines changed

1 file changed

+23
-25
lines changed

src/tests/Module/PSModule/PSModule.Tests.ps1

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -12,35 +12,33 @@ param(
1212
[string] $Path
1313
)
1414

15-
BeforeAll {
16-
$moduleName = Split-Path -Path (Split-Path -Path $Path -Parent) -Leaf
17-
$moduleManifestPath = Join-Path -Path $Path -ChildPath "$moduleName.psd1"
18-
$moduleRootPath = Join-Path -Path $Path -ChildPath "$moduleName.psm1"
19-
Write-Verbose "Module Manifest Path: [$moduleManifestPath]"
20-
Write-Verbose "Module Root Path: [$moduleRootPath]"
15+
# Discovery-phase variables — must be set at script scope (outside BeforeAll) so that
16+
# Pester's -Skip and -ForEach parameters can reference them during test discovery.
17+
$moduleName = Split-Path -Path (Split-Path -Path $Path -Parent) -Leaf
18+
$moduleManifestPath = Join-Path -Path $Path -ChildPath "$moduleName.psd1"
19+
$moduleRootPath = Join-Path -Path $Path -ChildPath "$moduleName.psm1"
2120

22-
# Discover public classes and enums from the compiled module source.
23-
# The class exporter region is injected by Build-PSModule when classes/public contains types.
24-
$moduleContent = if (Test-Path -Path $moduleRootPath) { Get-Content -Path $moduleRootPath -Raw } else { '' }
25-
$hasClassExporter = $moduleContent -match '#region\s+Class exporter'
21+
# Discover public classes and enums from the compiled module source.
22+
# The class exporter region is injected by Build-PSModule when classes/public contains types.
23+
$moduleContent = if (Test-Path -Path $moduleRootPath) { Get-Content -Path $moduleRootPath -Raw } else { '' }
24+
$hasClassExporter = $moduleContent -match '#region\s+Class exporter'
2625

27-
# Extract expected class and enum names from the class exporter block.
28-
$expectedClassNames = @()
29-
$expectedEnumNames = @()
30-
if ($hasClassExporter) {
31-
# Match $ExportableClasses = @( ... ) block
32-
if ($moduleContent -match '\$ExportableClasses\s*=\s*@\(([\s\S]*?)\)') {
33-
$expectedClassNames = [regex]::Matches($Matches[1], '\[([^\]]+)\]') | ForEach-Object { $_.Groups[1].Value }
34-
}
35-
# Match $ExportableEnums = @( ... ) block
36-
if ($moduleContent -match '\$ExportableEnums\s*=\s*@\(([\s\S]*?)\)') {
37-
$expectedEnumNames = [regex]::Matches($Matches[1], '\[([^\]]+)\]') | ForEach-Object { $_.Groups[1].Value }
38-
}
26+
# Extract expected class and enum names from the class exporter block.
27+
$expectedClassNames = @()
28+
$expectedEnumNames = @()
29+
if ($hasClassExporter) {
30+
# Match $ExportableClasses = @( ... ) block
31+
if ($moduleContent -match '\$ExportableClasses\s*=\s*@\(([\s\S]*?)\)') {
32+
$expectedClassNames = @([regex]::Matches($Matches[1], '\[([^\]]+)\]') | ForEach-Object { $_.Groups[1].Value })
33+
}
34+
# Match $ExportableEnums = @( ... ) block
35+
if ($moduleContent -match '\$ExportableEnums\s*=\s*@\(([\s\S]*?)\)') {
36+
$expectedEnumNames = @([regex]::Matches($Matches[1], '\[([^\]]+)\]') | ForEach-Object { $_.Groups[1].Value })
3937
}
40-
Write-Host "Has class exporter: $hasClassExporter"
41-
Write-Host "Expected classes: $($expectedClassNames -join ', ')"
42-
Write-Host "Expected enums: $($expectedEnumNames -join ', ')"
4338
}
39+
Write-Host "Has class exporter: $hasClassExporter"
40+
Write-Host "Expected classes: $($expectedClassNames -join ', ')"
41+
Write-Host "Expected enums: $($expectedEnumNames -join ', ')"
4442

4543
Describe 'PSModule - Module tests' {
4644
Context 'Module' {

0 commit comments

Comments
 (0)