Skip to content

Replay parallel worker debug output interleaved with tests#2854

Draft
nohwnd wants to merge 1 commit into
mainfrom
nohwnd-parallel-debug-output-warning
Draft

Replay parallel worker debug output interleaved with tests#2854
nohwnd wants to merge 1 commit into
mainfrom
nohwnd-parallel-debug-output-warning

Conversation

@nohwnd

@nohwnd nohwnd commented Jul 9, 2026

Copy link
Copy Markdown
Member

Fix #2825

Problem

When running with Run.Parallel = $true and Output.Verbosity = 'Diagnostic' (or Debug.WriteDebugMessages otherwise enabled), diagnostic output such as Mock debug messages was printed before the test results and detached from the test that produced it, so it was impossible to associate the output with a specific test.

Why it happened

In a parallel run each file executes in its own worker runspace. The worker runs silently (Output.Verbosity = 'None') and records the plugin events onto a tape that the parent replays in test order to produce console output. Debug output, however, went through Write-PesterHostMessage and was written straight to the host as the worker executed — it was never part of the replayed tape. So every worker's debug lines surfaced up front, out of order relative to the per-test output.

Fix

Capture the worker's host/debug output into the same event tape the recorder already uses, and replay it from the parent in tape order. Because each worker runs a single file synchronously, append order is already the correct order, so the debug lines come back interleaved with the per-test output they belong to instead of dumped up front.

  • Output.ps1 — when a worker's parallelOutputTape is set, Write-PesterHostMessage appends the message to the tape (a Step = $null entry carrying the bound parameters) instead of writing to the host live.
  • Pester.Parallel.ps1 — point the tape at the recorder's list so plugin steps and host/debug output share one ordered tape, and split the tape positionally at ContainerDiscoveryEnd so each host entry stays in the phase (discovery vs. run) it was produced in.
  • Main.ps1 — the parent replays host entries (Step = $null) to the real host in position; the earlier warning about uncorrelated debug output is removed since the output is now correlated.

Tests

Replaced the two tests in tst/Pester.RSpec.Parallel.ts.ps1: one runs two files in parallel with Diagnostic output and asserts the debug output is captured and replayed interleaved with each file's tests (on PS7+ it also checks the second file's discovery output lands after the first file's result); the other asserts the run no longer warns about uncorrelated debug output. The full parallel suite (21 tests) and Output.Tests.ps1 (55 tests) pass, and the custom build analyzer reports no new findings in the changed files.

@nohwnd nohwnd added this to the 6.1.0 milestone Jul 9, 2026
@nohwnd nohwnd marked this pull request as draft July 9, 2026 20:22
@nohwnd nohwnd changed the title Warn that debug output cannot be correlated to tests in parallel runs Replay parallel worker debug output interleaved with tests Jul 9, 2026
Debug output (Debug.WriteDebugMessages, enabled e.g. by
Output.Verbosity = 'Diagnostic') was written straight to the host by each
parallel worker as it ran, so it appeared up front, detached from the
test that produced it.

Capture host/debug output into the same event tape the recorder already
uses and replay it in tape order from the parent. Each worker runs one
file synchronously, so append order is the correct order and the debug
lines land interleaved with the per-test output they belong to.

- Output.ps1: Write-PesterHostMessage appends to the worker's tape
  (script-scoped parallelOutputTape) instead of writing live.
- Pester.Parallel.ps1: point the tape at the recorder's list and split
  the tape positionally at ContainerDiscoveryEnd so host entries stay in
  the phase they were produced in.
- Main.ps1: replay host entries (Step = $null) to the real host and drop
  the warning this branch previously added.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@nohwnd nohwnd force-pushed the nohwnd-parallel-debug-output-warning branch from aa07224 to 42ad920 Compare July 9, 2026 21:48
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.

[v6 Parallel] Debug output like Mock printed before relevant test

1 participant