Skip to content

Pester breaks on functions with unknown label #2669

Closed

Description

@iRon7

Checklist

What is the issue?

Not sure whether this is a PowerShell a bug or feature but fixing it in PowerShell would probably be a break change.

A side effect of this "feature" is also that it breaks all running Pester tests:

Actual behavior

Starting discovery in 1 files.
Discovery found 3 tests in 47ms.
Running tests.

(no further results)

Expected Behavior

Starting discovery in 1 files.
Discovery found 3 tests in 38ms.
Running tests.
[-] Describe.Context.It 1 27ms (27ms|0ms)
 Expected $null or empty, but got 1.
 at Test 1 | Should -BeNullOrEmpty, C:\Users\Gebruiker\OneDrive\Scripts\PowerShell\PowerSnippets\ObjectGraph\ObjectGraphTools\_Temp\Test\Pester\Test.ps1:13
 at <ScriptBlock>, C:\Users\Gebruiker\OneDrive\Scripts\PowerShell\PowerSnippets\ObjectGraph\ObjectGraphTools\_Temp\Test\Pester\Test.ps1:13
[-] Describe.Context.It 2 37ms (36ms|1ms)
 Expected $null or empty, but got @(1, 2).
 at Test 2 | Should -BeNullOrEmpty, C:\Users\Gebruiker\OneDrive\Scripts\PowerShell\PowerSnippets\ObjectGraph\ObjectGraphTools\_Temp\Test\Pester\Test.ps1:17
 at <ScriptBlock>, C:\Users\Gebruiker\OneDrive\Scripts\PowerShell\PowerSnippets\ObjectGraph\ObjectGraphTools\_Temp\Test\Pester\Test.ps1:17
[-] Describe.Context.It 3 28ms (27ms|1ms)
 Expected $null or empty, but got @(1, 2, 3).
 at Test 3 | Should -BeNullOrEmpty, C:\Users\Gebruiker\OneDrive\Scripts\PowerShell\PowerSnippets\ObjectGraph\ObjectGraphTools\_Temp\Test\Pester\Test.ps1:21
 at <ScriptBlock>, C:\Users\Gebruiker\OneDrive\Scripts\PowerShell\PowerSnippets\ObjectGraph\ObjectGraphTools\_Temp\Test\Pester\Test.ps1:21
Tests completed in 282ms
Tests Passed: 0, Failed: 3, Skipped: 0 NotRun: 0

Or at least:

Tests completed in ...
Tests Passed: ..., Failed: ..., Skipped: ... NotRun: ...

Or an error message

Steps To Reproduce

Function Test($b) {
    :foo foreach ($a in 1,2,3) {
        $a
        if ($a -ge $b) { break bar }
    }
}

Describe 'Describe' {

    Context 'Context' {

        It 'It 1' {
            Test 1 | Should -BeNullOrEmpty
        }

        It 'It 2' {
            Test 2 | Should -BeNullOrEmpty
        }

        It 'It 3' {
            Test 3 | Should -BeNullOrEmpty
        }
    }

}

Describe your environment

Pester version     : 5.5.0 C:\Users\Gebruiker\Documents\PowerShell\Modules\Pester\5.5.0\Pester.psm1                     
PowerShell version : 7.5.4
OS version         : Microsoft Windows NT 10.0.26100.0

Possible Solution?

Check for unknown labels during discovery?

Activity

  1. iRon7 commented on Jan 27, 2026

    @iRon7
    Author

    Fyi: a resolution for this unexpected PowerShell behavior has been declined: PowerShell/PowerShell#26587 (comment)
    I wonder whether there is any intention to resolve this in Pester (see the helpful comment for a possible solution)

    function DoWeirdBreak { break sdfsdf }
    
    $ps = $null
    try {
        $ps = [powershell]::Create('CurrentRunspace')
        $ps.AddScript{ DoWeirdBreak }.
            Invoke(
                @(),
                [System.Management.Automation.PSInvocationSettings]@{
                    ExposeFlowControlExceptions = $true
                })
    
    } catch [System.Management.Automation.BreakException] {
        'Break occurred with label: {0}' -f $_.Exception.Label
    } catch [System.Management.Automation.ContinueException] {
        'Continue occurred with label: {0}' -f $_.Exception.Label
    } finally {
        ($ps)?.Dispose()
    }
  2. iRon7 commented on Feb 26, 2026

    @iRon7
    Author

    I didn't had much experience with the Pester configuration, but this might help to figure out which test Pester was processed last and could also be helpful in the case of an infinitive loop:

    $PesterPreference = [PesterConfiguration]::Default
    $PesterPreference.Output.Verbosity = 'Detailed'
    .\Test.ps1
    Pester v5.5.0
    
    Starting discovery in 1 files.
    Discovery found 3 tests in 11ms.
    Running tests.
    
    Running tests from 'C:\Users\Gebruiker\OneDrive\Scripts\PowerShell\Pester\#2669_UnhandledLabel\Test.ps1'
    Describing Describe
     Context Context
    PS C:\>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions