From 7f01082b2c898cb15a03875fa037105fd03405e2 Mon Sep 17 00:00:00 2001 From: Mithun Sasidharan Date: Wed, 6 Dec 2017 20:02:42 +0530 Subject: [PATCH] test: replace assert.throws w/ common.expectsError PR-URL: https://github.com/nodejs/node/pull/17494 Reviewed-By: Anatoli Papirovski Reviewed-By: James M Snell Reviewed-By: Colin Ihrig Reviewed-By: Ruben Bridgewater Reviewed-By: Jon Moss Reviewed-By: Luigi Pinca --- test/parallel/test-dgram-createSocket-type.js | 6 ++-- test/parallel/test-dgram-custom-lookup.js | 7 ++--- test/parallel/test-dgram-membership.js | 24 +++++++------- test/parallel/test-dgram-multicast-setTTL.js | 6 ++-- .../parallel/test-dgram-send-address-types.js | 12 +++---- test/parallel/test-dgram-sendto.js | 31 +++++++++---------- test/parallel/test-dgram-setTTL.js | 6 ++-- .../parallel/test-dgram-socket-buffer-size.js | 28 ++++++++--------- test/parallel/test-dns-lookup.js | 30 +++++++++--------- test/parallel/test-dns-regress-7070.js | 23 +++++++------- 10 files changed, 85 insertions(+), 88 deletions(-) diff --git a/test/parallel/test-dgram-createSocket-type.js b/test/parallel/test-dgram-createSocket-type.js index 613e1c80308734..54f3aaa0603621 100644 --- a/test/parallel/test-dgram-createSocket-type.js +++ b/test/parallel/test-dgram-createSocket-type.js @@ -23,13 +23,13 @@ const errMessage = /^Bad socket type specified\. Valid types are: udp4, udp6$/; // Error must be thrown with invalid types invalidTypes.forEach((invalidType) => { - assert.throws(() => { + common.expectsError(() => { dgram.createSocket(invalidType); - }, common.expectsError({ + }, { code: 'ERR_SOCKET_BAD_TYPE', type: TypeError, message: errMessage - })); + }); }); // Error must not be thrown with valid types diff --git a/test/parallel/test-dgram-custom-lookup.js b/test/parallel/test-dgram-custom-lookup.js index a418116598c7c9..df715f81d63589 100644 --- a/test/parallel/test-dgram-custom-lookup.js +++ b/test/parallel/test-dgram-custom-lookup.js @@ -1,6 +1,5 @@ 'use strict'; const common = require('../common'); -const assert = require('assert'); const dgram = require('dgram'); const dns = require('dns'); @@ -36,12 +35,12 @@ const dns = require('dns'); { // Verify that non-functions throw. [null, true, false, 0, 1, NaN, '', 'foo', {}, Symbol()].forEach((value) => { - assert.throws(() => { + common.expectsError(() => { dgram.createSocket({ type: 'udp4', lookup: value }); - }, common.expectsError({ + }, { code: 'ERR_INVALID_ARG_TYPE', type: TypeError, message: 'The "lookup" argument must be of type Function' - })); + }); }); } diff --git a/test/parallel/test-dgram-membership.js b/test/parallel/test-dgram-membership.js index 8729b387bf3991..47704e90b7ce13 100644 --- a/test/parallel/test-dgram-membership.js +++ b/test/parallel/test-dgram-membership.js @@ -11,13 +11,13 @@ const setup = dgram.createSocket.bind(dgram, { type: 'udp4', reuseAddr: true }); { const socket = setup(); socket.close(common.mustCall(() => { - assert.throws(() => { + common.expectsError(() => { socket.addMembership(multicastAddress); - }, common.expectsError({ + }, { code: 'ERR_SOCKET_DGRAM_NOT_RUNNING', type: Error, message: /^Not running$/ - })); + }); })); } @@ -25,39 +25,39 @@ const setup = dgram.createSocket.bind(dgram, { type: 'udp4', reuseAddr: true }); { const socket = setup(); socket.close(common.mustCall(() => { - assert.throws(() => { + common.expectsError(() => { socket.dropMembership(multicastAddress); - }, common.expectsError({ + }, { code: 'ERR_SOCKET_DGRAM_NOT_RUNNING', type: Error, message: /^Not running$/ - })); + }); })); } // addMembership() with no argument should throw { const socket = setup(); - assert.throws(() => { + common.expectsError(() => { socket.addMembership(); - }, common.expectsError({ + }, { code: 'ERR_MISSING_ARGS', type: TypeError, message: /^The "multicastAddress" argument must be specified$/ - })); + }); socket.close(); } // dropMembership() with no argument should throw { const socket = setup(); - assert.throws(() => { + common.expectsError(() => { socket.dropMembership(); - }, common.expectsError({ + }, { code: 'ERR_MISSING_ARGS', type: TypeError, message: /^The "multicastAddress" argument must be specified$/ - })); + }); socket.close(); } diff --git a/test/parallel/test-dgram-multicast-setTTL.js b/test/parallel/test-dgram-multicast-setTTL.js index bd04ce4f32bde6..8cfa759ad9f382 100644 --- a/test/parallel/test-dgram-multicast-setTTL.js +++ b/test/parallel/test-dgram-multicast-setTTL.js @@ -35,13 +35,13 @@ socket.on('listening', common.mustCall(() => { socket.setMulticastTTL(1000); }, /^Error: setMulticastTTL EINVAL$/); - assert.throws(() => { + common.expectsError(() => { socket.setMulticastTTL('foo'); - }, common.expectsError({ + }, { code: 'ERR_INVALID_ARG_TYPE', type: TypeError, message: 'The "ttl" argument must be of type number. Received type string' - })); + }); //close the socket socket.close(); diff --git a/test/parallel/test-dgram-send-address-types.js b/test/parallel/test-dgram-send-address-types.js index 6b26c23a266558..b3f4f3fcea290d 100644 --- a/test/parallel/test-dgram-send-address-types.js +++ b/test/parallel/test-dgram-send-address-types.js @@ -38,19 +38,19 @@ const client = dgram.createSocket('udp4').bind(0, () => { client.send(buf, port, onMessage); // invalid address: object - assert.throws(() => { + common.expectsError(() => { client.send(buf, port, []); - }, common.expectsError(expectedError)); + }, expectedError); // invalid address: nonzero number - assert.throws(() => { + common.expectsError(() => { client.send(buf, port, 1); - }, common.expectsError(expectedError)); + }, expectedError); // invalid address: true - assert.throws(() => { + common.expectsError(() => { client.send(buf, port, true); - }, common.expectsError(expectedError)); + }, expectedError); }); client.unref(); diff --git a/test/parallel/test-dgram-sendto.js b/test/parallel/test-dgram-sendto.js index c922dc1039e732..782b2f692b290d 100644 --- a/test/parallel/test-dgram-sendto.js +++ b/test/parallel/test-dgram-sendto.js @@ -1,48 +1,47 @@ 'use strict'; const common = require('../common'); -const assert = require('assert'); const dgram = require('dgram'); const socket = dgram.createSocket('udp4'); const errorMessageOffset = /^The "offset" argument must be of type number$/; -assert.throws(() => { +common.expectsError(() => { socket.sendto(); -}, common.expectsError({ +}, { code: 'ERR_INVALID_ARG_TYPE', type: TypeError, message: errorMessageOffset -})); +}); -assert.throws(() => { +common.expectsError(() => { socket.sendto('buffer', 1, 'offset', 'port', 'address', 'cb'); -}, common.expectsError({ +}, { code: 'ERR_INVALID_ARG_TYPE', type: TypeError, message: /^The "length" argument must be of type number$/ -})); +}); -assert.throws(() => { +common.expectsError(() => { socket.sendto('buffer', 'offset', 1, 'port', 'address', 'cb'); -}, common.expectsError({ +}, { code: 'ERR_INVALID_ARG_TYPE', type: TypeError, message: errorMessageOffset -})); +}); -assert.throws(() => { +common.expectsError(() => { socket.sendto('buffer', 1, 1, 10, false, 'cb'); -}, common.expectsError({ +}, { code: 'ERR_INVALID_ARG_TYPE', type: TypeError, message: /^The "address" argument must be of type string$/ -})); +}); -assert.throws(() => { +common.expectsError(() => { socket.sendto('buffer', 1, 1, false, 'address', 'cb'); -}, common.expectsError({ +}, { code: 'ERR_INVALID_ARG_TYPE', type: TypeError, message: /^The "port" argument must be of type number$/ -})); +}); diff --git a/test/parallel/test-dgram-setTTL.js b/test/parallel/test-dgram-setTTL.js index 840a3f4d09f43a..c6e8cb3c66923c 100644 --- a/test/parallel/test-dgram-setTTL.js +++ b/test/parallel/test-dgram-setTTL.js @@ -9,13 +9,13 @@ socket.on('listening', common.mustCall(() => { const result = socket.setTTL(16); assert.strictEqual(result, 16); - assert.throws(() => { + common.expectsError(() => { socket.setTTL('foo'); - }, common.expectsError({ + }, { code: 'ERR_INVALID_ARG_TYPE', type: TypeError, message: 'The "ttl" argument must be of type number. Received type string' - })); + }); // TTL must be a number from > 0 to < 256 assert.throws(() => { diff --git a/test/parallel/test-dgram-socket-buffer-size.js b/test/parallel/test-dgram-socket-buffer-size.js index 7d6e2bfcc564ca..4b555284d4adb9 100644 --- a/test/parallel/test-dgram-socket-buffer-size.js +++ b/test/parallel/test-dgram-socket-buffer-size.js @@ -14,21 +14,21 @@ const dgram = require('dgram'); const socket = dgram.createSocket('udp4'); - assert.throws(() => { + common.expectsError(() => { socket.setRecvBufferSize(8192); - }, common.expectsError(errorObj)); + }, errorObj); - assert.throws(() => { + common.expectsError(() => { socket.setSendBufferSize(8192); - }, common.expectsError(errorObj)); + }, errorObj); - assert.throws(() => { + common.expectsError(() => { socket.getRecvBufferSize(); - }, common.expectsError(errorObj)); + }, errorObj); - assert.throws(() => { + common.expectsError(() => { socket.getSendBufferSize(); - }, common.expectsError(errorObj)); + }, errorObj); } { @@ -45,13 +45,13 @@ const dgram = require('dgram'); socket.bind(common.mustCall(() => { badBufferSizes.forEach((badBufferSize) => { - assert.throws(() => { + common.expectsError(() => { socket.setRecvBufferSize(badBufferSize); - }, common.expectsError(errorObj)); + }, errorObj); - assert.throws(() => { + common.expectsError(() => { socket.setSendBufferSize(badBufferSize); - }, common.expectsError(errorObj)); + }, errorObj); }); socket.close(); })); @@ -84,9 +84,9 @@ function checkBufferSizeError(type, size) { 'BufferSize'; const socket = dgram.createSocket('udp4'); socket.bind(common.mustCall(() => { - assert.throws(() => { + common.expectsError(() => { socket[functionName](size); - }, common.expectsError(errorObj)); + }, errorObj); socket.close(); })); } diff --git a/test/parallel/test-dns-lookup.js b/test/parallel/test-dns-lookup.js index 18ba8cddc73158..516f6ac0c145ec 100644 --- a/test/parallel/test-dns-lookup.js +++ b/test/parallel/test-dns-lookup.js @@ -7,51 +7,51 @@ const dns = require('dns'); // Stub `getaddrinfo` to *always* error. cares.getaddrinfo = () => process.binding('uv').UV_ENOENT; -assert.throws(() => { +common.expectsError(() => { dns.lookup(1, {}); -}, common.expectsError({ +}, { code: 'ERR_INVALID_ARG_TYPE', type: TypeError, message: /^The "hostname" argument must be one of type string or falsy/ -})); +}); -assert.throws(() => { +common.expectsError(() => { dns.lookup(false, 'cb'); -}, common.expectsError({ +}, { code: 'ERR_INVALID_CALLBACK', type: TypeError -})); +}); -assert.throws(() => { +common.expectsError(() => { dns.lookup(false, 'options', 'cb'); -}, common.expectsError({ +}, { code: 'ERR_INVALID_CALLBACK', type: TypeError -})); +}); -assert.throws(() => { +common.expectsError(() => { dns.lookup(false, { hints: 100, family: 0, all: false }, common.mustNotCall()); -}, common.expectsError({ +}, { code: 'ERR_INVALID_OPT_VALUE', type: TypeError, message: 'The value "100" is invalid for option "hints"' -})); +}); -assert.throws(() => { +common.expectsError(() => { dns.lookup(false, { hints: 0, family: 20, all: false }, common.mustNotCall()); -}, common.expectsError({ +}, { code: 'ERR_INVALID_OPT_VALUE', type: TypeError, message: 'The value "20" is invalid for option "family"' -})); +}); assert.doesNotThrow(() => { dns.lookup(false, { diff --git a/test/parallel/test-dns-regress-7070.js b/test/parallel/test-dns-regress-7070.js index f0e6554fdc7edf..c1f698c3a65035 100644 --- a/test/parallel/test-dns-regress-7070.js +++ b/test/parallel/test-dns-regress-7070.js @@ -21,18 +21,17 @@ 'use strict'; const common = require('../common'); -const assert = require('assert'); const dns = require('dns'); // Should not raise assertion error. Issue #7070 -assert.throws(() => dns.resolveNs([]), // bad name - common.expectsError({ - code: 'ERR_INVALID_ARG_TYPE', - type: TypeError, - message: /^The "name" argument must be of type string/ - })); -assert.throws(() => dns.resolveNs(''), // bad callback - common.expectsError({ - code: 'ERR_INVALID_CALLBACK', - type: TypeError - })); +common.expectsError(() => dns.resolveNs([]), // bad name + { + code: 'ERR_INVALID_ARG_TYPE', + type: TypeError, + message: /^The "name" argument must be of type string/ + }); +common.expectsError(() => dns.resolveNs(''), // bad callback + { + code: 'ERR_INVALID_CALLBACK', + type: TypeError + });