Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions src/Pester.RSpec.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
Expand Down Expand Up @@ -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"
}
Expand Down
73 changes: 73 additions & 0 deletions tst/Pester.RSpec.ts.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -2966,4 +2966,77 @@ i -PassThru:$PassThru {
$ex.Exception.Message | Verify-Like '*Unbound scriptblock*'
}
}

# Regression test for https://github.com/pester/Pester/issues/2538
# When discovery of a Describe body throws after at least one passing It, the
# container ends up with Passed=$true (the It passed) AND a non-empty
# ErrorRecord (the discovery throw lands on the container, not the inner
# block). Before this fix the result was computed by checking Passed before
# ErrorRecord, so such a container was reported as Passed even though the run
# also listed it under FailedContainers. The fix flips the precedence so
# ErrorRecord wins.
b "Discovery errors mark container as Failed" {
t "container with discovery error after a passing It has result Failed" {
# Exact repro from the issue: an It runs (so Passed becomes $true) and
# then a throw at the bottom of the Describe body fails discovery.
$sb = {
Describe 'd' {
It '1' {}
throw 'omg'
}
}

$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'
$r.FailedContainersCount | Verify-Equal 1
}

t "container with BeforeAll throw has result Failed" {
# BeforeAll throws at execution time. Passed is $false but ErrorRecord
# is set; verify the ErrorRecord branch still produces Failed (it would
# also fall through to the existing ShouldRun/Executed branch, but the
# new ErrorRecord branch is what should fire).
$sb = {
Describe 'Has BeforeAll error' {
BeforeAll {
throw 'deliberate BeforeAll 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'
$r.Containers[0].Result | Verify-Equal 'Passed'
}
}
}
Loading