Skip to content

Commit 43e1ca8

Browse files
committed
test: add expectWarning to common
There are multiple tests that use the same boilerplate to test that warnings are correctly emitted. This adds a new common function to do that and changes the tests to use it. PR-URL: #8662 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Benjamin Gruenbaum <[email protected]> Reviewed-By: Rich Trott <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent c063502 commit 43e1ca8

File tree

5 files changed

+20
-41
lines changed

5 files changed

+20
-41
lines changed

test/common.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -508,3 +508,16 @@ exports.isAlive = function isAlive(pid) {
508508
return false;
509509
}
510510
};
511+
512+
exports.expectWarning = function(name, expected) {
513+
if (typeof expected === 'string')
514+
expected = [expected];
515+
process.on('warning', exports.mustCall((warning) => {
516+
assert.strictEqual(warning.name, name);
517+
assert.ok(expected.includes(warning.message),
518+
`unexpected error message: "${warning.message}"`);
519+
// Remove a warning message after it is seen so that we guarantee that we
520+
// get each message only once.
521+
expected.splice(expected.indexOf(warning.message), 1);
522+
}, expected.length));
523+
};
Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,11 @@
11
'use strict';
22
const common = require('../common');
3-
const assert = require('assert');
43

54
const expected =
65
'Using Buffer without `new` will soon stop working. ' +
76
'Use `new Buffer()`, or preferably ' +
87
'`Buffer.from()`, `Buffer.allocUnsafe()` or `Buffer.alloc()` instead.';
9-
10-
process.on('warning', common.mustCall((warning) => {
11-
assert.strictEqual(warning.name, 'DeprecationWarning');
12-
assert.strictEqual(warning.message, expected,
13-
`unexpected error message: "${warning.message}"`);
14-
}, 1));
8+
common.expectWarning('DeprecationWarning', expected);
159

1610
Buffer(1);
1711
Buffer(1);

test/parallel/test-crypto-deprecated.js

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,10 @@ if (!common.hasCrypto) {
99
const crypto = require('crypto');
1010
const tls = require('tls');
1111

12-
const expected = [
12+
common.expectWarning('DeprecationWarning', [
1313
'crypto.Credentials is deprecated. Use tls.SecureContext instead.',
1414
'crypto.createCredentials is deprecated. Use tls.createSecureContext instead.'
15-
];
16-
17-
process.on('warning', common.mustCall((warning) => {
18-
assert.strictEqual(warning.name, 'DeprecationWarning');
19-
assert.notStrictEqual(expected.indexOf(warning.message), -1,
20-
`unexpected error message: "${warning.message}"`);
21-
// Remove a warning message after it is seen so that we guarantee that we get
22-
// each message only once.
23-
expected.splice(expected.indexOf(warning.message), 1);
24-
}, expected.length));
15+
]);
2516

2617
// Accessing the deprecated function is enough to trigger the warning event.
2718
// It does not need to be called. So the assert serves the purpose of both

test/parallel/test-repl-deprecated.js

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,8 @@ const common = require('../common');
33
const assert = require('assert');
44
const repl = require('repl');
55

6-
const expected = [
7-
'replServer.convertToContext() is deprecated'
8-
];
9-
10-
process.on('warning', common.mustCall((warning) => {
11-
assert.strictEqual(warning.name, 'DeprecationWarning');
12-
assert.notStrictEqual(expected.indexOf(warning.message), -1,
13-
`unexpected error message: "${warning.message}"`);
14-
// Remove a warning message after it is seen so that we guarantee that we get
15-
// each message only once.
16-
expected.splice(expected.indexOf(warning.message), 1);
17-
}, expected.length));
6+
common.expectWarning('DeprecationWarning',
7+
'replServer.convertToContext() is deprecated');
188

199
// Create a dummy stream that does nothing
2010
const stream = new common.ArrayStream();

test/parallel/test-util.js

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -121,21 +121,12 @@ assert.strictEqual(util.isFunction(function() {}), true);
121121
assert.strictEqual(util.isFunction(), false);
122122
assert.strictEqual(util.isFunction('string'), false);
123123

124-
const expected = [
124+
common.expectWarning('DeprecationWarning', [
125125
'util.print is deprecated. Use console.log instead.',
126126
'util.puts is deprecated. Use console.log instead.',
127127
'util.debug is deprecated. Use console.error instead.',
128128
'util.error is deprecated. Use console.error instead.'
129-
];
130-
131-
process.on('warning', common.mustCall((warning) => {
132-
assert.strictEqual(warning.name, 'DeprecationWarning');
133-
assert.notStrictEqual(expected.indexOf(warning.message), -1,
134-
`unexpected error message: "${warning.message}"`);
135-
// Remove a warning message after it is seen so that we guarantee that we get
136-
// each message only once.
137-
expected.splice(expected.indexOf(warning.message), 1);
138-
}, expected.length));
129+
]);
139130

140131
util.print('test');
141132
util.puts('test');

0 commit comments

Comments
 (0)