Skip to content

Instantly share code, notes, and snippets.

@nohwnd
Last active October 29, 2021 12:39
Show Gist options
  • Save nohwnd/efc339339dc328d93e0fe000249aea25 to your computer and use it in GitHub Desktop.
Save nohwnd/efc339339dc328d93e0fe000249aea25 to your computer and use it in GitHub Desktop.
Use Pester code coverage with CoverageGutters in VSCode
coverage.xml
// put into directory .vscode and remove this comment
{
"version": "0.2.0",
"configurations": [
{
"name": "Run tests in current file with CC",
"type": "PowerShell",
"request": "launch",
"script": "${workspaceFolder}/test.ps1",
"args": [
"-Path", "'${file}'",
"-Output", "${config:powershell.pester.debugOutputVerbosity}",
"-CodeCoverage",
],
"cwd": "${file}",
}
]
}
// put into directory .vscode and remove this comment
{
// disable gutters to make breakpoints usable
"coverage-gutters.showGutterCoverage": false,
"coverage-gutters.showLineCoverage": true
}
param (
[String] $Path = ".",
[String] $Output = "Detailed",
[Switch] $CodeCoverage
)
Import-Module Pester -MinimumVersion 5.2.0
$Configuration = [PesterConfiguration]::Default
$Configuration.Run.Path = $Path
$Configuration.Output.Verbosity = $Output
$Configuration.CodeCoverage.Enabled = [bool] $CodeCoverage
# CoverageGutters is new option in Pester 5.2
$Configuration.CodeCoverage.OutputFormat = "CoverageGutters"
$Configuration.CodeCoverage.Path = "$PSScriptRoot/src"
$Configuration.CodeCoverage.OutputPath = "$PSScriptRoot/coverage.xml"
$Configuration.CodeCoverage.CoveragePercentTarget = 90
$Configuration.Debug.WriteDebugMessages = $true
$Configuration.Debug.WriteDebugMessagesFrom = "CodeCoverage"
# for convenience just run the passed file when we invoke this via launchConfig.json when
# the file is a non-test file
$isDirectory = (Get-Item $Path).PSIsContainer
$isTestFile = $Path.EndsWith($Configuration.Run.TestExtension.Value)
if (-not $isDirectory -and -not $isTestFile) {
& $Path
return
}
Invoke-Pester -Configuration $Configuration
@davidwallis
Copy link

Will prob work better as I see an odd issue where I end up with $path being a path as a string, and then times an object with a path with a property and then EndsWith isn't a valid method when its a null value. I just haven't quite got to the bottom of it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment