From b0f47a089742adad0f9c2a1bc933bdaf2dc836fb Mon Sep 17 00:00:00 2001 From: Kevin Locke Date: Sat, 2 Nov 2024 15:11:36 -0600 Subject: [PATCH] test: handle console colorization The output of console.Console is colorized in Node.js >=20.15 <22.10 due to https://github.com/nodejs/node/pull/51629 and https://github.com/nodejs/node/pull/54677. Handle this in our test cases by comparing the output to the output of console.Console on a PassThrough stream. Signed-off-by: Kevin Locke --- test/cli.js | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/test/cli.js b/test/cli.js index 69337d2..9821416 100644 --- a/test/cli.js +++ b/test/cli.js @@ -227,8 +227,17 @@ describe('noderegression command', () => { assert(brOptions.console instanceof console.Console); brOptions.console.info('info test'); brOptions.console.error('error test'); + + // Note: Output of console.Console is colorized on Node.js >=20.15 <22.10 + // due to nodejs/node#51629 and nodejs/node#54677 + const testStream = new stream.PassThrough({ encoding: 'utf8' }); + // eslint-disable-next-line no-console + const testConsole = new console.Console(testStream); + testConsole.info('info test'); + testConsole.error('error test'); + assert.strictEqual(options.stdout.read(), null); - assert.strictEqual(options.stderr.read(), 'info test\nerror test\n'); + assert.strictEqual(options.stderr.read(), testStream.read()); }); it('passes through options.env', async () => { @@ -475,9 +484,19 @@ describe('noderegression command', () => { for (const level of ['debug', 'info', 'warn', 'error']) { brConsole[level](level); } + + // Note: Output of console.Console is colorized on Node.js >=20.15 <22.10 + // due to nodejs/node#51629 and nodejs/node#54677 + const testStream = new stream.PassThrough({ encoding: 'utf8' }); + // eslint-disable-next-line no-console + const testConsole = new console.Console(testStream); + for (const level of expectLevels) { + testConsole[level](level); + } + assert.strictEqual( options.stderr.read(), - `${expectLevels.join('\n')}\n`, + testStream.read(), ); }); }