From cd10e1ae4a52e54d6404836906a7cfe75866c222 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C3=ABl=20Zasso?= Date: Sat, 26 Nov 2016 11:17:58 +0100 Subject: [PATCH] test: refactor and fix test-dns * More precise length assertion. * Fix incorrect use of string instead of RegExp in `throws` assertions. * Add missing RegExp to `throws` assertions. PR-URL: https://github.com/nodejs/node/pull/9811 Reviewed-By: Colin Ihrig Reviewed-By: Rich Trott Reviewed-By: Luigi Pinca --- test/parallel/test-dns.js | 46 ++++++++++++++++++++------------------- 1 file changed, 24 insertions(+), 22 deletions(-) diff --git a/test/parallel/test-dns.js b/test/parallel/test-dns.js index 5fcc045b2e3fe4..9ee2e9f974f625 100644 --- a/test/parallel/test-dns.js +++ b/test/parallel/test-dns.js @@ -5,7 +5,7 @@ const assert = require('assert'); const dns = require('dns'); const existing = dns.getServers(); -assert(existing.length); +assert(existing.length > 0); // Verify that setServers() handles arrays with holes and other oddities assert.doesNotThrow(() => { @@ -42,7 +42,8 @@ const goog = [ ]; assert.doesNotThrow(() => dns.setServers(goog)); assert.deepStrictEqual(dns.getServers(), goog); -assert.throws(() => dns.setServers(['foobar'])); +assert.throws(() => dns.setServers(['foobar']), + /^Error: IP address is not properly formatted: foobar$/); assert.deepStrictEqual(dns.getServers(), goog); const goog6 = [ @@ -77,20 +78,18 @@ assert.throws(() => { }, 'Unexpected error'); // dns.lookup should accept falsey and string values -assert.throws(() => dns.lookup({}, noop), - 'invalid arguments: hostname must be a string or falsey'); +const errorReg = + /^TypeError: Invalid arguments: hostname must be a string or falsey$/; -assert.throws(() => dns.lookup([], noop), - 'invalid arguments: hostname must be a string or falsey'); +assert.throws(() => dns.lookup({}, noop), errorReg); -assert.throws(() => dns.lookup(true, noop), - 'invalid arguments: hostname must be a string or falsey'); +assert.throws(() => dns.lookup([], noop), errorReg); -assert.throws(() => dns.lookup(1, noop), - 'invalid arguments: hostname must be a string or falsey'); +assert.throws(() => dns.lookup(true, noop), errorReg); -assert.throws(() => dns.lookup(noop, noop), - 'invalid arguments: hostname must be a string or falsey'); +assert.throws(() => dns.lookup(1, noop), errorReg); + +assert.throws(() => dns.lookup(noop, noop), errorReg); assert.doesNotThrow(() => dns.lookup('', noop)); @@ -114,13 +113,13 @@ assert.doesNotThrow(() => dns.lookup(NaN, noop)); assert.throws(() => { dns.lookup('www.google.com', { hints: (dns.V4MAPPED | dns.ADDRCONFIG) + 1 }, noop); -}); +}, /^TypeError: Invalid argument: hints must use valid flags$/); assert.throws(() => dns.lookup('www.google.com'), - 'invalid arguments: callback must be passed'); + /^TypeError: Invalid arguments: callback must be passed$/); assert.throws(() => dns.lookup('www.google.com', 4), - 'invalid arguments: callback must be passed'); + /^TypeError: Invalid arguments: callback must be passed$/); assert.doesNotThrow(() => dns.lookup('www.google.com', 6, noop)); @@ -143,26 +142,29 @@ assert.doesNotThrow(() => { }, noop); }); -assert.throws(() => dns.lookupService('0.0.0.0'), /Invalid arguments/); +assert.throws(() => dns.lookupService('0.0.0.0'), + /^Error: Invalid arguments$/); assert.throws(() => dns.lookupService('fasdfdsaf', 0, noop), - /"host" argument needs to be a valid IP address/); + /^TypeError: "host" argument needs to be a valid IP address$/); assert.doesNotThrow(() => dns.lookupService('0.0.0.0', '0', noop)); assert.doesNotThrow(() => dns.lookupService('0.0.0.0', 0, noop)); assert.throws(() => dns.lookupService('0.0.0.0', null, noop), - /"port" should be >= 0 and < 65536, got "null"/); + /^TypeError: "port" should be >= 0 and < 65536, got "null"$/); -assert.throws(() => dns.lookupService('0.0.0.0', undefined, noop), - /"port" should be >= 0 and < 65536, got "undefined"/); +assert.throws( + () => dns.lookupService('0.0.0.0', undefined, noop), + /^TypeError: "port" should be >= 0 and < 65536, got "undefined"$/ +); assert.throws(() => dns.lookupService('0.0.0.0', 65538, noop), - /"port" should be >= 0 and < 65536, got "65538"/); + /^TypeError: "port" should be >= 0 and < 65536, got "65538"$/); assert.throws(() => dns.lookupService('0.0.0.0', 'test', noop), - /"port" should be >= 0 and < 65536, got "test"/); + /^TypeError: "port" should be >= 0 and < 65536, got "test"$/); assert.throws(() => dns.lookupService('0.0.0.0', 80, null), /^TypeError: "callback" argument must be a function$/);