Pester icon indicating copy to clipboard operation
Pester copied to clipboard

Add `-Watch` switch

Open dfinke opened this issue 9 years ago • 11 comments

This would enable the watching of file & directory trees, and enable continuous testing at the console.

dfinke avatar Sep 14 '16 17:09 dfinke

Hmm... how do you see this behaving with the various Output options? Separate parameter set that only writes to the console and nothing else? Run until you hit Control+C or something and then output to PassThru / file / etc? Constant output to file?

dlwyatt avatar Sep 14 '16 19:09 dlwyatt

I don't know ;-)

I'm using vs code and have not taken the time to setup a Pester task. I make code changes, switch back to the console, up arrow and press enter. If I had a -watch, it'd be done

dfinke avatar Sep 14 '16 19:09 dfinke

Have you tried https://github.com/smurawski/PowerShellGuard ? Same idea, just a separate module.

dlwyatt avatar Sep 14 '16 20:09 dlwyatt

Yeah, I like things in one place. Figured it be nice feature. No additional dependencies or figuring out how to wire it up, just do a -Watch. Guess I'm spoiled from npm

dfinke avatar Sep 14 '16 22:09 dfinke

If I could vote this up 1000 times...

codialsoftware avatar Dec 30 '16 08:12 codialsoftware

I've written a module to support -Watch like we would have in javascript if anyone wants to use it feel free. I am going to shove in the gallery.

https://github.com/GoEddie/PestWatch

Invoke-PesterWatcher -WatchFolder . -PesterOptions etc

(forwards all pester arguments onto pester)

GoEddie avatar Nov 14 '17 16:11 GoEddie

@GoEddie Thank you for your contribution. There are other projects that do the same or similar thing, but it does not hurt to have another one. If you want you can add your project to our list of projects connected to Pester.

nohwnd avatar Nov 15 '17 06:11 nohwnd

Moving this to 5.x, as mentioned here it is not as practical as it might seem so I'd like to do a bit more testing and thinking.

nohwnd avatar Mar 31 '19 11:03 nohwnd

The Pester Tests VSCode extension has a auto run on save feature.

Won't close this issue as it's editor specific, but it may help some of you. 🙂

fflaten avatar Jun 01 '22 19:06 fflaten

I have tried both https://github.com/smurawski/PowerShellGuard and https://github.com/GoEddie/PestWatch and neither works too well. The former is quite glithy, rerunning tests multiple times on every save, and ignoring the -Output Detailed I try to use with -TestCommand. The latter has the same issue and does not support passing arbitrary arguments either, complaining about Pester 4 deprecated options.

I think that is the issue with delegating what many consider core functionality to other projects - it requires their maintainers to keep up, while neither has been updated in over 5 years.

dkaszews avatar Jul 11 '23 17:07 dkaszews

I ended up using the following script, it should be relatively easy to integrate into Pester:

$Jobs = @()
$Watcher = New-Object 'System.IO.FileSystemWatcher' -Property @{
    Path = Resolve-Path $PSScriptRoot
    Filter = '*.*'
    IncludeSubdirectories = $true
    EnableRaisingEvents = $true
    NotifyFilter = 'FileName'
}

$global:_WatchTestsDebounce = [DateTime]::UnixEpoch
$Action = {
    # Using script-scope just hangs the script
    $Elapsed = ([DateTime]::Now - $global:_WatchTestsDebounce).Seconds

    if ($Elapsed -gt 3) {
        Clear-Host
        Invoke-Pester -Output Detailed | Out-Default
        $global:_WatchTestsDebounce = [DateTime]::Now
    } else {
        Write-Warning "Ignoring debounced event: ${Elapsed}"
    }
}

try {
    $Jobs += Register-ObjectEvent $Watcher 'Created' -Action $Action
    $Jobs += Register-ObjectEvent $Watcher 'Changed' -Action $Action
    & $Action
    while ($true) {
        Wait-Event -Timeout 60
    }
} finally {
    $Jobs | Remove-Job -Force
}

dkaszews avatar Jul 11 '23 18:07 dkaszews