pester / vscode-adapter

Run PowerShell Pester Tests with Visual Studio Code
MIT License
58 stars 14 forks source link

Cannot index into a null array while using -ForEach #186

Closed mmisztal1980 closed 1 year ago

mmisztal1980 commented 1 year ago

image

I'm attempting to run pester tests from VSCode (for convenience). I've noticed that whenever I'm using -ForEach, the VSCode integration fails with : Cannot index into a null array. error. This doesn't apply to regular tests.

Pester Tests version: v2023.7.7

Pester version:

Get-Module
ModuleType Version    PreRelease Name                                ExportedCommands
---------- -------    ---------- ----                                ----------------
Manifest   7.0.0.0               Microsoft.PowerShell.Management     {Add-Content, Clear-Content, …
Manifest   7.0.0.0               Microsoft.PowerShell.Utility        {Add-Member, Add-Type, Clear-…
Script     5.5.0                 Pester                              {Add-ShouldOperator, AfterAll…
Script     0.2.0                 PowerShellEditorServices.Commands   {Clear-Host, ConvertFrom-Scri…
Binary     0.2.0                 PowerShellEditorServices.VSCode     {Close-VSCodeHtmlContentView,…
JustinGrote commented 1 year ago

@mmisztal1980 thanks for reporting the issue! Have you tried -TestCases instead of -Foreach?

Do you have a minimal reproduction I can look at?

JustinGrote commented 1 year ago

For reference, this works fine and is in my current tests:

Describe 'Describe Nested Foreach <name> <symbol>' -ForEach @(
    @{ Name = 'cactus'; Symbol = '🌵'; Kind = 'Plant' }
    @{ Name = 'giraffe'; Symbol = '🦒'; Kind = 'Animal' }
) {
    It 'Returns <symbol>' { $true }

    It 'Has kind <kind>' { $true }

    It 'Nested Hashtable TestCase <kind> <name>' { $true } -TestCases @{
        Name = 'test'
    }
    It 'Nested Array TestCase <kind> <_>' { $true } -TestCases @(
        'Test'
    )
    It 'Nested Multiple Hashtable TestCase <kind> <name> <symbol>' { $true } -TestCases @(
        @{
            Name = 'Pester1'
        }
        @{
            Name = 'Pester2'
        }
    )

    Context 'Context Nested Foreach <name> <ContextValue>' -ForEach @(
        @{ ContextValue = 'Test1' }
        @{ ContextValue = 'Test2' }
    ) {
        It 'Describe Context Nested Array <name> <contextvalue> <_>' -TestCases @(
            'Test1'
            'Test2'
        ) { $true }
    }
}
JustinGrote commented 1 year ago

@mmisztal1980 I think actually it is because you have duplicate test names because you didn't use a variable in your foreach title description. The latest build now detects this and errors appropriately. Can you try the latest prerelease?

mmisztal1980 commented 1 year ago

Hi Justin, I'm going to try this out today. Thanks for taking a look

mmisztal1980 commented 1 year ago

@JustinGrote I don't see neither a new prerelease version of Pester nor of the VS Code Pester Tests extension - where should I look?

mmisztal1980 commented 1 year ago

I've added to placeholders to make the test cases' names unique, however I'm still seeing the issue.

image

I'm attaching the test-code (the parts that I can) https://gist.github.com/mmisztal1980/35b2ec0eecdd9dce40d6d6e337b4f063

JustinGrote commented 1 year ago

image

JustinGrote commented 1 year ago

Your example doesn't work in normal Pester either. It looks to me like your services block either needs to be defined at script scope or in a BeforeAll image

Here's an example that works for me (at least gets past the null index

BeforeAll {
  [hashtable]$SCRIPT:services = @{
    'NuGet' = 'SomeService';
    'Paket' = 'SomePaketService';
  }
}

Please check in the future that your Pester tests work fine with Invoke-Pester run inside pwsh -noprofile before filing an issue on the extension itself. Thanks!

mmisztal1980 commented 1 year ago

Hmmm interesting. Any idea why is this behavior occuring?

JustinGrote commented 1 year ago

Hmmm interesting. Any idea why is this behavior occuring?

https://pester.dev/docs/usage/discovery-and-run https://pester.dev/docs/usage/setup-and-teardown