Skip to content

Invoke-Pester doesn't recognize using statements #2630

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

Open
iRon7 opened this issue Apr 7, 2025 · 1 comment
Open

Invoke-Pester doesn't recognize using statements #2630

iRon7 opened this issue Apr 7, 2025 · 1 comment

Comments

@iRon7
Copy link

iRon7 commented Apr 7, 2025

What is the issue?

In my use case it actually concerns the using module ... statement but the general issue might be simply reproduced with any using statement as using namespace ...:
Taken the following test:

Using namespace System.Collections
Using namespace System.Collections.Generic

Describe 'Describe-Using' {
    Context 'Using' {
        It 'Should use the using statement' {
            'Test' | Should -not -BeOfType [IList]
        }
    }
}

The pester test runs fine when e.g. dot-sourced (or pressing F5 in VSCode).
But when launched by Invoke-Pester from a new PowerShell session, it apparently doesn't recognize the general using statements for the individual tests, and fails the tests:

invoke-pester .\Using.Tests.ps1

Starting discovery in 1 files.
Discovery found 1 tests in 357ms.
Running tests.
[-] Describe-Using.Using.Should use the using statement 296ms (250ms|46ms)
 ArgumentException: Could not find type [IList]. Make sure that the assembly that contains that type is loaded.
Tests completed in 1.74s
Tests Passed: 0, Failed: 1, Skipped: 0 NotRun: 0

Expected Behavior

No failures

Although this might somehow being expected behavior, it has its effects applications as the VSCode-Adapter, see: pester/vscode-adapter#286

@iRon7 iRon7 changed the title Invoke-Command doesn't recognize using statements Invoke-Pester doesn't recognize using statements Apr 7, 2025
@JustinGrote
Copy link
Contributor

I'm going to guess this is because the using is in a different scope than the test scriptblock when it is run. The using ends up being part of the "discovery" phase, wheres the execution is part of the "run" phase.

I tried a couple workarounds like dot-sourcing the script with using namespace in it, oddly it didn't work.

using namespace is global to the session, so that's why it works after you run it once, but not in a new session.

Not sure this is something that could be fixed in Pester, as it's a core PowerShell parsing issue (I think there's a feature request somewhere to allow using namespace at the top of scriptblocks for the parser but it'll probably not happen for a while if ever)

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

2 participants