Skip to content

Commit b94d9f8

Browse files
nohwndCopilot
andauthored
Fix #2538: Container/block with discovery errors reported as Failed (#2694)
* Fix #2538: Container/block with discovery errors reported as Failed Copilot-generated fix. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * Strengthen discovery-error tests with the issue's exact repro The previous test used BeforeAll { throw }, which is an execution-time error. That triggers the existing ShouldRun/Executed Failed branch, so the container would already report Failed without this fix and the test passed on the buggy code as well. Replace it with the actual repro from #2538: an It that runs (making the container Passed=true) followed by a throw at the bottom of the Describe body that fails discovery (populating ErrorRecord). This is the exact case where the old precedence (Passed before ErrorRecord) returned Passed for an obviously-failed container, and where the fix flips it to Failed. Verified: the new test fails on the previous main and passes on this branch. Kept the BeforeAll case as a smoke test alongside. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --------- Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 9537037 commit b94d9f8

2 files changed

Lines changed: 79 additions & 3 deletions

File tree

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: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2966,4 +2966,77 @@ 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 discovery of a Describe body throws after at least one passing It, the
2972+
# container ends up with Passed=$true (the It passed) AND a non-empty
2973+
# ErrorRecord (the discovery throw lands on the container, not the inner
2974+
# block). Before this fix the result was computed by checking Passed before
2975+
# ErrorRecord, so such a container was reported as Passed even though the run
2976+
# also listed it under FailedContainers. The fix flips the precedence so
2977+
# ErrorRecord wins.
2978+
b "Discovery errors mark container as Failed" {
2979+
t "container with discovery error after a passing It has result Failed" {
2980+
# Exact repro from the issue: an It runs (so Passed becomes $true) and
2981+
# then a throw at the bottom of the Describe body fails discovery.
2982+
$sb = {
2983+
Describe 'd' {
2984+
It '1' {}
2985+
throw 'omg'
2986+
}
2987+
}
2988+
2989+
$r = Invoke-Pester -Configuration ([PesterConfiguration]@{
2990+
Run = @{ ScriptBlock = $sb; PassThru = $true }
2991+
Output = @{ Verbosity = 'None' }
2992+
})
2993+
2994+
$r.Result | Verify-Equal 'Failed'
2995+
$r.Containers[0].Result | Verify-Equal 'Failed'
2996+
$r.FailedContainersCount | Verify-Equal 1
2997+
}
2998+
2999+
t "container with BeforeAll throw has result Failed" {
3000+
# BeforeAll throws at execution time. Passed is $false but ErrorRecord
3001+
# is set; verify the ErrorRecord branch still produces Failed (it would
3002+
# also fall through to the existing ShouldRun/Executed branch, but the
3003+
# new ErrorRecord branch is what should fire).
3004+
$sb = {
3005+
Describe 'Has BeforeAll error' {
3006+
BeforeAll {
3007+
throw 'deliberate BeforeAll error'
3008+
}
3009+
It 'should not run' {
3010+
$true | Should -Be $true
3011+
}
3012+
}
3013+
}
3014+
3015+
$r = Invoke-Pester -Configuration ([PesterConfiguration]@{
3016+
Run = @{ ScriptBlock = $sb; PassThru = $true }
3017+
Output = @{ Verbosity = 'None' }
3018+
})
3019+
3020+
$r.Result | Verify-Equal 'Failed'
3021+
$r.Containers[0].Result | Verify-Equal 'Failed'
3022+
}
3023+
3024+
t "container without errors still passes" {
3025+
$sb = {
3026+
Describe 'All good' {
3027+
It 'passes' {
3028+
$true | Should -Be $true
3029+
}
3030+
}
3031+
}
3032+
3033+
$r = Invoke-Pester -Configuration ([PesterConfiguration]@{
3034+
Run = @{ ScriptBlock = $sb; PassThru = $true }
3035+
Output = @{ Verbosity = 'None' }
3036+
})
3037+
3038+
$r.Result | Verify-Equal 'Passed'
3039+
$r.Containers[0].Result | Verify-Equal 'Passed'
3040+
}
3041+
}
29693042
}

0 commit comments

Comments
 (0)