From 13b34d2b23477ab12e8a2d846a866cc3034dbdf5 Mon Sep 17 00:00:00 2001 From: Pietro Marchini Date: Thu, 17 Oct 2024 23:23:32 +0200 Subject: [PATCH 1/5] lib: ensure FORCE_COLOR forces color output in non-TTY environments --- lib/internal/util/colors.js | 22 +++++++++---------- .../output/non-tty-forced-color-output.js | 6 +++++ .../non-tty-forced-color-output.snapshot | 9 ++++++++ test/parallel/test-runner-output.mjs | 4 ++++ 4 files changed, 29 insertions(+), 12 deletions(-) create mode 100644 test/fixtures/test-runner/output/non-tty-forced-color-output.js create mode 100644 test/fixtures/test-runner/output/non-tty-forced-color-output.snapshot diff --git a/lib/internal/util/colors.js b/lib/internal/util/colors.js index 054f9c4b4090d8..812048c923bee7 100644 --- a/lib/internal/util/colors.js +++ b/lib/internal/util/colors.js @@ -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; }, }; diff --git a/test/fixtures/test-runner/output/non-tty-forced-color-output.js b/test/fixtures/test-runner/output/non-tty-forced-color-output.js new file mode 100644 index 00000000000000..4d3c9de969a13f --- /dev/null +++ b/test/fixtures/test-runner/output/non-tty-forced-color-output.js @@ -0,0 +1,6 @@ +'use strict'; + +process.env.FORCE_COLOR = 1; + +const test = require('node:test'); +test('passing test', () => {}); diff --git a/test/fixtures/test-runner/output/non-tty-forced-color-output.snapshot b/test/fixtures/test-runner/output/non-tty-forced-color-output.snapshot new file mode 100644 index 00000000000000..19e236bf0e46f4 --- /dev/null +++ b/test/fixtures/test-runner/output/non-tty-forced-color-output.snapshot @@ -0,0 +1,9 @@ +✔ passing test (1.312459ms) +ℹ tests 1 +ℹ suites 0 +ℹ pass 1 +ℹ fail 0 +ℹ cancelled 0 +ℹ skipped 0 +ℹ todo 0 +ℹ duration_ms * diff --git a/test/parallel/test-runner-output.mjs b/test/parallel/test-runner-output.mjs index 72bd42ff73e163..41ecaec06ede4d 100644 --- a/test/parallel/test-runner-output.mjs +++ b/test/parallel/test-runner-output.mjs @@ -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'], From 398da0f170ee55ed7969330c750bf0d7041418b7 Mon Sep 17 00:00:00 2001 From: Pietro Marchini Date: Thu, 17 Oct 2024 23:23:33 +0200 Subject: [PATCH 2/5] test: add specTransform --- .../test-runner/output/non-tty-forced-color-output.snapshot | 2 +- test/parallel/test-runner-output.mjs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/test/fixtures/test-runner/output/non-tty-forced-color-output.snapshot b/test/fixtures/test-runner/output/non-tty-forced-color-output.snapshot index 19e236bf0e46f4..3d1f593832fe77 100644 --- a/test/fixtures/test-runner/output/non-tty-forced-color-output.snapshot +++ b/test/fixtures/test-runner/output/non-tty-forced-color-output.snapshot @@ -1,4 +1,4 @@ -✔ passing test (1.312459ms) +✔ passing test (*ms) ℹ tests 1 ℹ suites 0 ℹ pass 1 diff --git a/test/parallel/test-runner-output.mjs b/test/parallel/test-runner-output.mjs index 41ecaec06ede4d..f88ccacaa03633 100644 --- a/test/parallel/test-runner-output.mjs +++ b/test/parallel/test-runner-output.mjs @@ -205,7 +205,7 @@ const tests = [ }, { name: 'test-runner/output/non-tty-forced-color-output.js', - flags: ['--test-reporter=spec'], + transform: specTransform, }, { name: 'test-runner/output/async-test-scheduling.mjs', From e746328a2a0c52cd4d0fbcc9af376f42322a0e8b Mon Sep 17 00:00:00 2001 From: Pietro Marchini Date: Thu, 17 Oct 2024 23:23:33 +0200 Subject: [PATCH 3/5] test: verify assert colors match test colors --- .../output/assertion-color-tty.mjs | 6 +++ .../output/assertion-color-tty.snapshot | 37 +++++++++++++++++++ test/parallel/test-runner-output.mjs | 6 +++ 3 files changed, 49 insertions(+) create mode 100644 test/fixtures/test-runner/output/assertion-color-tty.mjs create mode 100644 test/fixtures/test-runner/output/assertion-color-tty.snapshot diff --git a/test/fixtures/test-runner/output/assertion-color-tty.mjs b/test/fixtures/test-runner/output/assertion-color-tty.mjs new file mode 100644 index 00000000000000..0a268e5632812f --- /dev/null +++ b/test/fixtures/test-runner/output/assertion-color-tty.mjs @@ -0,0 +1,6 @@ +import assert from 'node:assert/strict' +import { test } from 'node:test' + +test('failing assertion', () => { + assert.equal('apple', 'pear') +}) diff --git a/test/fixtures/test-runner/output/assertion-color-tty.snapshot b/test/fixtures/test-runner/output/assertion-color-tty.snapshot new file mode 100644 index 00000000000000..46b0ec084a2015 --- /dev/null +++ b/test/fixtures/test-runner/output/assertion-color-tty.snapshot @@ -0,0 +1,37 @@ +[31m✖ failing assertion [90m(*ms)[39m[39m + [AssertionError [ERR_ASSERTION]: Expected values to be strictly equal: + [32m+ actual[39m [31m- expected[39m + + [32m+[39m 'apple' + [31m-[39m 'pear'] { + generatedMessage: [33mtrue[39m, + code: [32m'ERR_ASSERTION'[39m, + actual: [32m'apple'[39m, + expected: [32m'pear'[39m, + operator: [32m'strictEqual'[39m + } + +[34mℹ tests 1[39m +[34mℹ suites 0[39m +[34mℹ pass 0[39m +[34mℹ fail 1[39m +[34mℹ cancelled 0[39m +[34mℹ skipped 0[39m +[34mℹ todo 0[39m +[34mℹ duration_ms *[39m + +[31m✖ failing tests:[39m + +* +[31m✖ failing assertion [90m(*ms)[39m[39m + [AssertionError [ERR_ASSERTION]: Expected values to be strictly equal: + [32m+ actual[39m [31m- expected[39m + + [32m+[39m 'apple' + [31m-[39m 'pear'] { + generatedMessage: [33mtrue[39m, + code: [32m'ERR_ASSERTION'[39m, + actual: [32m'apple'[39m, + expected: [32m'pear'[39m, + operator: [32m'strictEqual'[39m + } diff --git a/test/parallel/test-runner-output.mjs b/test/parallel/test-runner-output.mjs index f88ccacaa03633..a79a0a8822aa55 100644 --- a/test/parallel/test-runner-output.mjs +++ b/test/parallel/test-runner-output.mjs @@ -207,6 +207,12 @@ const tests = [ name: 'test-runner/output/non-tty-forced-color-output.js', transform: specTransform, }, + { + name: 'test-runner/output/assertion-color-tty.mjs', + flags: ['--test', '--stack-trace-limit=0'], + transform: specTransform, + tty: true, + }, { name: 'test-runner/output/async-test-scheduling.mjs', flags: ['--test-reporter=tap'], From b74485e2a3eb2a2d9cfe5d7c992736a95f0ed4df Mon Sep 17 00:00:00 2001 From: Pietro Marchini Date: Thu, 17 Oct 2024 23:37:09 +0200 Subject: [PATCH 4/5] test: update fixture --- .../output/assertion-color-tty.mjs | 2 +- .../output/assertion-color-tty.snapshot | 20 +++++++++---------- test/parallel/test-runner-output.mjs | 4 ++-- 3 files changed, 13 insertions(+), 13 deletions(-) diff --git a/test/fixtures/test-runner/output/assertion-color-tty.mjs b/test/fixtures/test-runner/output/assertion-color-tty.mjs index 0a268e5632812f..28845882c10002 100644 --- a/test/fixtures/test-runner/output/assertion-color-tty.mjs +++ b/test/fixtures/test-runner/output/assertion-color-tty.mjs @@ -2,5 +2,5 @@ import assert from 'node:assert/strict' import { test } from 'node:test' test('failing assertion', () => { - assert.equal('apple', 'pear') + assert.strictEqual('!Hello World', 'Hello World!') }) diff --git a/test/fixtures/test-runner/output/assertion-color-tty.snapshot b/test/fixtures/test-runner/output/assertion-color-tty.snapshot index 46b0ec084a2015..2909d909351743 100644 --- a/test/fixtures/test-runner/output/assertion-color-tty.snapshot +++ b/test/fixtures/test-runner/output/assertion-color-tty.snapshot @@ -1,13 +1,13 @@ [31m✖ failing assertion [90m(*ms)[39m[39m [AssertionError [ERR_ASSERTION]: Expected values to be strictly equal: - [32m+ actual[39m [31m- expected[39m + [32mactual[39m [31mexpected[39m - [32m+[39m 'apple' - [31m-[39m 'pear'] { + [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 + ] { generatedMessage: [33mtrue[39m, code: [32m'ERR_ASSERTION'[39m, - actual: [32m'apple'[39m, - expected: [32m'pear'[39m, + actual: [32m'!Hello World'[39m, + expected: [32m'Hello World!'[39m, operator: [32m'strictEqual'[39m } @@ -25,13 +25,13 @@ * [31m✖ failing assertion [90m(*ms)[39m[39m [AssertionError [ERR_ASSERTION]: Expected values to be strictly equal: - [32m+ actual[39m [31m- expected[39m + [32mactual[39m [31mexpected[39m - [32m+[39m 'apple' - [31m-[39m 'pear'] { + [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 + ] { generatedMessage: [33mtrue[39m, code: [32m'ERR_ASSERTION'[39m, - actual: [32m'apple'[39m, - expected: [32m'pear'[39m, + actual: [32m'!Hello World'[39m, + expected: [32m'Hello World!'[39m, operator: [32m'strictEqual'[39m } diff --git a/test/parallel/test-runner-output.mjs b/test/parallel/test-runner-output.mjs index a79a0a8822aa55..f7e1c07a4d5f94 100644 --- a/test/parallel/test-runner-output.mjs +++ b/test/parallel/test-runner-output.mjs @@ -207,12 +207,12 @@ const tests = [ name: 'test-runner/output/non-tty-forced-color-output.js', transform: specTransform, }, - { + !skipForceColors ? { name: 'test-runner/output/assertion-color-tty.mjs', flags: ['--test', '--stack-trace-limit=0'], transform: specTransform, tty: true, - }, + } : false, { name: 'test-runner/output/async-test-scheduling.mjs', flags: ['--test-reporter=tap'], From dd82e49d473b230672a560b564e248c301b164cd Mon Sep 17 00:00:00 2001 From: Pietro Marchini Date: Sun, 20 Oct 2024 17:48:01 +0200 Subject: [PATCH 5/5] test: add canColorize --- test/parallel/test-runner-output.mjs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/parallel/test-runner-output.mjs b/test/parallel/test-runner-output.mjs index f7e1c07a4d5f94..4f745a5d4a9be7 100644 --- a/test/parallel/test-runner-output.mjs +++ b/test/parallel/test-runner-output.mjs @@ -207,7 +207,7 @@ const tests = [ name: 'test-runner/output/non-tty-forced-color-output.js', transform: specTransform, }, - !skipForceColors ? { + canColorize ? { name: 'test-runner/output/assertion-color-tty.mjs', flags: ['--test', '--stack-trace-limit=0'], transform: specTransform,