Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nested Context can cause tests results to display incorrectly #196

Open
kevball2 opened this issue Sep 7, 2023 · 3 comments
Open

Nested Context can cause tests results to display incorrectly #196

kevball2 opened this issue Sep 7, 2023 · 3 comments

Comments

@kevball2
Copy link

kevball2 commented Sep 7, 2023

Nest contexts don't appear to display results correctly in the test explorer pane.

BeforeDiscovery {
    $testCases = @(
        @{  
            Name = "fizz"
        },
        @{
            Name = "buzz"
        },
        @{  
            Name = "foo"
        },
        @{
            Name = "bar"
        }
    )
}

Describe "Testing Policy Definition <name>" -ForEach $testCases {   
    Context 'Level 1' {
        It 'Should not be null level 1' {
            $name | should -Not -BeNullOrEmpty
        }
        Context "Level 2" {
            It 'Should be 3 characters or greater' {
                $name.length | should -BeGreaterOrEqual  3
            }
        }
    }
}

The output in the test pane does not show the expect results though

image

The pester output shows that the level 2 context tests are showing as "Test Moved or Changed" on each run.

2023-09-07 07:53:57.336 [info] Test Run Start: 1 test items
2023-09-07 07:53:57.642 [info] Test Discovery Start: 1 files
2023-09-07 07:53:57.756 [info] Discovery: Test Moved Or Changed - C:\USERS\<USER>\GIT\PESTER-TESTING\PESTER.TESTS.PS1>>TESTING POLICY DEFINITION <NAME>>>LEVEL 1>>LEVEL 2>>SHOULD BE 3 CHARACTERS OR GREATER
2023-09-07 07:53:57.763 [info] Discovery: Test Moved Or Changed - C:\USERS\<USER>\GIT\PESTER-TESTING\PESTER.TESTS.PS1>>TESTING POLICY DEFINITION <NAME>>>LEVEL 1>>LEVEL 2>>SHOULD BE 3 CHARACTERS OR GREATER
2023-09-07 07:53:57.775 [info] Discovery: Test Moved Or Changed - C:\USERS\<USER>\GIT\PESTER-TESTING\PESTER.TESTS.PS1>>TESTING POLICY DEFINITION <NAME>>>LEVEL 1>>LEVEL 2>>SHOULD BE 3 CHARACTERS OR GREATER
2023-09-07 07:53:57.787 [info] Discovery: Test Moved Or Changed - C:\USERS\<USER>\GIT\PESTER-TESTING\PESTER.TESTS.PS1>>TESTING POLICY DEFINITION <NAME>>>LEVEL 1>>LEVEL 2>>SHOULD BE 3 CHARACTERS OR GREATER
2023-09-07 07:53:57.797 [info] Discovery Run End (PesterInterface stream closed)
2023-09-07 07:53:58.489 [info] Test Run End: PesterInterface stream closed

In the test results pane it does show the correct number of tests was run

Starting discovery in 1 files.
Discovery found 8 tests in 47ms.
Running tests.
[+] C:\Users\<user>\git\pester-testing\pester.tests.ps1
 566ms (81ms|453ms)
Tests completed in 580ms
Tests Passed: 8,
Failed: 0,
Skipped: 0

NotRun: 0
@kevball2 kevball2 changed the title Nested Context can cause tests to be skipped Nested Context can cause tests results to display incorrectly Sep 7, 2023
@JustinGrote
Copy link
Collaborator

JustinGrote commented Sep 11, 2023

THe <name> is not resolving in the test change info, maybe there's an order issue with resolving that.

@kevball2
Copy link
Author

THe <name> is not resolving in the test change info, maybe there's an order issue with resolving that.

Is there more info I can gather to help sort this issue? Sadly I am not any good at TypeScript or JavaScript or I would take a look at the source

@lupuscaoticus
Copy link

lupuscaoticus commented Feb 12, 2025

Same problem... -->> Workaround / Gist: https://gist.github.com/lupuscaoticus/6ab376c057c371124856c02ec8c36b3d / Issue: #196

Observed Behavior

  • Pester terminal commands work as expected
  • The Pester VSCode extension does not display all tests

Conditions to Reproduce

This occurs if blocks that use -ForEach contain more than two child It/Describe/Context blocks.

Potential Cause

It might be caused by the extension only using variables from the three innermost blocks for the test path.

Proposed Fix

Modify the extension to recursively use all defined variables for the test path.

Workaround

Use a custom function to pass test-case variables down the hierarchy until they are within the innermost three blocks.

Custom 'Case' Function

# HACK: Workaround / Gist: https://gist.github.com/lupuscaoticus/6ab376c057c371124856c02ec8c36b3d / Issue: https://github.com/pester/vscode-adapter/issues/196
Function Case {
  [Hashtable]$Variables = @{}
  ForEach ($Key in $Case.Keys) {
    If ($Key -NotIn $null, 'Case') {
      $Variables.Add($Key, $Case[$Key])
    }
  }
  ForEach ($Arg in $Args) {
    ForEach ($Key In $Arg.Keys) {
      If ($Key -NotIn $null, 'Case') {
        If ($Variables.ContainsKey($Key)) {
          $Variables[$Key] = $Arg[$Key]
        }
        Else {
          $Variables.Add($Key, $Arg[$Key])
        }
      }
    }
  }
  $Variables.Add('Case', $Variables)
  Return $Variables
}

Example Tests File

# HACK: Workaround / Gist: https://gist.github.com/lupuscaoticus/6ab376c057c371124856c02ec8c36b3d / Issue: https://github.com/pester/vscode-adapter/issues/196
Function Case { $V = @{}; ForEach ($K in $Case.Keys) { If ($K -NotIn $null, 'Case') { $V.Add($K, $Case[$K]) } } ForEach ($A in $Args) { ForEach ($K In $A.Keys) { If ($K -NotIn $null, 'Case') { If ($V.ContainsKey($K)) { $V[$K] = $A[$K] } Else { $V.Add($K, $A[$K]) } } } } $V.Add('Case', $V); $V }

Context 'Test: Scenario ''<Scenario>''' -ForEach @(
  Case @{ Scenario = 'Alpha'; }
  Case @{ Scenario = 'Omega'; }
) {

  Context 'Test: Variant ''<Variant>''' -ForEach @(
    Case @{ Variant = 'Reused'; }
    Case @{ Variant = 'Unique'; }
  ) {

    BeforeAll {
      $TestID = "${Scenario}${Variant}$(If ($Variant -iEq 'Reused') { '00000000000000' } Else { Get-Date -Format 'yyyyMMddHHmmss' })"
      # ... -... $TestID -...
    }

    Describe 'Mirror ''<Mirror>''' -ForEach @(
      Case @{ Mirror = 'London'; }
      Case @{ Mirror = 'Berlin'; }
    ) {

      Describe 'API ''<API>''' -ForEach @(
        Case @{ API = 'Public'; }
        Case @{ API = 'Private'; }
      ) {

        It 'Endpoint ''<Endpoint>''' -ForEach @(
          Case @{ Endpoint = 'allUsers'; }
          Case @{ Endpoint = 'pagUsers'; }
        ) {
          
          $true | `
            Should -Be $true `
            -Because 'This is a test'

          $false | `
            Should -Be $false `
            -Because 'This is a test'

        }

      }

    }

  }

}

Applied on the eample from @kevball2

# HACK: Workaround / Gist: https://gist.github.com/lupuscaoticus/6ab376c057c371124856c02ec8c36b3d / Issue: https://github.com/pester/vscode-adapter/issues/196
Function Case { $V = @{}; ForEach ($K in $Case.Keys) { If ($K -NotIn $null, 'Case') { $V.Add($K, $Case[$K]) } } ForEach ($A in $Args) { ForEach ($K In $A.Keys) { If ($K -NotIn $null, 'Case') { If ($V.ContainsKey($K)) { $V[$K] = $A[$K] } Else { $V.Add($K, $A[$K]) } } } } $V.Add('Case', $V); $V }

BeforeDiscovery {
    $testCases = @(
        @{ Name = "fizz"; };
        @{ Name = "buzz"; };
        @{ Name = "foo" ; };
        @{ Name = "bar" ; };
    )
}

Describe "Testing Policy Definition <name>" -ForEach @($testCases|%{Case $PSItem}) {   
    Context 'Level 1' -ForEach @(Case) {
        It 'Should not be null level 1' {
            $name | should -Not -BeNullOrEmpty
        }
        Context "Level 2" -ForEach @(Case) {
            It 'Should be 3 characters or greater' {
                $name.length | should -BeGreaterOrEqual  3
            }
        }
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants