Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: add expectDeprecationWarning to common #8662

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions test/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -508,3 +508,16 @@ exports.isAlive = function isAlive(pid) {
return false;
}
};

exports.expectWarning = function(name, expected) {
if (typeof expected === 'string')
expected = [expected];
process.on('warning', exports.mustCall((warning) => {
assert.strictEqual(warning.name, name);
assert.ok(expected.includes(warning.message),
`unexpected error message: "${warning.message}"`);
// Remove a warning message after it is seen so that we guarantee that we
// get each message only once.
expected.splice(expected.indexOf(warning.message), 1);
}, expected.length));
};
8 changes: 1 addition & 7 deletions test/parallel/test-buffer-deprecated.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,11 @@
'use strict';
const common = require('../common');
const assert = require('assert');

const expected =
'Using Buffer without `new` will soon stop working. ' +
'Use `new Buffer()`, or preferably ' +
'`Buffer.from()`, `Buffer.allocUnsafe()` or `Buffer.alloc()` instead.';

process.on('warning', common.mustCall((warning) => {
assert.strictEqual(warning.name, 'DeprecationWarning');
assert.strictEqual(warning.message, expected,
`unexpected error message: "${warning.message}"`);
}, 1));
common.expectWarning('DeprecationWarning', expected);

Buffer(1);
Buffer(1);
13 changes: 2 additions & 11 deletions test/parallel/test-crypto-deprecated.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,10 @@ if (!common.hasCrypto) {
const crypto = require('crypto');
const tls = require('tls');

const expected = [
common.expectWarning('DeprecationWarning', [
'crypto.Credentials is deprecated. Use tls.SecureContext instead.',
'crypto.createCredentials is deprecated. Use tls.createSecureContext instead.'
];

process.on('warning', common.mustCall((warning) => {
assert.strictEqual(warning.name, 'DeprecationWarning');
assert.notStrictEqual(expected.indexOf(warning.message), -1,
`unexpected error message: "${warning.message}"`);
// Remove a warning message after it is seen so that we guarantee that we get
// each message only once.
expected.splice(expected.indexOf(warning.message), 1);
}, expected.length));
]);

// Accessing the deprecated function is enough to trigger the warning event.
// It does not need to be called. So the assert serves the purpose of both
Expand Down
14 changes: 2 additions & 12 deletions test/parallel/test-repl-deprecated.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,8 @@ const common = require('../common');
const assert = require('assert');
const repl = require('repl');

const expected = [
'replServer.convertToContext() is deprecated'
];

process.on('warning', common.mustCall((warning) => {
assert.strictEqual(warning.name, 'DeprecationWarning');
assert.notStrictEqual(expected.indexOf(warning.message), -1,
`unexpected error message: "${warning.message}"`);
// Remove a warning message after it is seen so that we guarantee that we get
// each message only once.
expected.splice(expected.indexOf(warning.message), 1);
}, expected.length));
common.expectWarning('DeprecationWarning',
'replServer.convertToContext() is deprecated');

// Create a dummy stream that does nothing
const stream = new common.ArrayStream();
Expand Down
13 changes: 2 additions & 11 deletions test/parallel/test-util.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,21 +121,12 @@ assert.strictEqual(util.isFunction(function() {}), true);
assert.strictEqual(util.isFunction(), false);
assert.strictEqual(util.isFunction('string'), false);

const expected = [
common.expectWarning('DeprecationWarning', [
'util.print is deprecated. Use console.log instead.',
'util.puts is deprecated. Use console.log instead.',
'util.debug is deprecated. Use console.error instead.',
'util.error is deprecated. Use console.error instead.'
];

process.on('warning', common.mustCall((warning) => {
assert.strictEqual(warning.name, 'DeprecationWarning');
assert.notStrictEqual(expected.indexOf(warning.message), -1,
`unexpected error message: "${warning.message}"`);
// Remove a warning message after it is seen so that we guarantee that we get
// each message only once.
expected.splice(expected.indexOf(warning.message), 1);
}, expected.length));
]);

util.print('test');
util.puts('test');
Expand Down