Skip to content

fix(jest-console): buffer console output in CustomConsole for reporters#16155

Open
ahnpnl wants to merge 1 commit into
jestjs:mainfrom
ahnpnl:fix/6441-console-buffer
Open

fix(jest-console): buffer console output in CustomConsole for reporters#16155
ahnpnl wants to merge 1 commit into
jestjs:mainfrom
ahnpnl:fix/6441-console-buffer

Conversation

@ahnpnl
Copy link
Copy Markdown
Contributor

@ahnpnl ahnpnl commented May 10, 2026

Summary

Fixes #6441

When running a single test file, Jest forces verbose: true (source), which causes runTest to use CustomConsole instead of BufferedConsole. CustomConsole.getBuffer() returned undefined, so TestResult.console was never populated — reporters (including custom reporters) received no console data.

This PR adds buffer tracking to CustomConsole so it both:

  • Prints live to stdout (preserving the existing verbose UX)
  • Buffers entries so getBuffer() returns a ConsoleBuffer, populating TestResult.console for reporters

Changes

  • packages/jest-console/src/CustomConsole.ts:

    • Added _buffer: ConsoleBuffer field
    • Added _addToBuffer() private method that captures stack origin (matching BufferedConsole.write behavior)
    • _log() and _logError() now also write to the buffer
    • getBuffer() returns the buffer (instead of always undefined)
  • packages/jest-reporters/src/DefaultReporter.ts:

    • Skip printing the ● Console section when the effective verbose setting (config.verbose ?? globalConfig.verbose) is true, since CustomConsole already prints output inline — avoids double-printing
  • packages/jest-console/src/__tests__/CustomConsole.test.ts:

    • Added 6 unit tests covering getBuffer() behavior
  • e2e/__tests__/__snapshots__/executeTestsOnceInMpr.ts.snap:

    • Updated snapshot to reflect new behavior

Test plan

  • All affected e2e tests pass: console.test.ts, consoleDebugging.test.ts, consoleLogOutputWhenRunInBand.test.ts, testRetries.test.ts, jest.config.ts.test.ts, jest.config.js.test.ts, executeTestsOnceInMpr.ts
  • Unit tests pass: yarn jest packages/jest-console --no-coverage
  • Lint passes: yarn lint
  • Build succeeds: yarn build

@netlify
Copy link
Copy Markdown

netlify Bot commented May 10, 2026

Deploy Preview for jestjs ready!

Built without sensitive environment variables

Name Link
🔨 Latest commit 59f9bc1
🔍 Latest deploy log https://app.netlify.com/projects/jestjs/deploys/6a0dd3db69ca5900086a37a9
😎 Deploy Preview https://deploy-preview-16155--jestjs.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

@ahnpnl ahnpnl force-pushed the fix/6441-console-buffer branch from 8bf58d0 to 9235c0a Compare May 10, 2026 06:49
@pkg-pr-new
Copy link
Copy Markdown

pkg-pr-new Bot commented May 10, 2026

Open in StackBlitz

babel-jest

npm i https://pkg.pr.new/babel-jest@16155

babel-plugin-jest-hoist

npm i https://pkg.pr.new/babel-plugin-jest-hoist@16155

babel-preset-jest

npm i https://pkg.pr.new/babel-preset-jest@16155

create-jest

npm i https://pkg.pr.new/create-jest@16155

@jest/diff-sequences

npm i https://pkg.pr.new/@jest/diff-sequences@16155

expect

npm i https://pkg.pr.new/expect@16155

@jest/expect-utils

npm i https://pkg.pr.new/@jest/expect-utils@16155

jest

npm i https://pkg.pr.new/jest@16155

jest-changed-files

npm i https://pkg.pr.new/jest-changed-files@16155

jest-circus

npm i https://pkg.pr.new/jest-circus@16155

jest-cli

npm i https://pkg.pr.new/jest-cli@16155

jest-config

npm i https://pkg.pr.new/jest-config@16155

@jest/console

npm i https://pkg.pr.new/@jest/console@16155

@jest/core

npm i https://pkg.pr.new/@jest/core@16155

@jest/create-cache-key-function

npm i https://pkg.pr.new/@jest/create-cache-key-function@16155

jest-diff

npm i https://pkg.pr.new/jest-diff@16155

jest-docblock

npm i https://pkg.pr.new/jest-docblock@16155

jest-each

npm i https://pkg.pr.new/jest-each@16155

@jest/environment

npm i https://pkg.pr.new/@jest/environment@16155

jest-environment-jsdom

npm i https://pkg.pr.new/jest-environment-jsdom@16155

@jest/environment-jsdom-abstract

npm i https://pkg.pr.new/@jest/environment-jsdom-abstract@16155

jest-environment-node

npm i https://pkg.pr.new/jest-environment-node@16155

@jest/expect

npm i https://pkg.pr.new/@jest/expect@16155

@jest/fake-timers

npm i https://pkg.pr.new/@jest/fake-timers@16155

@jest/get-type

npm i https://pkg.pr.new/@jest/get-type@16155

@jest/globals

npm i https://pkg.pr.new/@jest/globals@16155

jest-haste-map

npm i https://pkg.pr.new/jest-haste-map@16155

jest-jasmine2

npm i https://pkg.pr.new/jest-jasmine2@16155

jest-leak-detector

npm i https://pkg.pr.new/jest-leak-detector@16155

jest-matcher-utils

npm i https://pkg.pr.new/jest-matcher-utils@16155

jest-message-util

npm i https://pkg.pr.new/jest-message-util@16155

jest-mock

npm i https://pkg.pr.new/jest-mock@16155

@jest/pattern

npm i https://pkg.pr.new/@jest/pattern@16155

jest-phabricator

npm i https://pkg.pr.new/jest-phabricator@16155

jest-regex-util

npm i https://pkg.pr.new/jest-regex-util@16155

@jest/reporters

npm i https://pkg.pr.new/@jest/reporters@16155

jest-resolve

npm i https://pkg.pr.new/jest-resolve@16155

jest-resolve-dependencies

npm i https://pkg.pr.new/jest-resolve-dependencies@16155

jest-runner

npm i https://pkg.pr.new/jest-runner@16155

jest-runtime

npm i https://pkg.pr.new/jest-runtime@16155

@jest/schemas

npm i https://pkg.pr.new/@jest/schemas@16155

jest-snapshot

npm i https://pkg.pr.new/jest-snapshot@16155

@jest/snapshot-utils

npm i https://pkg.pr.new/@jest/snapshot-utils@16155

@jest/source-map

npm i https://pkg.pr.new/@jest/source-map@16155

@jest/test-result

npm i https://pkg.pr.new/@jest/test-result@16155

@jest/test-sequencer

npm i https://pkg.pr.new/@jest/test-sequencer@16155

@jest/transform

npm i https://pkg.pr.new/@jest/transform@16155

@jest/types

npm i https://pkg.pr.new/@jest/types@16155

jest-util

npm i https://pkg.pr.new/jest-util@16155

jest-validate

npm i https://pkg.pr.new/jest-validate@16155

jest-watcher

npm i https://pkg.pr.new/jest-watcher@16155

jest-worker

npm i https://pkg.pr.new/jest-worker@16155

pretty-format

npm i https://pkg.pr.new/pretty-format@16155

commit: 59f9bc1

@ahnpnl ahnpnl force-pushed the fix/6441-console-buffer branch 3 times, most recently from f91156c to 2da52ad Compare May 10, 2026 08:25
@ahnpnl ahnpnl marked this pull request as ready for review May 10, 2026 09:02
Copilot AI review requested due to automatic review settings May 10, 2026 09:02
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes a long-standing gap where, in verbose mode (notably when Jest forces verbose: true for single-file runs), CustomConsole did not provide a console buffer—so TestResult.console stayed empty and reporters couldn’t access console output. The change makes CustomConsole both print live output and also buffer entries so reporters can consume them.

Changes:

  • Add ConsoleBuffer tracking to CustomConsole and implement getBuffer() to return buffered entries.
  • Update DefaultReporter to avoid printing the buffered “Console” section when running in verbose mode.
  • Add unit tests for CustomConsole.getBuffer() behavior and add a changelog entry.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.

File Description
packages/jest-reporters/src/DefaultReporter.ts Adds a guard to avoid printing the buffered Console section in verbose mode (but the guard currently uses only global verbose).
packages/jest-console/src/CustomConsole.ts Implements buffering in CustomConsole (tracks messages + origin stack) and returns it via getBuffer().
packages/jest-console/src/tests/CustomConsole.test.ts Adds coverage verifying getBuffer() returns undefined when empty and buffers log/error/warn while still printing live.
CHANGELOG.md Documents the fix under ## main### Fixes.

Comment thread packages/jest-reporters/src/DefaultReporter.ts Outdated
@ahnpnl ahnpnl force-pushed the fix/6441-console-buffer branch 3 times, most recently from ebe47f2 to 1b6b435 Compare May 12, 2026 12:37
@ahnpnl ahnpnl force-pushed the fix/6441-console-buffer branch from 1b6b435 to 8b591b5 Compare May 17, 2026 10:09
CustomConsole.getBuffer() returned undefined, causing TestResult.console
to be empty when verbose mode was active. Reporters relying on
TestResult.console (e.g. custom reporters) received no console data.

Added buffer tracking to CustomConsole so it both prints live to stdout
(preserving verbose UX) and populates the buffer for reporters.

Closes jestjs#6441
@ahnpnl ahnpnl force-pushed the fix/6441-console-buffer branch from 8b591b5 to 59f9bc1 Compare May 20, 2026 15:31
@github-actions github-actions Bot added the require-changelog If a PR does requires a changelog entry label May 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

require-changelog If a PR does requires a changelog entry

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Allow buffering console when running a single test

2 participants