From c34703e4d161dbf733d0bebe3d61882cd5d0b3a9 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 | 30 +++++++++++++++++------------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/test/parallel/test-dns.js b/test/parallel/test-dns.js index 9a2577ec90de78..2d19da31f048c7 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'); var existing = dns.getServers(); -assert(existing.length); +assert(existing.length > 0); function noop() {} @@ -15,7 +15,8 @@ var goog = [ ]; assert.doesNotThrow(function() { dns.setServers(goog); }); assert.deepEqual(dns.getServers(), goog); -assert.throws(function() { dns.setServers(['foobar']); }); +assert.throws(function() { dns.setServers(['foobar']); }, + /^Error: IP address is not properly formatted: foobar$/); assert.deepEqual(dns.getServers(), goog); var goog6 = [ @@ -50,25 +51,28 @@ assert.throws(function() { }, 'Unexpected error'); // dns.lookup should accept falsey and string values +const errorReg = + /^TypeError: invalid arguments: hostname must be a string or falsey$/; + assert.throws(function() { dns.lookup({}, noop); -}, 'invalid arguments: hostname must be a string or falsey'); +}, errorReg); assert.throws(function() { dns.lookup([], noop); -}, 'invalid arguments: hostname must be a string or falsey'); +}, errorReg); assert.throws(function() { dns.lookup(true, noop); -}, 'invalid arguments: hostname must be a string or falsey'); +}, errorReg); assert.throws(function() { dns.lookup(1, noop); -}, 'invalid arguments: hostname must be a string or falsey'); +}, errorReg); assert.throws(function() { dns.lookup(noop, noop); -}, 'invalid arguments: hostname must be a string or falsey'); +}, errorReg); assert.doesNotThrow(function() { dns.lookup('', noop); @@ -102,15 +106,15 @@ assert.doesNotThrow(function() { assert.throws(function() { dns.lookup('www.google.com', { hints: (dns.V4MAPPED | dns.ADDRCONFIG) + 1 }, noop); -}); +}, /^TypeError: invalid argument: hints must use valid flags$/); assert.throws(function() { dns.lookup('www.google.com'); -}, 'invalid arguments: callback must be passed'); +}, /^TypeError: invalid arguments: callback must be passed$/); assert.throws(function() { dns.lookup('www.google.com', 4); -}, 'invalid arguments: callback must be passed'); +}, /^TypeError: invalid arguments: callback must be passed$/); assert.doesNotThrow(function() { dns.lookup('www.google.com', 6, noop); @@ -148,15 +152,15 @@ assert.doesNotThrow(function() { assert.throws(function() { dns.lookupService('0.0.0.0'); -}, /invalid arguments/); +}, /^Error: invalid arguments$/); assert.throws(function() { dns.lookupService('fasdfdsaf', 0, noop); -}, /host needs to be a valid IP address/); +}, /^TypeError: host needs to be a valid IP address$/); assert.throws(function() { dns.lookupService('0.0.0.0', '0', noop); -}, /port argument must be a number, got "0"/); +}, /^TypeError: port argument must be a number, got "0"$/); assert.doesNotThrow(function() { dns.lookupService('0.0.0.0', 0, noop);