TylerLeonhardt / vscode-pester-test-adapter

MIT License
33 stars 13 forks source link

Cannot find Pester v5 although installed #39

Closed blindzero closed 3 years ago

blindzero commented 3 years ago

I have Pester v5 installed, now as some test even on AllUsers scope:

Directory: C:\Program Files\WindowsPowerShell\Modules

ModuleType Version    Name                                ExportedCommands
---------- -------    ----                                ----------------
Script     5.1.1      Pester                              {Invoke-Pester, Describe, Context, It...}

But anyway I still get the error:

Pester version '5.0.0' or higher was not found in any module directory. Make sure you have Pester v5+ installed: Install-Module Pester -MinimumVersion 5.0

Any idea? Thanks, appreciating your help.

TylerLeonhardt commented 3 years ago

What version of PowerShell is the PowerShell extension for VS Code using? (run $PSVersionTable in the PowerShell Integrated Console)

blindzero commented 3 years ago

I am using 5.1 Desktop

TylerLeonhardt commented 3 years ago

Can you tell me what this setting is set to?: powershell.powerShellDefaultVersion

Does this happen when you Run or Debug tests?

blindzero commented 3 years ago

In this workspace it is set to

"powershell.powerShellDefaultVersion": "Windows PowerShell (x64)"

It happens when opening my workspace as a small warning layover at the bottom. I was used that the module scanned my workspace for *.Tests.ps1 files. As this is not happening the tests are not shown in the left "Test" pane.

Running tests individually in the *.Tests.ps1 files will not work due to extensive "BeforeAll{}" which is necessary to run the tests.

TylerLeonhardt commented 3 years ago

Very odd... That happens when we can't find the Pester module which really isn't my codes fault.

You can try running the script that I run in the extention to get a better sense:

https://github.com/TylerLeonhardt/vscode-pester-test-adapter/blob/master/src/powershellScripts.ts#L6-L108

All you need to do is change the top to be an array of test file paths.

Can you give it a go and see if anything unusual happens?

Also in the Output pane in VS Code there should a Pester Test Explorer choice that will have the debug logs for the extension. Can you add those to this thread?

carlocardella commented 3 years ago

I had a similar error for one of my modules, but it turned I had a typo in one of my test files.

Log from Pester Text Explorer:

[2021-04-02 14:43:38.800] [ERROR] stderr: Import-Module: C:\Users\carloc\Git\AzToolbox\Tests\CheckHelp.Tests.ps1:2
Line |
   2 |  Import-Module $PSScriptRoot/../src/ClusterConfig -Force -ErrorAction  …
     | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     | The specified module 'C:\Users\carloc\Git\AzToolbox\Tests/../src/ClusterConfig' was not loaded because no valid module file was found in
     | many module directory.

[2021-04-02 14:43:38.830] [DEBUG] child process exited with code 1
[2021-04-02 14:43:38.834] [ERROR] Unable to parse JSON data from Pester test discovery script. Contents: 
[2021-04-02 14:43:39.322] [DEBUG] No test files found.

I have multiple test files, in one of them I was trying to import a non existing module, which in turn apparently lead to an error in the Pester test discovery and ultimately the extension does not recognize any test.

Bottom line, this was a user error in my case, but the error message shown is misleading (I do have Pester 5.1.1 available in my module path).

TylerLeonhardt commented 3 years ago

Interesting. @carlocardella in your case, I think I need to see if Pester couldn't discover tests due to an error and then let the user know about that.

jbolton-corvalgroup commented 3 years ago

@TylerLeonhardt mine is definitely broken, but I think I why. At least for my environment.

Two places are not using my $Env:PSModulePath (in my vscode profile)

Easy fix?

If I could force it to use my $Env:PSModulePath, I think it'd fix the errors.

Environment

# from inside the PSIT
ModuleType Version    PreRelease Name
---------- -------    ---------- ----
Script     5.1.1                 Pester

Name                      Value
----                      -----
PSVersion                 7.1.1
PSEdition                 Core
GitCommitId               7.1.1
OS                        Microsoft Windows 10.0.19042
Platform                  Win32NT
PSCompatibleVersions      {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion 2.3
SerializationVersion      1.1.0.1
WSManStackVersion         3.0

tl;dr; I can't use the default my documents folder.

Work forces my documents path to be network only over VPN. This means autocomplete, help, import, auto-import, etc... have a massive performance hit. Like up to 10 seconds to tab when it's not cached.

I get a huge boost if I

Which means pwsh -nop does not load my module path, ie: fails to import modules

About

The extension doesn't show an error until after PSIT finishes, so I thought maybe it'd use the same one/environment.

image

Partial Fix

If I edit powershellScripts.js, ( which appears to be powershellScripts.ts ) to include

$env:PSModulePath = 'C:\\jake\\github\\powershell_modules_import', $env:PSModulePath -join ';

Then Pester loads, and finds some tests.

"Fix" doesn't fix "Run tests" button

run test is using the 'my docs' over the internet instead of my module folder, so it errors out.

image

image

PSIT is loading Pester

PSIT imports fine. image

Join-Path ($env:PSModulePath -split ';' | select -First 1) 'Pester' | gi -ea Stop

Log (full contents are attached)

pester_explorer.log

[2021-04-16 19:29:01.199] [INFO] Test Explorer found
[2021-04-16 19:29:01.199] [INFO] Creating adapter for c:\jake\github\powershell\Portal.Powershell
[2021-04-16 19:29:01.199] [INFO] Initializing Pester adapter
[2021-04-16 19:29:01.199] [INFO] Initializing Pester test runner.
[2021-04-16 19:29:01.208] [INFO] Registering adapter for c:\jake\github\powershell\Portal.Powershell
[2021-04-16 19:29:01.210] [INFO] Loading Pester tests
[2021-04-16 19:29:01.210] [INFO] Initialization finished
[2021-04-16 19:29:03.416] [DEBUG] Found 1 paths
[2021-04-16 19:29:19.677] [DEBUG] Using PowerShell (x64) at: C:\Program Files\PowerShell\7\pwsh.exe
[2021-04-16 19:29:19.678] [DEBUG]
[2021-04-16 19:29:20.781] [ERROR] stderr: Import-Module:

[2021-04-16 19:29:20.781] [ERROR] stderr: Line |
   9 |  Import-Module Pester -MinimumVersion 5.0.0 -ErrorAction Stop
     |  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     | The specified module 'Pester' with version '5.0.0' was not loaded because no valid module file was found in any module directory.

[2021-04-16 19:29:20.814] [DEBUG] child process exited with code 1
[2021-04-16 19:29:20.818] [ERROR] Unable to parse JSON data from Pester test discovery script. Contents:
[2021-04-16 19:29:20.909] [DEBUG] No test files found.
TylerLeonhardt commented 3 years ago

@jbolton-corvalgroup do you have admin permissions? Can you access the path C:\Program Files\PowerShell\Modules? If so, you could install Pester using:

Install-Module Pester -Scope AllUsers

which you must run from an Administrator PowerShell session.

TylerLeonhardt commented 3 years ago

Also, you could modify the environment variable for your user account:

image

image

blindzero commented 3 years ago

Somehow time fixes a lot...Pester is now found...poss. new environment, new setup, new VSCode, new Pester, ...