Skip to content

test_runner: attribute global console output to active tests - #65147

Open
hanityx wants to merge 2 commits into
nodejs:mainfrom
hanityx:test-runner-console-attribution
Open

test_runner: attribute global console output to active tests#65147
hanityx wants to merge 2 commits into
nodejs:mainfrom
hanityx:test-runner-console-attribution

Conversation

@hanityx

@hanityx hanityx commented Aug 8, 2026

Copy link
Copy Markdown
Contributor

When tests run under process isolation, output from the built-in global console reaches the parent through the child process's stdout and stderr streams. By the time the parent emits test:stdout or test:stderr, it no longer knows which test produced the write. Custom reporters therefore cannot associate the output with a test, and it can arrive before the matching test:dequeue. Depending on how the pipe is drained, writes from different tests can also be combined into a single raw event.

The test runner installs an internal write hook on the built-in global console in child processes that use the V8 serializer. While a non-root test or suite is active, the formatted message is emitted through its TestsStream with testId, parentId, name, nesting, and source location metadata. These events bypass the per-file declaration-order buffer, so they stay in execution order after the matching test:dequeue.

This follows the approach suggested by cjihrig in #53103 (comment).

context.log() and context.diagnostic() remain the explicit reporting APIs. The hook also covers global console calls made by code under test or third-party modules, without requiring those callers to use the test context APIs. The existing test:stdout and test:stderr event types are preserved.

Scope

  • Only test runner child processes that use the V8 serializer install the hook. Ordinary Node.js processes do not.
  • Output that the hook does not attribute keeps its existing file-scoped path: direct process.stdout and process.stderr writes, native output, separately constructed Console instances, top-level and root-hook output, output written while suites are being defined, and asynchronous output after a test has finished.
  • Suite-owned hooks follow the existing hook-context ownership, so output from a suite's beforeEach() is attributed to that suite.
  • If the source location of an attributed test or suite is unavailable, file falls back to the child's entry file and remains a string.

Tests

  • test/parallel/test-runner-console-output-attribution.mjs
  • all test/parallel/test-runner-* tests
  • make test-only
  • make lint
  • make test-doc

Fixes: #53103

Signed-off-by: hanityx <hanityx@gmail.com>
@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

Review requested:

  • @nodejs/test_runner

@nodejs-github-bot nodejs-github-bot added console Issues and PRs related to the console subsystem. needs-ci PRs that need a full CI run. test_runner Issues and PRs related to the test runner subsystem. labels Aug 8, 2026
Signed-off-by: hanityx <hanityx@gmail.com>
@codecov

codecov Bot commented Aug 8, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 96.11650% with 4 lines in your changes missing coverage. Please review.
✅ Project coverage is 90.32%. Comparing base (c59cd6b) to head (d5a88d0).
⚠️ Report is 8 commits behind head on main.

Files with missing lines Patch % Lines
lib/internal/console/constructor.js 86.20% 4 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #65147      +/-   ##
==========================================
+ Coverage   90.31%   90.32%   +0.01%     
==========================================
  Files         759      759              
  Lines      248301   248425     +124     
  Branches    46864    46887      +23     
==========================================
+ Hits       224246   224394     +148     
- Misses      15471    15475       +4     
+ Partials     8584     8556      -28     
Files with missing lines Coverage Δ
lib/internal/test_runner/harness.js 89.22% <100.00%> (+1.08%) ⬆️
lib/internal/test_runner/runner.js 94.45% <100.00%> (+0.01%) ⬆️
lib/internal/test_runner/tests_stream.js 93.67% <100.00%> (+0.71%) ⬆️
lib/internal/console/constructor.js 97.71% <86.20%> (-0.48%) ⬇️

... and 36 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@MoLow

MoLow commented Aug 9, 2026

Copy link
Copy Markdown
Member

IMHO #64389 is a better approach rather than monkeypatching existing APis

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

Labels

console Issues and PRs related to the console subsystem. needs-ci PRs that need a full CI run. test_runner Issues and PRs related to the test runner subsystem.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

node:test custom reporters get test:stdout and test:stderr events before test:dequeue

3 participants