GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — util: fix test to prevent styleText color from changing by islandryu · Pull Request #57935 · nodejs/node

Conversation

@islandryu

@islandryu islandryu commented Apr 19, 2025

Copy link
Copy Markdown
Member

Fixes: #57921

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

regseb reacted with thumbs up emoji bricss reacted with rocket emoji
All reactions
  • 👍 1 reaction
  • 🚀 1 reaction
@islandryu
util: fix test to prevent styleText color from changing
b17d4e0
Fixes: nodejs#57921
@nodejs-github-bot

nodejs-github-bot commented Apr 19, 2025

Copy link
Copy Markdown
Collaborator

Review requested:

  • @nodejs/test_runner
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@nodejs-github-bot nodejs-github-bot added needs-ci PRs that need a full CI run. test_runner Issues and PRs related to the test runner subsystem. util Issues and PRs related to the built-in util module. labels Apr 19, 2025
islandryu
islandryu commented Apr 19, 2025
View reviewed changes
Comment thread lib/internal/test_runner/runner.js Outdated
}
if (opts.root.harness.shouldColorizeTestFiles) {
env.FORCE_COLOR = '1';
env.TEST_REPORT_COLORIZE = '1';

@islandryu islandryu Apr 19, 2025

Copy link
Copy Markdown
Member Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

#57921 (comment)
As stated here, I too don't think we should override the existing FORCE_COLOR by test.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@cjihrig cjihrig Apr 19, 2025 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

We definitely shouldn't be creating new, undocumented environment variables like this. There is already NODE_TEST_CONTEXT, which is documented as something that users should not rely on. We would need to leverage that environment variable.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@islandryu islandryu Apr 20, 2025

Copy link
Copy Markdown
Member Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

I addressed it by setting NOE_TEXT_CONTEXT to child-v8-test-colorize. What do you think?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@cjihrig cjihrig Apr 21, 2025

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Instead of adding a new value for NODE_TEST_CONTEXT, would it work to update shouldColorize() such that:

  • process.env.FORCE_COLOR takes the highest priority if it is set.
  • The TTY logic is evaluated after that.
  • process.env.NODE_TEST_CONTEXT is only considered as a last resort.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

pmarchini reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

@islandryu islandryu Apr 21, 2025 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Would it be something like this?

    if (process.env.FORCE_COLOR !== undefined) {
      return lazyInternalTTY().getColorDepth() > 2;
    }
    if (stream?.isTTY) {
      return typeof stream.getColorDepth === 'function' ?
        stream.getColorDepth() > 2 : true;
    }

    if (process.env.NODE_TEST_CONTEXT === 'child-v8') {
      return lazyInternalTTY().getColorDepth() > 2;
    }

That alone would result in behavior differing from the existing implementation in scenarios such as the following.

console.log({foo: "bar"})
node  --test index.js | cat > text.txt
{ foo: 'bar' }

# fixed
./out/Debug/node  --test index.js | cat > text.txt
{ foo: �[32m'bar'�[39m }

Regardless of the order within shouldColorize, I think the test process needs some way to know the value of opts.root.harness.shouldColorizeTestFiles in the parent process.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@cjihrig cjihrig Apr 23, 2025

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

If you want to start passing parent config through to the child, the NODE_TEST_CONTEXT environment variable is probably the best place to do it.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
cjihrig
cjihrig previously requested changes Apr 19, 2025
View reviewed changes

@cjihrig cjihrig left a comment •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

This FORCE_COLOR change came from #48057. Is it possible to achieve the same result without hacking FORCE_COLOR? The test runner child processes already know that they are test runner child processes. Can we leverage that?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread lib/internal/test_runner/runner.js Outdated
}
if (opts.root.harness.shouldColorizeTestFiles) {
env.FORCE_COLOR = '1';
env.TEST_REPORT_COLORIZE = '1';

@cjihrig cjihrig Apr 19, 2025 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

We definitely shouldn't be creating new, undocumented environment variables like this. There is already NODE_TEST_CONTEXT, which is documented as something that users should not rely on. We would need to leverage that environment variable.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@codecov

codecov Bot commented Apr 19, 2025 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 87.50000% with 5 lines in your changes missing coverage. Please review.
✅ Project coverage is 90.27%. Comparing base (52d95f5) to head (0348c08).
⚠️ Report is 3606 commits behind head on main.

Files with missing lines Patch % Lines
lib/internal/test_runner/runner.js 16.66% 5 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #57935      +/-   ##
==========================================
+ Coverage   90.26%   90.27%   +0.01%     
==========================================
  Files         630      630              
  Lines      185933   186193     +260     
  Branches    36450    36477      +27     
==========================================
+ Hits       167829   168089     +260     
- Misses      10972    10981       +9     
+ Partials     7132     7123       -9     
Files with missing lines Coverage Δ
lib/internal/test_runner/utils.js 60.89% <100.00%> (+0.42%) ⬆️
lib/internal/util/colors.js 100.00% <100.00%> (ø)
lib/util.js 97.78% <100.00%> (+0.02%) ⬆️
lib/internal/test_runner/runner.js 88.46% <16.66%> (-0.41%) ⬇️

... and 55 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.
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@islandryu
use NOE_TEXT_CONTEXT to determine if you should colorize
90924d3
cjihrig
cjihrig reviewed Apr 21, 2025
View reviewed changes
Comment thread lib/internal/util/colors.js
reset: '',
hasColors: false,
shouldColorize(stream) {
shouldColorize(stream, ignoreTestContext = false) {

@cjihrig cjihrig Apr 21, 2025

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Is this new ignoreTestContext argument necessary? It seems like it could be computed within the function based on the value of stream.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@cjihrig
cjihrig self-requested a review April 23, 2025 14:04
@cjihrig
cjihrig dismissed their stale review April 23, 2025 14:07

Dismissing my block because this isn't introducing a new environment variable anymore, but I don't think the current approach is quite correct either. If anything, I'd pass parent settings as a JSON string.

@islandryu
make test context JSON object
0348c08
@islandryu

islandryu commented Apr 25, 2025

Copy link
Copy Markdown
Member Author

I've made a change to store the options in NODE_TEST_CONTEXT as a JSON-formatted string.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@github-actions

github-actions Bot commented Jul 28, 2026

Copy link
Copy Markdown
Contributor

This pull request has been marked as stale due to 90 days of inactivity.
It will be automatically closed in 30 days if no further activity occurs. If this is still relevant, please leave a comment or update it to keep it open.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@github-actions github-actions Bot added the stale Issues and PRs marked stale due to inactivity and scheduled for automatic closure. label Jul 28, 2026
@avivkeller avivkeller closed this Aug 9, 2026
piyushrajyadav added a commit to piyushrajyadav/node that referenced this pull request Aug 26, 2026
@piyushrajyadav
lib,test_runner: fix styleText colorizing non-TTY streams under --test
d734d0c
When
ode --test runs files in process-isolation mode it used to set
FORCE_COLOR=1 in the child's environment so that test-runner output
arrives pre-colored. A side-effect was that util.styleText() inside
the child process would colorize *any* stream, even those with an
explicit isTTY=false, because shouldColorize() short-circuits to
getColorDepth() as soon as FORCE_COLOR is defined.

Fix: stop setting FORCE_COLOR in the child environment. Instead, encode
the parent's colorize decision as a JSON field in NODE_TEST_CONTEXT
(the existing internal-only variable that child processes already use
to identify themselves as test-runner workers):

  NODE_TEST_CONTEXT = JSON.stringify({ context: 'child-v8', colorize: <bool> })

In lib/internal/util/colors.js shouldColorize() is updated to use that
field as a *last-resort* fallback when neither FORCE_COLOR nor
stream.isTTY is truthy. The priority order is now:

  1. FORCE_COLOR (user-controlled, highest priority)
  2. stream.isTTY / stream.getColorDepth()
  3. NODE_TEST_CONTEXT.colorize (test-runner internal, last resort)

lib/internal/test_runner/utils.js and runner.js are updated to parse
the JSON form of NODE_TEST_CONTEXT alongside the legacy plain-string
values ('child' / 'child-v8') for backwards compatibility.

Fixes: nodejs#57921
Refs: nodejs#57935

Signed-off-by: piyushrajyadav <[email protected]>
@piyushrajyadav piyushrajyadav mentioned this pull request Aug 26, 2026
lib,test_runner: fix styleText colorizing non-TTY streams under --test #65558
Open
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@cjihrig cjihrig Awaiting requested review from cjihrig

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

needs-ci PRs that need a full CI run. stale Issues and PRs marked stale due to inactivity and scheduled for automatic closure. test_runner Issues and PRs related to the test runner subsystem. util Issues and PRs related to the built-in util module.

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

styleText(): isTTY check fails with --test

Uh oh!

There was an error while loading. Please reload this page.

4 participants

@islandryu @nodejs-github-bot @cjihrig @avivkeller
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.