GeezerVison is showing github.com — util: fix test to prevent styleText color from changing by islandryu · Pull Request #57935 · nodejs/node
Conversation
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
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
|
|
}
|
|
|
if (opts.root.harness.shouldColorizeTestFiles) {
|
|
|
env.FORCE_COLOR = '1';
|
|
|
env.TEST_REPORT_COLORIZE = '1';
|
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.
cjihrig
Apr 19, 2025
•
edited
Loading
Uh oh!
There was an error while loading. Please reload this page.
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.
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.
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
islandryu
Apr 21, 2025
•
edited
Loading
Uh oh!
There was an error while loading. Please reload this page.
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.
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.
cjihrig
left a comment
•
edited
Loading
Uh oh!
There was an error while loading. Please reload this page.
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.
|
|
}
|
|
|
if (opts.root.harness.shouldColorizeTestFiles) {
|
|
|
env.FORCE_COLOR = '1';
|
|
|
env.TEST_REPORT_COLORIZE = '1';
|
cjihrig
Apr 19, 2025
•
edited
Loading
Uh oh!
There was an error while loading. Please reload this page.
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.
codecov
Bot
commented
Apr 19, 2025
•
edited
Loading
Uh oh!
There was an error while loading. Please reload this page.
|
❌ 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.
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
... 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.
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
|
|
reset: '',
|
|
|
hasColors: false,
|
|
|
shouldColorize(stream) {
|
|
|
shouldColorize(stream, ignoreTestContext = false) {
|
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.
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.
|
I've made a change to store the options in NODE_TEST_CONTEXT as a JSON-formatted string.
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
|
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.
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
piyushrajyadav
added a commit
to piyushrajyadav/node
that referenced
this pull request
Aug 26, 2026
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]>
Reviewers
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.
Milestone
No milestone
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.
|