Search code examples
powershellunit-testingwindows-10throwpester

Pester v5.1: Should -Throw with message containing square-brackets fails


I'm using pester to test my thrown exception (negative tests).

Although it seems, that the thrown exception message is a exact match with my Should -Throw, Pester declares the test failed, when there's square brackets in the exception message. By removing the square brackets from the thrown exception message (or replacing them with other kind of brackets), Pester tests will satisfy.

The following Pester tests illustrate these cases:

Describe "Testing Pester Should Throw" {
   Context "Positive tests: different brackets and one without" {
      It "Test 1" {
         [String]$strMyexMsg = "my text [with square brackets] 1"
         { throw $strMyexMsg } | Should -Throw $strMyexMsg
      }
      It "Test 2" {
         [String]$strMyexMsg = "my text with * asterix"
         { throw $strMyexMsg } | Should -Throw $strMyexMsg
      }
      It "Test 3" {
         [String]$strMyexMsg = "my text (with basic brackets)" 
         { throw $strMyexMsg } | Should -Throw $strMyexMsg
      }
      It "Test 4" {
         [String]$strMyexMsg = "my text {with curly brackets}"
         { throw $strMyexMsg } | Should -Throw $strMyexMsg
      }
      It "Test 5" {
         [String]$strMyexMsg = "my text without brackets"
         { throw $strMyexMsg } | Should -Throw $strMyexMsg
      }
   }
}

My Question: Why are test 1 failing and the tests 2...5 are successful?

This is the Pester output I get:

PS D:\srcGitHpg\hpg-tools\PowerShell> Invoke-Pester .\TestPesterShouldThrow.Test.ps1

Starting discovery in 1 files.
Discovery finished in 163ms.
[-] Testing Pester Should Throw.Positive tests: different brackets and one without.Test 1 13ms (12ms|1ms)
 Expected an exception, with message 'my text [with square brackets] 1' to be thrown, but the message was 'my text [with square brackets] 1'. from D:\srcGitHpg\hpg-tools\PowerShell\TestPesterShouldThrow.Test.ps1:7 char:12
     +          { throw $strMyexMsg } | Should -Throw $strMyexMsg
     +            ~~~~~~~~~~~~~~~~~
 at { throw $strMyexMsg } | Should -Throw $strMyexMsg, D:\srcGitHpg\hpg-tools\PowerShell\TestPesterShouldThrow.Test.ps1:7
 at <ScriptBlock>, D:\srcGitHpg\hpg-tools\PowerShell\TestPesterShouldThrow.Test.ps1:7
Tests completed in 389ms
Tests Passed: 4, Failed: 1, Skipped: 0 NotRun: 0

PowerShell version:

PS D:\srcGitHpg\hpg-tools\PowerShell> $PSVersionTable.PSVersion

Major  Minor  Build  Revision
-----  -----  -----  --------
5      1      17763  1490

Pester version:

PS D:\srcGitHpg\hpg-tools\PowerShell> Get-Module -Name "Pester"

ModuleType Version    Name                                ExportedCommands
---------- -------    ----                                ----------------
Script     5.1.0      Pester                              {Add-ShouldOperator, AfterAll, AfterEach, Assert-MockCalled...}

I'm using Windows command line on Windows with version:

Microsoft Windows [Version 10.0.17763.1577]
(c) 2018 Microsoft Corporation. All rights reserved.

C:\Windows\System32>

Thanks for help, Tommi


Solution

  • During the Pester v5 Upgrade the Should -Throw was changed to use -like to match the exception message instead of .Contains as it previously did in Pester v4.

    From the Breaking changes in v5

    Should -Throw is using -like to match the exception message instead of .Contains.
    Use * or any of the other -like wildcard to match only part of the message.
    

    You can work around this by escaping the [] with backticks `; as this example shows:

    Import-Module Pester -MinimumVersion '5.0.0'
    
    function Test-Throw {
        throw "I should throw a [message]"
    }
    
    Describe 'PesterThrowBug' {
        It 'Should Throw a Message' {
            { Test-Throw } | Should -Throw -ExpectedMessage 'I should throw a `[message`]'
        }
    }
    

    Here is a related Pester GitHub Issue https://github.com/pester/Pester/issues/1793