Skip to content

Commit

Permalink
test: ensure FORCE_COLOR forces color output in non-TTY environments
Browse files Browse the repository at this point in the history
  • Loading branch information
pmarchini committed Oct 16, 2024
1 parent 0f375db commit 57e19b4
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 12 deletions.
22 changes: 10 additions & 12 deletions lib/internal/util/colors.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,16 @@ module.exports = {
stream.getColorDepth() > 2 : true);
},
refresh() {
if (process.stderr.isTTY) {
const hasColors = module.exports.shouldColorize(process.stderr);
module.exports.blue = hasColors ? '\u001b[34m' : '';
module.exports.green = hasColors ? '\u001b[32m' : '';
module.exports.white = hasColors ? '\u001b[39m' : '';
module.exports.yellow = hasColors ? '\u001b[33m' : '';
module.exports.red = hasColors ? '\u001b[31m' : '';
module.exports.gray = hasColors ? '\u001b[90m' : '';
module.exports.clear = hasColors ? '\u001bc' : '';
module.exports.reset = hasColors ? '\u001b[0m' : '';
module.exports.hasColors = hasColors;
}
const hasColors = module.exports.shouldColorize(process.stderr);
module.exports.blue = hasColors ? '\u001b[34m' : '';
module.exports.green = hasColors ? '\u001b[32m' : '';
module.exports.white = hasColors ? '\u001b[39m' : '';
module.exports.yellow = hasColors ? '\u001b[33m' : '';
module.exports.red = hasColors ? '\u001b[31m' : '';
module.exports.gray = hasColors ? '\u001b[90m' : '';
module.exports.clear = hasColors ? '\u001bc' : '';
module.exports.reset = hasColors ? '\u001b[0m' : '';
module.exports.hasColors = hasColors;
},
};

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
'use strict';

process.env.FORCE_COLOR = 1;

const test = require('node:test');
test('passing test', () => {});
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
✔ passing test (1.312459ms)
ℹ tests 1
ℹ suites 0
ℹ pass 1
ℹ fail 0
ℹ cancelled 0
ℹ skipped 0
ℹ todo 0
ℹ duration_ms *
4 changes: 4 additions & 0 deletions test/parallel/test-runner-output.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,10 @@ const tests = [
name: 'test-runner/output/arbitrary-output.js',
flags: ['--test-reporter=tap'],
},
{
name: 'test-runner/output/non-tty-forced-color-output.js',
flags: ['--test-reporter=spec'],
},
{
name: 'test-runner/output/async-test-scheduling.mjs',
flags: ['--test-reporter=tap'],
Expand Down

0 comments on commit 57e19b4

Please sign in to comment.