test/Trace-Health.Tests.ps1
#requires -Version 5 #requires -Modules Pester Import-Module (Join-Path $PSScriptRoot "../PSHealthchecksIO.psd1" -Resolve) -Force InModuleScope "PSHealthchecksIO" { Describe "Trace-Health" { Context "When the script fails" { Mock Send-HealthchecksIOPing It "Rethrows the error" { { Trace-Health { throw "My custom error" } -CheckUUID "65378039-7210-44bb-b32f-39339fab0ac7" } | Should -Throw "My custom error" } It "Reports a failure to healthchecks.io" { Assert-MockCalled Send-HealthchecksIOPing -ParameterFilter { $Type -eq "fail" -and $CheckUUID -eq "65378039-7210-44bb-b32f-39339fab0ac7" } -Times 1 -Exactly -Scope Context } It "Does not report a success to healthchecks.io" { Assert-MockCalled Send-HealthchecksIOPing -ParameterFilter { [string]::IsNullOrEmpty($Type) } -Times 0 -Exactly -Scope Context } } Context "When the script succeeds" { Mock Send-HealthchecksIOPing It "Doesn't throw an error" { { Trace-Health { return "My success message" } -CheckUUID "d4851a2a-2cfb-4874-b441-501693b32a31" } | Should -Not -Throw } It "Reports a success to healthchecks.io" { Assert-MockCalled Send-HealthchecksIOPing -ParameterFilter { [string]::IsNullOrEmpty($Type) -and $CheckUUID -eq "d4851a2a-2cfb-4874-b441-501693b32a31" } -Times 1 -Exactly -Scope Context } It "Does not report a failure to healthchecks.io" { Assert-MockCalled Send-HealthchecksIOPing -ParameterFilter { $Type -eq "fail" } -Times 0 -Exactly -Scope Context } # TODO: Using variable scoping to avoid calling this twice $result = Trace-Health { return "My success message" } -CheckUUID "d4851a2a-2cfb-4874-b441-501693b32a31" It "Returns the value from the scriptblock" { $result | Should Be "My success message" } } } } |