Skip to content

Commit 75c565b

Browse files
Trotttargos
authored andcommitted
test: improve expectWarning error message
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: #41326 Reviewed-By: Tobias Nießen <[email protected]> Reviewed-By: Luigi Pinca <[email protected]>
1 parent c136d59 commit 75c565b

File tree

2 files changed

+55
-1
lines changed

2 files changed

+55
-1
lines changed

test/common/index.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -549,7 +549,11 @@ function _expectWarning(name, expected, code) {
549549
expected.forEach(([_, code]) => assert(code, expected));
550550
}
551551
return mustCall((warning) => {
552-
const [ message, code ] = expected.shift();
552+
const expectedProperties = expected.shift();
553+
if (!expectedProperties) {
554+
assert.fail(`Unexpected extra warning received: ${warning}`);
555+
}
556+
const [ message, code ] = expectedProperties;
553557
assert.strictEqual(warning.name, name);
554558
if (typeof message === 'string') {
555559
assert.strictEqual(warning.message, message);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
const assert = require('assert');
5+
const { spawn } = require('child_process');
6+
7+
if (process.argv[2] !== 'child') {
8+
// Expected error not emitted.
9+
{
10+
const child = spawn(
11+
process.execPath, [__filename, 'child', 0], { encoding: 'utf8' }
12+
);
13+
child.on('exit', common.mustCall((status) => {
14+
assert.notStrictEqual(status, 0);
15+
}));
16+
}
17+
18+
// Expected error emitted.
19+
{
20+
const child = spawn(
21+
process.execPath, [__filename, 'child', 1], { encoding: 'utf8' }
22+
);
23+
child.on('exit', common.mustCall((status) => {
24+
assert.strictEqual(status, 0);
25+
}));
26+
}
27+
28+
// Expected error emitted too many times.
29+
{
30+
const child = spawn(
31+
process.execPath, [__filename, 'child', 2], { encoding: 'utf8' }
32+
);
33+
child.stderr.setEncoding('utf8');
34+
35+
let stderr = '';
36+
child.stderr.on('data', (data) => {
37+
stderr += data;
38+
});
39+
child.on('exit', common.mustCall((status) => {
40+
assert.notStrictEqual(status, 0);
41+
assert.match(stderr, /Unexpected extra warning received/);
42+
}));
43+
}
44+
} else {
45+
const iterations = +process.argv[3];
46+
common.expectWarning('fhqwhgads', 'fhqwhgads', 'fhqwhgads');
47+
for (let i = 0; i < iterations; i++) {
48+
process.emitWarning('fhqwhgads', 'fhqwhgads', 'fhqwhgads');
49+
}
50+
}

0 commit comments

Comments
 (0)