Skip to content

Commit d33eaf2

Browse files
pmarchiniaduh95
authored andcommittedFeb 6, 2025
lib: ensure FORCE_COLOR forces color output in non-TTY environments
PR-URL: #55404 Backport-PR-URL: #56631 Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Jake Yuesong Li <jake.yuesong@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Chemi Atlow <chemi@atlow.co.il> Reviewed-By: Moshe Atlow <moshe@atlow.co.il> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 299d6fa commit d33eaf2

File tree

6 files changed

+79
-12
lines changed

6 files changed

+79
-12
lines changed
 

‎lib/internal/util/colors.js

+10-12
Original file line numberDiff line numberDiff line change
@@ -24,18 +24,16 @@ module.exports = {
2424
stream.getColorDepth() > 2 : true);
2525
},
2626
refresh() {
27-
if (process.stderr.isTTY) {
28-
const hasColors = module.exports.shouldColorize(process.stderr);
29-
module.exports.blue = hasColors ? '\u001b[34m' : '';
30-
module.exports.green = hasColors ? '\u001b[32m' : '';
31-
module.exports.white = hasColors ? '\u001b[39m' : '';
32-
module.exports.yellow = hasColors ? '\u001b[33m' : '';
33-
module.exports.red = hasColors ? '\u001b[31m' : '';
34-
module.exports.gray = hasColors ? '\u001b[90m' : '';
35-
module.exports.clear = hasColors ? '\u001bc' : '';
36-
module.exports.reset = hasColors ? '\u001b[0m' : '';
37-
module.exports.hasColors = hasColors;
38-
}
27+
const hasColors = module.exports.shouldColorize(process.stderr);
28+
module.exports.blue = hasColors ? '\u001b[34m' : '';
29+
module.exports.green = hasColors ? '\u001b[32m' : '';
30+
module.exports.white = hasColors ? '\u001b[39m' : '';
31+
module.exports.yellow = hasColors ? '\u001b[33m' : '';
32+
module.exports.red = hasColors ? '\u001b[31m' : '';
33+
module.exports.gray = hasColors ? '\u001b[90m' : '';
34+
module.exports.clear = hasColors ? '\u001bc' : '';
35+
module.exports.reset = hasColors ? '\u001b[0m' : '';
36+
module.exports.hasColors = hasColors;
3937
},
4038
};
4139

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import assert from 'node:assert/strict'
2+
import { test } from 'node:test'
3+
4+
test('failing assertion', () => {
5+
assert.strictEqual('!Hello World', 'Hello World!')
6+
})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
[31m✖ failing assertion [90m(*ms)[39m[39m
2+
[AssertionError [ERR_ASSERTION]: Expected values to be strictly equal:
3+
[32mactual[39m [31mexpected[39m
4+
5+
[39m'[39m[32m![39m[39mH[39m[39me[39m[39ml[39m[39ml[39m[39mo[39m[39m [39m[39mW[39m[39mo[39m[39mr[39m[39ml[39m[39md[39m[31m![39m[39m'[39m
6+
] {
7+
generatedMessage: [33mtrue[39m,
8+
code: [32m'ERR_ASSERTION'[39m,
9+
actual: [32m'!Hello World'[39m,
10+
expected: [32m'Hello World!'[39m,
11+
operator: [32m'strictEqual'[39m
12+
}
13+
14+
[34mℹ tests 1[39m
15+
[34mℹ suites 0[39m
16+
[34mℹ pass 0[39m
17+
[34mℹ fail 1[39m
18+
[34mℹ cancelled 0[39m
19+
[34mℹ skipped 0[39m
20+
[34mℹ todo 0[39m
21+
[34mℹ duration_ms *[39m
22+
23+
[31m✖ failing tests:[39m
24+
25+
*
26+
[31m✖ failing assertion [90m(*ms)[39m[39m
27+
[AssertionError [ERR_ASSERTION]: Expected values to be strictly equal:
28+
[32mactual[39m [31mexpected[39m
29+
30+
[39m'[39m[32m![39m[39mH[39m[39me[39m[39ml[39m[39ml[39m[39mo[39m[39m [39m[39mW[39m[39mo[39m[39mr[39m[39ml[39m[39md[39m[31m![39m[39m'[39m
31+
] {
32+
generatedMessage: [33mtrue[39m,
33+
code: [32m'ERR_ASSERTION'[39m,
34+
actual: [32m'!Hello World'[39m,
35+
expected: [32m'Hello World!'[39m,
36+
operator: [32m'strictEqual'[39m
37+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
'use strict';
2+
3+
process.env.FORCE_COLOR = 1;
4+
5+
const test = require('node:test');
6+
test('passing test', () => {});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
✔ passing test (*ms)
2+
ℹ tests 1
3+
ℹ suites 0
4+
ℹ pass 1
5+
ℹ fail 0
6+
ℹ cancelled 0
7+
ℹ skipped 0
8+
ℹ todo 0
9+
ℹ duration_ms *

‎test/parallel/test-runner-output.mjs

+11
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,17 @@ const tests = [
203203
name: 'test-runner/output/arbitrary-output.js',
204204
flags: ['--test-reporter=tap'],
205205
},
206+
{
207+
name: 'test-runner/output/non-tty-forced-color-output.js',
208+
flags: ['--test-reporter=spec'],
209+
transform: specTransform,
210+
},
211+
canColorize ? {
212+
name: 'test-runner/output/assertion-color-tty.mjs',
213+
flags: ['--test', '--stack-trace-limit=0'],
214+
transform: specTransform,
215+
tty: true,
216+
} : false,
206217
{
207218
name: 'test-runner/output/async-test-scheduling.mjs',
208219
flags: ['--test-reporter=tap'],

0 commit comments

Comments
 (0)
Please sign in to comment.