Skip to content

Commit

Permalink
test: improve expectWarning error message
Browse files Browse the repository at this point in the history
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: nodejs#41326
Reviewed-By: Tobias Nießen <[email protected]>
Reviewed-By: Luigi Pinca <[email protected]>
  • Loading branch information
Trott authored and Linkgoron committed Jan 31, 2022
1 parent 286c9af commit 26c349a
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 1 deletion.
6 changes: 5 additions & 1 deletion test/common/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
50 changes: 50 additions & 0 deletions test/parallel/test-common-expect-warning.js
Original file line number Diff line number Diff line change
@@ -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');
}
}

0 comments on commit 26c349a

Please sign in to comment.