test: replace assert.throws w/ common.expectsError

PR-URL: https://github.com/nodejs/node/pull/17494
Reviewed-By: Anatoli Papirovski <apapirovski@mac.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Jon Moss <me@jonathanmoss.me>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
This commit is contained in:
Mithun Sasidharan 2017-12-06 20:02:42 +05:30 committed by Anatoli Papirovski
parent f81bb7b527
commit 20a8e83e44
No known key found for this signature in database
GPG Key ID: 614E2E1ABEB4B2C0
10 changed files with 85 additions and 88 deletions

View File

@ -23,13 +23,13 @@ const errMessage = /^Bad socket type specified\. Valid types are: udp4, udp6$/;
// Error must be thrown with invalid types // Error must be thrown with invalid types
invalidTypes.forEach((invalidType) => { invalidTypes.forEach((invalidType) => {
assert.throws(() => { common.expectsError(() => {
dgram.createSocket(invalidType); dgram.createSocket(invalidType);
}, common.expectsError({ }, {
code: 'ERR_SOCKET_BAD_TYPE', code: 'ERR_SOCKET_BAD_TYPE',
type: TypeError, type: TypeError,
message: errMessage message: errMessage
})); });
}); });
// Error must not be thrown with valid types // Error must not be thrown with valid types

View File

@ -1,6 +1,5 @@
'use strict'; 'use strict';
const common = require('../common'); const common = require('../common');
const assert = require('assert');
const dgram = require('dgram'); const dgram = require('dgram');
const dns = require('dns'); const dns = require('dns');
@ -36,12 +35,12 @@ const dns = require('dns');
{ {
// Verify that non-functions throw. // Verify that non-functions throw.
[null, true, false, 0, 1, NaN, '', 'foo', {}, Symbol()].forEach((value) => { [null, true, false, 0, 1, NaN, '', 'foo', {}, Symbol()].forEach((value) => {
assert.throws(() => { common.expectsError(() => {
dgram.createSocket({ type: 'udp4', lookup: value }); dgram.createSocket({ type: 'udp4', lookup: value });
}, common.expectsError({ }, {
code: 'ERR_INVALID_ARG_TYPE', code: 'ERR_INVALID_ARG_TYPE',
type: TypeError, type: TypeError,
message: 'The "lookup" argument must be of type Function' message: 'The "lookup" argument must be of type Function'
})); });
}); });
} }

View File

@ -11,13 +11,13 @@ const setup = dgram.createSocket.bind(dgram, { type: 'udp4', reuseAddr: true });
{ {
const socket = setup(); const socket = setup();
socket.close(common.mustCall(() => { socket.close(common.mustCall(() => {
assert.throws(() => { common.expectsError(() => {
socket.addMembership(multicastAddress); socket.addMembership(multicastAddress);
}, common.expectsError({ }, {
code: 'ERR_SOCKET_DGRAM_NOT_RUNNING', code: 'ERR_SOCKET_DGRAM_NOT_RUNNING',
type: Error, type: Error,
message: /^Not running$/ message: /^Not running$/
})); });
})); }));
} }
@ -25,39 +25,39 @@ const setup = dgram.createSocket.bind(dgram, { type: 'udp4', reuseAddr: true });
{ {
const socket = setup(); const socket = setup();
socket.close(common.mustCall(() => { socket.close(common.mustCall(() => {
assert.throws(() => { common.expectsError(() => {
socket.dropMembership(multicastAddress); socket.dropMembership(multicastAddress);
}, common.expectsError({ }, {
code: 'ERR_SOCKET_DGRAM_NOT_RUNNING', code: 'ERR_SOCKET_DGRAM_NOT_RUNNING',
type: Error, type: Error,
message: /^Not running$/ message: /^Not running$/
})); });
})); }));
} }
// addMembership() with no argument should throw // addMembership() with no argument should throw
{ {
const socket = setup(); const socket = setup();
assert.throws(() => { common.expectsError(() => {
socket.addMembership(); socket.addMembership();
}, common.expectsError({ }, {
code: 'ERR_MISSING_ARGS', code: 'ERR_MISSING_ARGS',
type: TypeError, type: TypeError,
message: /^The "multicastAddress" argument must be specified$/ message: /^The "multicastAddress" argument must be specified$/
})); });
socket.close(); socket.close();
} }
// dropMembership() with no argument should throw // dropMembership() with no argument should throw
{ {
const socket = setup(); const socket = setup();
assert.throws(() => { common.expectsError(() => {
socket.dropMembership(); socket.dropMembership();
}, common.expectsError({ }, {
code: 'ERR_MISSING_ARGS', code: 'ERR_MISSING_ARGS',
type: TypeError, type: TypeError,
message: /^The "multicastAddress" argument must be specified$/ message: /^The "multicastAddress" argument must be specified$/
})); });
socket.close(); socket.close();
} }

View File

@ -35,13 +35,13 @@ socket.on('listening', common.mustCall(() => {
socket.setMulticastTTL(1000); socket.setMulticastTTL(1000);
}, /^Error: setMulticastTTL EINVAL$/); }, /^Error: setMulticastTTL EINVAL$/);
assert.throws(() => { common.expectsError(() => {
socket.setMulticastTTL('foo'); socket.setMulticastTTL('foo');
}, common.expectsError({ }, {
code: 'ERR_INVALID_ARG_TYPE', code: 'ERR_INVALID_ARG_TYPE',
type: TypeError, type: TypeError,
message: 'The "ttl" argument must be of type number. Received type string' message: 'The "ttl" argument must be of type number. Received type string'
})); });
//close the socket //close the socket
socket.close(); socket.close();

View File

@ -38,19 +38,19 @@ const client = dgram.createSocket('udp4').bind(0, () => {
client.send(buf, port, onMessage); client.send(buf, port, onMessage);
// invalid address: object // invalid address: object
assert.throws(() => { common.expectsError(() => {
client.send(buf, port, []); client.send(buf, port, []);
}, common.expectsError(expectedError)); }, expectedError);
// invalid address: nonzero number // invalid address: nonzero number
assert.throws(() => { common.expectsError(() => {
client.send(buf, port, 1); client.send(buf, port, 1);
}, common.expectsError(expectedError)); }, expectedError);
// invalid address: true // invalid address: true
assert.throws(() => { common.expectsError(() => {
client.send(buf, port, true); client.send(buf, port, true);
}, common.expectsError(expectedError)); }, expectedError);
}); });
client.unref(); client.unref();

View File

@ -1,48 +1,47 @@
'use strict'; 'use strict';
const common = require('../common'); const common = require('../common');
const assert = require('assert');
const dgram = require('dgram'); const dgram = require('dgram');
const socket = dgram.createSocket('udp4'); const socket = dgram.createSocket('udp4');
const errorMessageOffset = const errorMessageOffset =
/^The "offset" argument must be of type number$/; /^The "offset" argument must be of type number$/;
assert.throws(() => { common.expectsError(() => {
socket.sendto(); socket.sendto();
}, common.expectsError({ }, {
code: 'ERR_INVALID_ARG_TYPE', code: 'ERR_INVALID_ARG_TYPE',
type: TypeError, type: TypeError,
message: errorMessageOffset message: errorMessageOffset
})); });
assert.throws(() => { common.expectsError(() => {
socket.sendto('buffer', 1, 'offset', 'port', 'address', 'cb'); socket.sendto('buffer', 1, 'offset', 'port', 'address', 'cb');
}, common.expectsError({ }, {
code: 'ERR_INVALID_ARG_TYPE', code: 'ERR_INVALID_ARG_TYPE',
type: TypeError, type: TypeError,
message: /^The "length" argument must be of type number$/ message: /^The "length" argument must be of type number$/
})); });
assert.throws(() => { common.expectsError(() => {
socket.sendto('buffer', 'offset', 1, 'port', 'address', 'cb'); socket.sendto('buffer', 'offset', 1, 'port', 'address', 'cb');
}, common.expectsError({ }, {
code: 'ERR_INVALID_ARG_TYPE', code: 'ERR_INVALID_ARG_TYPE',
type: TypeError, type: TypeError,
message: errorMessageOffset message: errorMessageOffset
})); });
assert.throws(() => { common.expectsError(() => {
socket.sendto('buffer', 1, 1, 10, false, 'cb'); socket.sendto('buffer', 1, 1, 10, false, 'cb');
}, common.expectsError({ }, {
code: 'ERR_INVALID_ARG_TYPE', code: 'ERR_INVALID_ARG_TYPE',
type: TypeError, type: TypeError,
message: /^The "address" argument must be of type string$/ message: /^The "address" argument must be of type string$/
})); });
assert.throws(() => { common.expectsError(() => {
socket.sendto('buffer', 1, 1, false, 'address', 'cb'); socket.sendto('buffer', 1, 1, false, 'address', 'cb');
}, common.expectsError({ }, {
code: 'ERR_INVALID_ARG_TYPE', code: 'ERR_INVALID_ARG_TYPE',
type: TypeError, type: TypeError,
message: /^The "port" argument must be of type number$/ message: /^The "port" argument must be of type number$/
})); });

View File

@ -9,13 +9,13 @@ socket.on('listening', common.mustCall(() => {
const result = socket.setTTL(16); const result = socket.setTTL(16);
assert.strictEqual(result, 16); assert.strictEqual(result, 16);
assert.throws(() => { common.expectsError(() => {
socket.setTTL('foo'); socket.setTTL('foo');
}, common.expectsError({ }, {
code: 'ERR_INVALID_ARG_TYPE', code: 'ERR_INVALID_ARG_TYPE',
type: TypeError, type: TypeError,
message: 'The "ttl" argument must be of type number. Received type string' message: 'The "ttl" argument must be of type number. Received type string'
})); });
// TTL must be a number from > 0 to < 256 // TTL must be a number from > 0 to < 256
assert.throws(() => { assert.throws(() => {

View File

@ -14,21 +14,21 @@ const dgram = require('dgram');
const socket = dgram.createSocket('udp4'); const socket = dgram.createSocket('udp4');
assert.throws(() => { common.expectsError(() => {
socket.setRecvBufferSize(8192); socket.setRecvBufferSize(8192);
}, common.expectsError(errorObj)); }, errorObj);
assert.throws(() => { common.expectsError(() => {
socket.setSendBufferSize(8192); socket.setSendBufferSize(8192);
}, common.expectsError(errorObj)); }, errorObj);
assert.throws(() => { common.expectsError(() => {
socket.getRecvBufferSize(); socket.getRecvBufferSize();
}, common.expectsError(errorObj)); }, errorObj);
assert.throws(() => { common.expectsError(() => {
socket.getSendBufferSize(); socket.getSendBufferSize();
}, common.expectsError(errorObj)); }, errorObj);
} }
{ {
@ -45,13 +45,13 @@ const dgram = require('dgram');
socket.bind(common.mustCall(() => { socket.bind(common.mustCall(() => {
badBufferSizes.forEach((badBufferSize) => { badBufferSizes.forEach((badBufferSize) => {
assert.throws(() => { common.expectsError(() => {
socket.setRecvBufferSize(badBufferSize); socket.setRecvBufferSize(badBufferSize);
}, common.expectsError(errorObj)); }, errorObj);
assert.throws(() => { common.expectsError(() => {
socket.setSendBufferSize(badBufferSize); socket.setSendBufferSize(badBufferSize);
}, common.expectsError(errorObj)); }, errorObj);
}); });
socket.close(); socket.close();
})); }));
@ -83,9 +83,9 @@ function checkBufferSizeError(type, size) {
'BufferSize'; 'BufferSize';
const socket = dgram.createSocket('udp4'); const socket = dgram.createSocket('udp4');
socket.bind(common.mustCall(() => { socket.bind(common.mustCall(() => {
assert.throws(() => { common.expectsError(() => {
socket[functionName](size); socket[functionName](size);
}, common.expectsError(errorObj)); }, errorObj);
socket.close(); socket.close();
})); }));
} }

View File

@ -7,51 +7,51 @@ const dns = require('dns');
// Stub `getaddrinfo` to *always* error. // Stub `getaddrinfo` to *always* error.
cares.getaddrinfo = () => process.binding('uv').UV_ENOENT; cares.getaddrinfo = () => process.binding('uv').UV_ENOENT;
assert.throws(() => { common.expectsError(() => {
dns.lookup(1, {}); dns.lookup(1, {});
}, common.expectsError({ }, {
code: 'ERR_INVALID_ARG_TYPE', code: 'ERR_INVALID_ARG_TYPE',
type: TypeError, type: TypeError,
message: /^The "hostname" argument must be one of type string or falsy/ message: /^The "hostname" argument must be one of type string or falsy/
})); });
assert.throws(() => { common.expectsError(() => {
dns.lookup(false, 'cb'); dns.lookup(false, 'cb');
}, common.expectsError({ }, {
code: 'ERR_INVALID_CALLBACK', code: 'ERR_INVALID_CALLBACK',
type: TypeError type: TypeError
})); });
assert.throws(() => { common.expectsError(() => {
dns.lookup(false, 'options', 'cb'); dns.lookup(false, 'options', 'cb');
}, common.expectsError({ }, {
code: 'ERR_INVALID_CALLBACK', code: 'ERR_INVALID_CALLBACK',
type: TypeError type: TypeError
})); });
assert.throws(() => { common.expectsError(() => {
dns.lookup(false, { dns.lookup(false, {
hints: 100, hints: 100,
family: 0, family: 0,
all: false all: false
}, common.mustNotCall()); }, common.mustNotCall());
}, common.expectsError({ }, {
code: 'ERR_INVALID_OPT_VALUE', code: 'ERR_INVALID_OPT_VALUE',
type: TypeError, type: TypeError,
message: 'The value "100" is invalid for option "hints"' message: 'The value "100" is invalid for option "hints"'
})); });
assert.throws(() => { common.expectsError(() => {
dns.lookup(false, { dns.lookup(false, {
hints: 0, hints: 0,
family: 20, family: 20,
all: false all: false
}, common.mustNotCall()); }, common.mustNotCall());
}, common.expectsError({ }, {
code: 'ERR_INVALID_OPT_VALUE', code: 'ERR_INVALID_OPT_VALUE',
type: TypeError, type: TypeError,
message: 'The value "20" is invalid for option "family"' message: 'The value "20" is invalid for option "family"'
})); });
assert.doesNotThrow(() => { assert.doesNotThrow(() => {
dns.lookup(false, { dns.lookup(false, {

View File

@ -21,18 +21,17 @@
'use strict'; 'use strict';
const common = require('../common'); const common = require('../common');
const assert = require('assert');
const dns = require('dns'); const dns = require('dns');
// Should not raise assertion error. Issue #7070 // Should not raise assertion error. Issue #7070
assert.throws(() => dns.resolveNs([]), // bad name common.expectsError(() => dns.resolveNs([]), // bad name
common.expectsError({ {
code: 'ERR_INVALID_ARG_TYPE', code: 'ERR_INVALID_ARG_TYPE',
type: TypeError, type: TypeError,
message: /^The "name" argument must be of type string/ message: /^The "name" argument must be of type string/
})); });
assert.throws(() => dns.resolveNs(''), // bad callback common.expectsError(() => dns.resolveNs(''), // bad callback
common.expectsError({ {
code: 'ERR_INVALID_CALLBACK', code: 'ERR_INVALID_CALLBACK',
type: TypeError type: TypeError
})); });