nohwnd / Assert

A set of advanced assertions for Pester to simplify how you write tests.
MIT License
101 stars 12 forks source link

How should non-terminating errors in Assert-Throw behave? #17

Open nohwnd opened 7 years ago

nohwnd commented 7 years ago

PowerShell has two types of errors: terminating and non-terminating. For the moment Assert-Throw only catches terminating errors. To force catching even non-terminating errors you need to change $ErrorActionPreference to Stop in the test script block as so:

{ 
    $ErrorActionPreference = 'stop' 
    Write-Error "this is terminating error" 
} | Assert-Throw

This is inconvenient to do for every test, and hard to discover, and should imho be handled by the framework.

The proposed behavior is to be strict and force $ErrorActionPreference = 'stop' in every Assert-Throw, and only optionally relaxing this rule by specifying -AllowNonTerminatingError switch:

{ 
    # $ErrorActionPreference = 'stop' is set by the framework
    Write-Error "this is terminating error" 
} | Assert-Throw
{ 
    # $ErrorActionPreference = 'continue' is set by the framework
    Write-Error "this is non-terminating error" 
} | Assert-Throw -AllowNonTerminatingError

Thoughts?

nohwnd commented 7 years ago

Piloting this in 0.7.0