TylerLeonhardt / vscode-pester-test-adapter

MIT License
33 stars 13 forks source link

Test Explorer does not show tests #51

Open JFAlbrecht opened 3 years ago

JFAlbrecht commented 3 years ago

Hi..

I can Run Tests from the Test explorer, but the Treeview remains Empty. Screenshot

Atached the Logfile. I hope, you can give me a hint, what I'm doing wrong.

Pester.log

TylerLeonhardt commented 3 years ago

Inside of the log file you'll see the script that we run to generate the tests.

Can you run it yourself and share the errors that it generates?

For your convenience, this is the script:

$Path = @(
    'c:\Temp\Pester.tests.ps1'
'c:\Temp\Get-Planet.Tests.ps1'
)

$VerbosePreference = 'Ignore'
$WarningPreference = 'Ignore'
$DebugPreference = 'Ignore'
Import-Module Pester -MinimumVersion 5.0.0 -ErrorAction Stop
function Discover-Test
{
    [CmdletBinding()]
    param(
        [Parameter(Mandatory)]
        [String[]] $Path,
        [String[]] $ExcludePath
    )
    & (Get-Module Pester) { 
        param (
            $Path, 
            $ExcludePath,
            $SessionState)

        Reset-TestSuiteState
        # to avoid Describe thinking that we run in interactive mode
        $invokedViaInvokePester = $true
        $files = Find-File -Path $Path -ExcludePath $ExcludePath -Extension $PesterPreference.Run.TestExtension.Value
        $containers = foreach ($f in $files) {
            <# HACK: We check to see if there is a single Describe block in the file so that we don't accidentally execute code that shouldn't need to be executed. #>
            if (!(Select-String -Path $f -SimpleMatch 'Describe')) {
                continue
            }
            New-BlockContainerObject -File (Get-Item $f)
        }
        Find-Test -BlockContainer $containers -SessionState $SessionState } -Path $Path -ExcludePath $ExcludePath -SessionState $PSCmdlet.SessionState
}

function New-SuiteObject ($Block) { 
    [PSCustomObject]@{
        type = 'suite'
        id = $Block.ScriptBlock.File + ';' + $Block.StartLine
        file = $Block.ScriptBlock.File
        line = $Block.StartLine - 1
        label = $Block.Name
        children = [Collections.Generic.List[Object]]@()
    }
}

function New-TestObject ($Test) { 
    [PSCustomObject]@{
        type = 'test'
        id = $Test.ScriptBlock.File + ';' + $Test.StartLine
        file = $Test.ScriptBlock.File
        line = $Test.StartLine - 1
        label = $Test.Name
    }
}

function fold ($children, $Block) {
    foreach ($b in $Block.Blocks) { 
        $o = (New-SuiteObject $b)
        $children.Add($o)
        fold $o.children $b
    }

    $hashset = [System.Collections.Generic.HashSet[string]]::new()
    foreach ($t in $Block.Tests) {
        $key = "$($t.ExpandedPath):$($t.StartLine)"
        if ($hashset.Contains($key)) {
            continue
        }
        $children.Add((New-TestObject $t))
        $hashset.Add($key) | Out-Null
    }
    $hashset.Clear() | Out-Null
}

$found = Discover-Test -Path $Path

# whole suite
$suite = [PSCustomObject]@{
    Blocks = [Collections.Generic.List[Object]] $found
    Tests = [Collections.Generic.List[Object]]@()
}

$testSuiteInfo = [PSCustomObject]@{
    type = 'suite'
    id = 'root'
    label = 'Pester'
    children = [Collections.Generic.List[Object]]@()
}

foreach ($file in $found) {
    $fileSuite = [PSCustomObject]@{
        type = 'suite'
        id = $file.BlockContainer.Item.FullName
        file = $file.BlockContainer.Item.FullName
        label = $file.BlockContainer.Item.Name
        children = [Collections.Generic.List[Object]]@()
    }
    $testSuiteInfo.children.Add($fileSuite)
    fold $fileSuite.children $file
}

$testSuiteInfo | ConvertTo-Json -Depth 100
JFAlbrecht commented 3 years ago

Hi Tyler,

Thanks for the quick reply. I think, I tried this already. The result is:

Import-Module : Der Wert "Ignore" wird für ActionPreference-Variablen nicht unterstützt. Der angegebene Wert darf nur als Wert für einen Einstellungsparameter verwendet werden und wurde daher durch den 
Standardwert ersetzt. Weitere Informationen finden Sie im Hilfethema "about_Preference_Variables".
In C:\Temp\Pester.ps1:9 Zeichen:1
+ Import-Module Pester -MinimumVersion 5.0.0 -ErrorAction Stop
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Import-Module], NotSupportedException
    + FullyQualifiedErrorId : System.NotSupportedException,Microsoft.PowerShell.Commands.ImportModuleCommand

Der Ausdruck nach "&" in einem Pipelineelement hat ein ungültiges Objekt erzeugt. Der Ausdruck muss einen Befehlsnamen, Skriptblock oder ein CommandInfo-Objekt ergeben.
In C:\Temp\Pester.ps1:18 Zeichen:7
+     & (Get-Module Pester) {
+       ~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : BadExpression

{
    "type":  "suite",
    "id":  "root",
    "label":  "Pester",
    "children":  [

                 ]
}
JFAlbrecht commented 3 years ago

Hi Tyler,

I did some more testing. I commented the following three lines:

#$VerbosePreference = 'Ignore'
#$WarningPreference = 'Ignore'
#$DebugPreference = 'Ignore'

This resulted in the below Log: It seems, the Script does a little bit more, but now it does not find a function named "Reset-TestSuiteState"

[2021-06-29 15:30:35.550] [INFO] Test Explorer found
[2021-06-29 15:30:35.550] [INFO] Creating adapter for c:\Temp
[2021-06-29 15:30:35.550] [INFO] Initializing Pester adapter
[2021-06-29 15:30:35.550] [INFO] Initializing Pester test runner.
[2021-06-29 15:30:35.552] [INFO] Registering adapter for c:\Temp
[2021-06-29 15:30:35.553] [INFO] Loading Pester tests
[2021-06-29 15:30:35.553] [INFO] Initialization finished
[2021-06-29 15:30:35.857] [DEBUG] Found 2 paths
[2021-06-29 15:30:42.346] [DEBUG] Using Windows PowerShell (x64) at: C:\WINDOWS\System32\WindowsPowerShell\v1.0\powershell.exe
[2021-06-29 15:30:42.346] [DEBUG] 
$Path = @(
    'c:\Temp\Pester.tests.ps1'
'c:\Temp\Get-Planet.Tests.ps1'
)

#$VerbosePreference = 'Ignore'
#$WarningPreference = 'Ignore'
#$DebugPreference = 'Ignore'
Import-Module Pester -MinimumVersion 5.0.0 -ErrorAction Stop
function Discover-Test
{
    [CmdletBinding()]
    param(
        [Parameter(Mandatory)]
        [String[]] $Path,
        [String[]] $ExcludePath
    )
    & (Get-Module Pester) { 
        param (
            $Path, 
            $ExcludePath,
            $SessionState)

        Reset-TestSuiteState
        # to avoid Describe thinking that we run in interactive mode
        $invokedViaInvokePester = $true
        $files = Find-File -Path $Path -ExcludePath $ExcludePath -Extension $PesterPreference.Run.TestExtension.Value
        $containers = foreach ($f in $files) {
            <# HACK: We check to see if there is a single Describe block in the file so that we don't accidentally execute code that shouldn't need to be executed. #>
            if (!(Select-String -Path $f -SimpleMatch 'Describe')) {
                continue
            }
            New-BlockContainerObject -File (Get-Item $f)
        }
        Find-Test -BlockContainer $containers -SessionState $SessionState } -Path $Path -ExcludePath $ExcludePath -SessionState $PSCmdlet.SessionState
}

function New-SuiteObject ($Block) { 
    [PSCustomObject]@{
        type = 'suite'
        id = $Block.ScriptBlock.File + ';' + $Block.StartLine
        file = $Block.ScriptBlock.File
        line = $Block.StartLine - 1
        label = $Block.Name
        children = [Collections.Generic.List[Object]]@()
    }
}

function New-TestObject ($Test) { 
    [PSCustomObject]@{
        type = 'test'
        id = $Test.ScriptBlock.File + ';' + $Test.StartLine
        file = $Test.ScriptBlock.File
        line = $Test.StartLine - 1
        label = $Test.Name
    }
}

function fold ($children, $Block) {
    foreach ($b in $Block.Blocks) { 
        $o = (New-SuiteObject $b)
        $children.Add($o)
        fold $o.children $b
    }

    $hashset = [System.Collections.Generic.HashSet[string]]::new()
    foreach ($t in $Block.Tests) {
        $key = "$($t.ExpandedPath):$($t.StartLine)"
        if ($hashset.Contains($key)) {
            continue
        }
        $children.Add((New-TestObject $t))
        $hashset.Add($key) | Out-Null
    }
    $hashset.Clear() | Out-Null
}

$found = Discover-Test -Path $Path

# whole suite
$suite = [PSCustomObject]@{
    Blocks = [Collections.Generic.List[Object]] $found
    Tests = [Collections.Generic.List[Object]]@()
}

$testSuiteInfo = [PSCustomObject]@{
    type = 'suite'
    id = 'root'
    label = 'Pester'
    children = [Collections.Generic.List[Object]]@()
}

foreach ($file in $found) {
    $fileSuite = [PSCustomObject]@{
        type = 'suite'
        id = $file.BlockContainer.Item.FullName
        file = $file.BlockContainer.Item.FullName
        label = $file.BlockContainer.Item.Name
        children = [Collections.Generic.List[Object]]@()
    }
    $testSuiteInfo.children.Add($fileSuite)
    fold $fileSuite.children $file
}

$testSuiteInfo | ConvertTo-Json -Depth 100

[2021-06-29 15:30:44.912] [ERROR] stderr: Reset-TestSuiteState : Die Benennung "Reset-TestSuiteState" wurde nicht als Name eines Cmdlet, einer Funktion, einer 

[2021-06-29 15:30:44.912] [ERROR] stderr: Skriptdatei oder eines ausf�hrbaren Programms erkannt. �berpr�fen Sie die Schreibweise des Namens, oder ob der Pfad 
korrekt ist (sofern enthalten), und wiederholen Sie den Vorgang.
In Zeile:25 Zeichen:9

[2021-06-29 15:30:44.912] [ERROR] stderr: +         Reset-TestSuiteState
+         ~~~~~~~~~~~~~~~~~~~~

[2021-06-29 15:30:44.913] [ERROR] stderr:     + CategoryInfo          : ObjectNotFound: (Reset-TestSuiteState:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

[2021-06-29 15:30:46.711] [DEBUG] stdout: {
    "type":  "suite",
    "id":  "root",
    "label":  "Pester",
    "children":  [
                     {
                         "type":  "suite",
                         "id":  null,
                         "file":  null,
                         "label":  null,
                         "children":  [
                                          {
                                              "type":  "suite",
                                              "id":  "C:\\Temp\\Pester.tests.ps1;29",
                                              "file":  "C:\\Temp\\Pester.tests.ps1",
                                              "line":  28,
                                              "label":  "BuildIfChanged",
                                              "children":  [
                                                               {
                                                                   "type":  "suite",
                                                                   "id":  "C:\\Temp\\Pester.tests.ps1;30",
                                                                   "file":  "C:\\Temp\\Pester.tests.ps1",
                                                                   "line":  29,
                                                                   "label":  "When there are Changes",
                                                                   "children":  [
                                                                                    {
                                                                                        "type":  "test",
                                                                                        "id":  "C:\\Temp\\Pester.tests.ps1;39",

[2021-06-29 15:30:46.711] [DEBUG] stdout:                                                        "file":  "C:\\Temp\\Pester.tests.ps1",
                                                                                        "line":  38,
                                                                                        "label":  "Builds the next version"
                                                                                    },
                                                                                    {
                                                                                        "type":  "test",
                                                                                        "id":  "C:\\Temp\\Pester.tests.ps1;43",
                                                                                        "file":  "C:\\Temp\\Pester.tests.ps1",
                                                                                        "line":  42,
                                                                                        "label":  "returns the next version number"
                                                                                    }
                                                                                ]
                                                               },
                                                               {
                                                                   "type":  "suite",
                                                                   "id":  "C:\\Temp\\Pester.tests.ps1;47",
                                                                   "file":  "C:\\Temp\\Pester.tests.ps1",
                                                                   "line":  46,
                                                                   "label":  "When there are no Changes",
                                                                   "children":  [
                                                                                    {
                                                                                        "type":  "test",
                                                                                        "id":  "C:\\Temp\\Pester.tests.ps1;56",
                                                                                        "file":  "C:\\Temp\\Pester.tests.ps1",
                                                                                        "line":  55,
                                                                                        "label":  "Should not build the next version"
                                                                                    }
                                                                                ]
                                                               }
                                                           ]
                                          }
                                      ]
                     },
                     {
                         "type":  "suite",
                         "id":  null,
                         "file":  null,
                         "label":  null,
                         "children":  [
                                          {
                                              "type":  "suite",
                                              "id":  "C:\\Temp\\Get-Planet.Tests.ps1;5",
                                              "file":  "C:\\Temp\\Get-Planet.Tests.ps1",
                                              "line":  4,
                                              "label":  "Get-Planet",
                                              "children":  [
                                                               {
                                                                   "type":  "test",
                                                                   "id":  "C:\\Temp\\Get-Planet.Tests.ps1;6",
                                                                   "file":  "C:\\Temp\\Get-Planet.Tests.ps1",
                                                                   "line":  5,
                                                                   "label":  "Given no parameters, it lists all 8 planets"
                                                               },
                                                               {
                                                                   "type":  "test",
                                                                   "id":  "C:\\Temp\\Get-Planet.Tests.ps1;10",
                                                                   "file":  "C:\\Temp\\Get-Planet.Tests.ps1",
                                                                   "line":  9,
                                                                   "label":  "Earth is the third planet in our Solar System"
                                                               },
                                                               {
                                                                   "type":  "test",
                                                                   "id":  "C:\\Temp\\Get-Planet.Tests.ps1;14",
                                                                   "file":  "C:\\Temp\\Get-Planet.Tests.ps1",
                                                                   "line":  13,
                                                                   "label":  "Pluto is not part of our Solar System"
                                                               },
                                                               {
                                                                   "type":  "test",
                                                                   "id":  "C:\\Temp\\Get-Planet.Tests.ps1;19",
                                                                   "file":  "C:\\Temp\\Get-Planet.Tests.ps1",
                                                                   "line":  18,
                                                                   "label":  "Planets have this order: Mercury, Venus, Earth, Mars, Jupiter, Saturn, Uranus, Neptune"
                                                               }
                                                           ]
                                          }
                                      ]
                     }
                 ]
}

[2021-06-29 15:30:46.860] [DEBUG] child process exited with code 0
[2021-06-29 15:30:46.861] [ERROR] Unable to parse JSON data from Pester test discovery script. Contents: {
    "type":  "suite",
    "id":  "root",
    "label":  "Pester",
    "children":  [
                     {
                         "type":  "suite",
                         "id":  null,
                         "file":  null,
                         "label":  null,
                         "children":  [
                                          {
                                              "type":  "suite",
                                              "id":  "C:\\Temp\\Pester.tests.ps1;29",
                                              "file":  "C:\\Temp\\Pester.tests.ps1",
                                              "line":  28,
                                              "label":  "BuildIfChanged",
                                              "children":  [
                                                               {
                                                                   "type":  "suite",
                                                                   "id":  "C:\\Temp\\Pester.tests.ps1;30",
                                                                   "file":  "C:\\Temp\\Pester.tests.ps1",
                                                                   "line":  29,
                                                                   "label":  "When there are Changes",
                                                                   "children":  [
                                                                                    {
                                                                                        "type":  "test",
                                                                                        "id":  "C:\\Temp\\Pester.tests.ps1;39",
                                                                                        "file":  "C:\\Temp\\Pester.tests.ps1",
                                                                                        "line":  38,
                                                                                        "label":  "Builds the next version"
                                                                                    },
                                                                                    {
                                                                                        "type":  "test",
                                                                                        "id":  "C:\\Temp\\Pester.tests.ps1;43",
                                                                                        "file":  "C:\\Temp\\Pester.tests.ps1",
                                                                                        "line":  42,
                                                                                        "label":  "returns the next version number"
                                                                                    }
                                                                                ]
                                                               },
                                                               {
                                                                   "type":  "suite",
                                                                   "id":  "C:\\Temp\\Pester.tests.ps1;47",
                                                                   "file":  "C:\\Temp\\Pester.tests.ps1",
                                                                   "line":  46,
                                                                   "label":  "When there are no Changes",
                                                                   "children":  [
                                                                                    {
                                                                                        "type":  "test",
                                                                                        "id":  "C:\\Temp\\Pester.tests.ps1;56",
                                                                                        "file":  "C:\\Temp\\Pester.tests.ps1",
                                                                                        "line":  55,
                                                                                        "label":  "Should not build the next version"
                                                                                    }
                                                                                ]
                                                               }
                                                           ]
                                          }
                                      ]
                     },
                     {
                         "type":  "suite",
                         "id":  null,
                         "file":  null,
                         "label":  null,
                         "children":  [
                                          {
                                              "type":  "suite",
                                              "id":  "C:\\Temp\\Get-Planet.Tests.ps1;5",
                                              "file":  "C:\\Temp\\Get-Planet.Tests.ps1",
                                              "line":  4,
                                              "label":  "Get-Planet",
                                              "children":  [
                                                               {
                                                                   "type":  "test",
                                                                   "id":  "C:\\Temp\\Get-Planet.Tests.ps1;6",
                                                                   "file":  "C:\\Temp\\Get-Planet.Tests.ps1",
                                                                   "line":  5,
                                                                   "label":  "Given no parameters, it lists all 8 planets"
                                                               },
                                                               {
                                                                   "type":  "test",
                                                                   "id":  "C:\\Temp\\Get-Planet.Tests.ps1;10",
                                                                   "file":  "C:\\Temp\\Get-Planet.Tests.ps1",
                                                                   "line":  9,
                                                                   "label":  "Earth is the third planet in our Solar System"
                                                               },
                                                               {
                                                                   "type":  "test",
                                                                   "id":  "C:\\Temp\\Get-Planet.Tests.ps1;14",
                                                                   "file":  "C:\\Temp\\Get-Planet.Tests.ps1",
                                                                   "line":  13,
                                                                   "label":  "Pluto is not part of our Solar System"
                                                               },
                                                               {
                                                                   "type":  "test",
                                                                   "id":  "C:\\Temp\\Get-Planet.Tests.ps1;19",
                                                                   "file":  "C:\\Temp\\Get-Planet.Tests.ps1",
                                                                   "line":  18,
                                                                   "label":  "Planets have this order: Mercury, Venus, Earth, Mars, Jupiter, Saturn, Uranus, Neptune"
                                                               }
                                                           ]
                                          }
                                      ]
                     }
                 ]
}

[2021-06-29 15:31:01.667] [INFO] Running Pester tests ["root"]
JFAlbrecht commented 3 years ago

Hi... Me again. I read Issue #46 and rolled back to pester 5.1.1. Now, the errormessage about the reset-testsuite command is gone. However the Message "Unable to parse JSON Data" still remains and the test View is still empty

[2021-06-30 06:19:35.156] [DEBUG] child process exited with code 0
[2021-06-30 06:19:35.158] [ERROR] Unable to parse JSON data from Pester test discovery script. Contents: {
"type": "suite",
"id": "root",
"label": "Pester",
"children": [
{
"type": "suite",
"id": "C:\Temp\Pester.tests.ps1",
"file": "C:\Temp\Pester.tests.ps1",
"label": "Pester.tests.ps1",
"children": [
{
"type": "suite",
"id": "C:\Temp\Pester.tests.ps1;29",
"file": "C:\Temp\Pester.tests.ps1",
"line": 28,
"label": "BuildIfChanged",
"children": [
{
"type": "suite",
"id": "C:\Temp\Pester.tests.ps1;30",
"file": "C:\Temp\Pester.tests.ps1",
"line": 29,
"label": "When there are Changes",
"children": [
{
"type": "test",
"id": "C:\Temp\Pester.tests.ps1;39",
"file": "C:\Temp\Pester.tests.ps1",
"line": 38,
"label": "Builds the next version"
},
{
"type": "test",
"id": "C:\Temp\Pester.tests.ps1;43",
"file": "C:\Temp\Pester.tests.ps1",
"line": 42,
"label": "returns the next version number"
}
]
},
{
"type": "suite",
"id": "C:\Temp\Pester.tests.ps1;47",
"file": "C:\Temp\Pester.tests.ps1",
"line": 46,
"label": "When there are no Changes",
"children": [
{
"type": "test",
"id": "C:\Temp\Pester.tests.ps1;56",
"file": "C:\Temp\Pester.tests.ps1",
"line": 55,
"label": "Should not build the next version"
}
]
}
]
}
]
},
{
"type": "suite",
"id": "C:\Temp\Get-Planet.Tests.ps1",
"file": "C:\Temp\Get-Planet.Tests.ps1",
"label": "Get-Planet.Tests.ps1",
"children": [
{
"type": "suite",
"id": "C:\Temp\Get-Planet.Tests.ps1;5",
"file": "C:\Temp\Get-Planet.Tests.ps1",
"line": 4,
"label": "Get-Planet",
"children": [
{
"type": "test",
"id": "C:\Temp\Get-Planet.Tests.ps1;6",
"file": "C:\Temp\Get-Planet.Tests.ps1",
"line": 5,
"label": "Given no parameters, it lists all 8 planets"
},
{
"type": "test",
"id": "C:\Temp\Get-Planet.Tests.ps1;10",
"file": "C:\Temp\Get-Planet.Tests.ps1",
"line": 9,
"label": "Earth is the third planet in our Solar System"
},
{
"type": "test",
"id": "C:\Temp\Get-Planet.Tests.ps1;14",
"file": "C:\Temp\Get-Planet.Tests.ps1",
"line": 13,
"label": "Pluto is not part of our Solar System"
},
{
"type": "test",
"id": "C:\Temp\Get-Planet.Tests.ps1;19",
"file": "C:\Temp\Get-Planet.Tests.ps1",
"line": 18,
"label": "Planets have this order: Mercury, Venus, Earth, Mars, Jupiter, Saturn, Uranus,
Neptune" }
]
}
]
}
]
}

mtonnes-bc commented 3 years ago

I'm experiencing the same issue -- attaching my own log. Pester2.log

craiglemon commented 3 years ago

I am having the exact same issue, and also have a fix. I will submit a PR relating to this issue shortly