pester / vscode-adapter

Run PowerShell Pester Tests with Visual Studio Code

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Cannot index into a null array while using -ForEach

mmisztal1980 opened this issue · comments

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,

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

Do you have a minimal reproduction I can look at?

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 }
	}
}

@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?

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

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

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

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!

Hmmm interesting. Any idea why is this behavior occuring?