From 42044722a95a679a3bb74a9ea01fd58b51adae67 Mon Sep 17 00:00:00 2001 From: Jakub Jares Date: Fri, 3 Apr 2026 11:42:13 +0200 Subject: [PATCH] Fix #2538: Container/block with discovery errors reported as Failed Copilot-generated fix. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- src/Pester.RSpec.ps1 | 9 ++++++--- tst/Pester.RSpec.ts.ps1 | 44 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 3 deletions(-) diff --git a/src/Pester.RSpec.ps1 b/src/Pester.RSpec.ps1 index e15094b98..10ba6759b 100644 --- a/src/Pester.RSpec.ps1 +++ b/src/Pester.RSpec.ps1 @@ -176,6 +176,9 @@ function PostProcess-RspecTestRun ($TestRun) { $b.Result = if ($b.Skip) { "Skipped" } + elseif (0 -lt $b.ErrorRecord.Count) { + "Failed" + } elseif ($b.Passed) { "Passed" } @@ -206,12 +209,12 @@ function PostProcess-RspecTestRun ($TestRun) { $b.result = if ($b.Skip) { "Skipped" } - elseif ($b.Passed) { - "Passed" - } elseif (0 -lt $b.ErrorRecord.Count) { "Failed" } + elseif ($b.Passed) { + "Passed" + } elseif (-not $discoveryOnly -and $b.ShouldRun -and (-not $b.Executed -or -not $b.Passed)) { "Failed" } diff --git a/tst/Pester.RSpec.ts.ps1 b/tst/Pester.RSpec.ts.ps1 index c8847d604..2333ed922 100644 --- a/tst/Pester.RSpec.ts.ps1 +++ b/tst/Pester.RSpec.ts.ps1 @@ -2966,4 +2966,48 @@ i -PassThru:$PassThru { $ex.Exception.Message | Verify-Like '*Unbound scriptblock*' } } + + # Regression test for https://github.com/pester/Pester/issues/2538 + # When a container has discovery errors (e.g. syntax error in BeforeAll), the + # overall result must be Failed, not Passed. Before this fix, errors were checked + # after Passed, so a container with both Passed=true and ErrorRecord was marked Passed. + b "Discovery errors mark container as Failed" { + t "container with discovery error has result Failed" { + $sb = { + Describe 'Has discovery error' { + BeforeAll { + throw 'deliberate discovery error' + } + It 'should not run' { + $true | Should -Be $true + } + } + } + + $r = Invoke-Pester -Configuration ([PesterConfiguration]@{ + Run = @{ ScriptBlock = $sb; PassThru = $true } + Output = @{ Verbosity = 'None' } + }) + + $r.Result | Verify-Equal 'Failed' + $r.Containers[0].Result | Verify-Equal 'Failed' + } + + t "container without errors still passes" { + $sb = { + Describe 'All good' { + It 'passes' { + $true | Should -Be $true + } + } + } + + $r = Invoke-Pester -Configuration ([PesterConfiguration]@{ + Run = @{ ScriptBlock = $sb; PassThru = $true } + Output = @{ Verbosity = 'None' } + }) + + $r.Result | Verify-Equal 'Passed' + } + } }