From 75c565bf181965f52e765955c5bf776041d42ce3 Mon Sep 17 00:00:00 2001 From: Rich Trott Date: Wed, 29 Dec 2021 21:58:53 -0800 Subject: [PATCH] test: improve expectWarning error message MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit expectWarning() fails with a TypeError and a message about undefined not being iterable when the warning is emitted more times than expected. This change produces a more useful error message. Refs: https://github.com/nodejs/node/pull/41307/files#r775197738 PR-URL: https://github.com/nodejs/node/pull/41326 Reviewed-By: Tobias Nießen Reviewed-By: Luigi Pinca --- test/common/index.js | 6 ++- test/parallel/test-common-expect-warning.js | 50 +++++++++++++++++++++ 2 files changed, 55 insertions(+), 1 deletion(-) create mode 100644 test/parallel/test-common-expect-warning.js diff --git a/test/common/index.js b/test/common/index.js index 5efb36e87cccdd..a1744836887d2f 100644 --- a/test/common/index.js +++ b/test/common/index.js @@ -549,7 +549,11 @@ function _expectWarning(name, expected, code) { expected.forEach(([_, code]) => assert(code, expected)); } return mustCall((warning) => { - const [ message, code ] = expected.shift(); + const expectedProperties = expected.shift(); + if (!expectedProperties) { + assert.fail(`Unexpected extra warning received: ${warning}`); + } + const [ message, code ] = expectedProperties; assert.strictEqual(warning.name, name); if (typeof message === 'string') { assert.strictEqual(warning.message, message); diff --git a/test/parallel/test-common-expect-warning.js b/test/parallel/test-common-expect-warning.js new file mode 100644 index 00000000000000..be3e385b11e0f4 --- /dev/null +++ b/test/parallel/test-common-expect-warning.js @@ -0,0 +1,50 @@ +'use strict'; + +const common = require('../common'); +const assert = require('assert'); +const { spawn } = require('child_process'); + +if (process.argv[2] !== 'child') { + // Expected error not emitted. + { + const child = spawn( + process.execPath, [__filename, 'child', 0], { encoding: 'utf8' } + ); + child.on('exit', common.mustCall((status) => { + assert.notStrictEqual(status, 0); + })); + } + + // Expected error emitted. + { + const child = spawn( + process.execPath, [__filename, 'child', 1], { encoding: 'utf8' } + ); + child.on('exit', common.mustCall((status) => { + assert.strictEqual(status, 0); + })); + } + + // Expected error emitted too many times. + { + const child = spawn( + process.execPath, [__filename, 'child', 2], { encoding: 'utf8' } + ); + child.stderr.setEncoding('utf8'); + + let stderr = ''; + child.stderr.on('data', (data) => { + stderr += data; + }); + child.on('exit', common.mustCall((status) => { + assert.notStrictEqual(status, 0); + assert.match(stderr, /Unexpected extra warning received/); + })); + } +} else { + const iterations = +process.argv[3]; + common.expectWarning('fhqwhgads', 'fhqwhgads', 'fhqwhgads'); + for (let i = 0; i < iterations; i++) { + process.emitWarning('fhqwhgads', 'fhqwhgads', 'fhqwhgads'); + } +}