Skip to content

Add per-file discovery and run mode (fixes #2723)#2725

Draft
nohwnd wants to merge 1 commit into
mainfrom
fix/2723-per-file-execution
Draft

Add per-file discovery and run mode (fixes #2723)#2725
nohwnd wants to merge 1 commit into
mainfrom
fix/2723-per-file-execution

Conversation

@nohwnd

@nohwnd nohwnd commented Jun 17, 2026

Copy link
Copy Markdown
Member

Adds a Run.PerFileDiscovery option (default true) that discovers and runs each test container one at a time instead of discovering all containers first, then running all.

Why: The current batch model (discover all → run all) accumulates module state and scope pollution across containers during discovery. Per-file mode isolates each container: discover it, run it, release it, move to the next.

What changed:

  • New Run.PerFileDiscovery boolean option (default true)
  • Refactored Invoke-Test into Invoke-TestBatch (original flow) and Invoke-TestPerFile (new flow)
  • Invoke-ContainerDiscovery extracted as a single-container discovery helper
  • Global plugin hooks (DiscoveryStart/End, RunStart/End) fire once around the full run
  • Per-container hooks fire per container as before
  • Block tree references released after each container run to reduce peak memory

Fallback: Set Run.PerFileDiscovery = $false to revert to the original batch behavior.

All 2285 tests pass in both modes.

Adds Run.PerFileDiscovery option (default true) that discovers and runs
each test container one at a time instead of discovering all first.
This improves container isolation and reduces peak memory usage by
releasing block trees after each container completes.

The original batch behavior is preserved as a fallback when
PerFileDiscovery is set to false.

Refactored Invoke-Test into three functions:
- Invoke-Test: dispatches based on PerFileDiscovery setting
- Invoke-TestBatch: original discover-all-then-run-all flow
- Invoke-TestPerFile: per-container discover-then-run loop
- Invoke-ContainerDiscovery: single-container discovery helper

Global plugin hooks (DiscoveryStart/End, RunStart/End) fire once
around the full run. Per-container hooks (ContainerDiscoveryStart/End,
ContainerRunStart/End) fire as before.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Comment thread src/Pester.Runtime.ps1
$steps = $state.Plugin.RunStart
if ($null -ne $steps -and 0 -lt @($steps).Count) {
Invoke-PluginStep -Plugins $state.Plugin -Step RunStart -Context @{
Blocks = @($allDiscovered | & $SafeCommands['ForEach-Object'] { $_.Block })
@nohwnd nohwnd marked this pull request as ready for review June 17, 2026 13:17
@nohwnd nohwnd marked this pull request as draft June 17, 2026 13:38
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

Successfully merging this pull request may close these issues.

2 participants