Skip to content

Commit 05ae05f

Browse files
committed
lib: ensure FORCE_COLOR forces color output in non-TTY environments
1 parent 0f375db commit 05ae05f

File tree

4 files changed

+29
-12
lines changed

4 files changed

+29
-12
lines changed

lib/internal/util/colors.js

Lines changed: 10 additions & 12 deletions
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

Lines changed: 6 additions & 0 deletions
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', () => {});
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
✔ passing test (1.312459ms)
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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,10 @@ 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+
},
206210
{
207211
name: 'test-runner/output/async-test-scheduling.mjs',
208212
flags: ['--test-reporter=tap'],

0 commit comments

Comments
 (0)