Skip to content

Commit 4204472

Browse files
nohwndCopilot
andcommitted
Fix #2538: Container/block with discovery errors reported as Failed
Copilot-generated fix. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent d5f7c87 commit 4204472

File tree

2 files changed

+50
-3
lines changed

2 files changed

+50
-3
lines changed

src/Pester.RSpec.ps1

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,9 @@ function PostProcess-RspecTestRun ($TestRun) {
176176
$b.Result = if ($b.Skip) {
177177
"Skipped"
178178
}
179+
elseif (0 -lt $b.ErrorRecord.Count) {
180+
"Failed"
181+
}
179182
elseif ($b.Passed) {
180183
"Passed"
181184
}
@@ -206,12 +209,12 @@ function PostProcess-RspecTestRun ($TestRun) {
206209
$b.result = if ($b.Skip) {
207210
"Skipped"
208211
}
209-
elseif ($b.Passed) {
210-
"Passed"
211-
}
212212
elseif (0 -lt $b.ErrorRecord.Count) {
213213
"Failed"
214214
}
215+
elseif ($b.Passed) {
216+
"Passed"
217+
}
215218
elseif (-not $discoveryOnly -and $b.ShouldRun -and (-not $b.Executed -or -not $b.Passed)) {
216219
"Failed"
217220
}

tst/Pester.RSpec.ts.ps1

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2966,4 +2966,48 @@ i -PassThru:$PassThru {
29662966
$ex.Exception.Message | Verify-Like '*Unbound scriptblock*'
29672967
}
29682968
}
2969+
2970+
# Regression test for https://github.com/pester/Pester/issues/2538
2971+
# When a container has discovery errors (e.g. syntax error in BeforeAll), the
2972+
# overall result must be Failed, not Passed. Before this fix, errors were checked
2973+
# after Passed, so a container with both Passed=true and ErrorRecord was marked Passed.
2974+
b "Discovery errors mark container as Failed" {
2975+
t "container with discovery error has result Failed" {
2976+
$sb = {
2977+
Describe 'Has discovery error' {
2978+
BeforeAll {
2979+
throw 'deliberate discovery error'
2980+
}
2981+
It 'should not run' {
2982+
$true | Should -Be $true
2983+
}
2984+
}
2985+
}
2986+
2987+
$r = Invoke-Pester -Configuration ([PesterConfiguration]@{
2988+
Run = @{ ScriptBlock = $sb; PassThru = $true }
2989+
Output = @{ Verbosity = 'None' }
2990+
})
2991+
2992+
$r.Result | Verify-Equal 'Failed'
2993+
$r.Containers[0].Result | Verify-Equal 'Failed'
2994+
}
2995+
2996+
t "container without errors still passes" {
2997+
$sb = {
2998+
Describe 'All good' {
2999+
It 'passes' {
3000+
$true | Should -Be $true
3001+
}
3002+
}
3003+
}
3004+
3005+
$r = Invoke-Pester -Configuration ([PesterConfiguration]@{
3006+
Run = @{ ScriptBlock = $sb; PassThru = $true }
3007+
Output = @{ Verbosity = 'None' }
3008+
})
3009+
3010+
$r.Result | Verify-Equal 'Passed'
3011+
}
3012+
}
29693013
}

0 commit comments

Comments
 (0)