pester / Pester

Pester is the ubiquitous test and mock framework for PowerShell.
https://pester.dev/
Other
3.08k stars 470 forks source link

`Should-BeNull`: Strange behaviour when `$null` #2555

Open johlju opened 1 month ago

johlju commented 1 month ago

Checklist

What is the issue?

I'm getting that the $result passed to Should-BeNull is not $null, but the verbose output I added to debug this do say it is not an array and it is indeed $null. πŸ€” Not sure if I'm doing something wrong. I have seen this in another place as well but ignored it there, when I got it here I thought this must be an error somewhere. πŸ™‚

The Out-Diff currently only writes Write-Information messages and returns no values. I output verbose message to determine that it is indeed $null and not an array, still Should-BeNull thinks it is.

It 'Should output to console' {
    $expected = @(
        'My String that is longer than actual'
    )
    $actual = @(
        'My string that is shorter'
    )

    $result = Out-Diff -ExpectedString $expected -ActualString $actual
    Write-Verbose ($result -is [array]) -Verbose
    Write-Verbose ($null -eq $result) -Verbose
    $result | Should-BeNull
}
VERBOSE: False
VERBOSE: True
[-] Out-Diff.When output is made as informational message.Should output to console 124ms (122ms|2ms)
 Expected $null, but got [Object[]] '@()'.
 at $result | Should-BeNull -Debug -Verbose, /Users/johlju/source/Viscalyx.Common/tests/Unit/Public/Out-Diff.tests.ps1:94

Expected Behavior

Should pass test as the actual value is $null.

Steps To Reproduce

Running this reproduces the issue:

BeforeAll {
    function Out-Something
    {
        param
        (
        )
    }
}

Describe 'Out-Something' {
    It 'Should not output anything' {
        $result = $null
        $result = Out-Something

        Write-Verbose ("Result is null: {0}" -f ($null -eq $result)) -Verbose
        Write-Verbose ("Result is array: {0}" -f ($result -is [array])) -Verbose

        $result | Should-BeNull
    }
}

Returns:

Starting discovery in 1 files.
Discovery found 1 tests in 101ms.
Running tests.
VERBOSE: Result is null: True
VERBOSE: Result is array: False
[-] Out-Something.Should not output anything 118ms (109ms|9ms)
 Expected $null, but got [Object[]] '@()'.
 at $result | Should-BeNull, /Users/johlju/source/Viscalyx.Common/tests/Unit/Public/Out-Something.tests.ps1:18

Describe your environment

Pester version     : 6.0.0-alpha4 /Users/johlju/source/Viscalyx.Common/output/RequiredModules/Pester/6.0.0/Pester.psm1  
PowerShell version : 7.4.4
OS version         : Unix 14.5.0

Possible Solution?

Hopefully I'm just doing something wrong. πŸ™‚

johlju commented 1 month ago

If I actually return $null in the function Out-Something then it works. πŸ€”

BeforeAll {
    function Out-Something
    {
        param
        (
        )

        $null
    }
}

Describe 'Out-Something' {
    It 'Should not output anything' {
        $result = $null
        $result = Out-Something

        Write-Verbose ("Result is null: {0}" -f ($null -eq $result)) -Verbose
        Write-Verbose ("Result is array: {0}" -f ($result -is [array])) -Verbose

        $result | Should-BeNull
    }
}

Returns:

Starting discovery in 1 files.
Discovery found 1 tests in 11ms.
Running tests.
VERBOSE: Result is null: True
VERBOSE: Result is array: False
[+] /Users/johlju/source/Viscalyx.Common/tests/Unit/Public/Out-Something.tests.ps1 420ms (5ms|405ms)
Tests completed in 421ms
Tests Passed: 1, Failed: 0, Skipped: 0, Inconclusive: 0, NotRun: 0
johlju commented 1 month ago

For some reason $result is not actually $null in the reproducible example, although the verbose statements says it is - but it is not... πŸ€”

This doesn't seem to be a Pester issue, but probably how PowerShell works... I just don't understand why it behaves as it does. If someone could shed som light it would be appreciated. πŸ™‚