Skip to content

Commit f5b952b

Browse files
targosMylesBorins
authored andcommitted
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: #9811 Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Rich Trott <[email protected]> Reviewed-By: Luigi Pinca <[email protected]>
1 parent 8b733dc commit f5b952b

File tree

1 file changed

+17
-13
lines changed

1 file changed

+17
-13
lines changed

test/parallel/test-dns.js

+17-13
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const assert = require('assert');
55
const dns = require('dns');
66

77
var existing = dns.getServers();
8-
assert(existing.length);
8+
assert(existing.length > 0);
99

1010
function noop() {}
1111

@@ -15,7 +15,8 @@ var goog = [
1515
];
1616
assert.doesNotThrow(function() { dns.setServers(goog); });
1717
assert.deepEqual(dns.getServers(), goog);
18-
assert.throws(function() { dns.setServers(['foobar']); });
18+
assert.throws(function() { dns.setServers(['foobar']); },
19+
/^Error: IP address is not properly formatted: foobar$/);
1920
assert.deepEqual(dns.getServers(), goog);
2021

2122
var goog6 = [
@@ -50,25 +51,28 @@ assert.throws(function() {
5051
}, 'Unexpected error');
5152

5253
// dns.lookup should accept falsey and string values
54+
const errorReg =
55+
/^TypeError: invalid arguments: hostname must be a string or falsey$/;
56+
5357
assert.throws(function() {
5458
dns.lookup({}, noop);
55-
}, 'invalid arguments: hostname must be a string or falsey');
59+
}, errorReg);
5660

5761
assert.throws(function() {
5862
dns.lookup([], noop);
59-
}, 'invalid arguments: hostname must be a string or falsey');
63+
}, errorReg);
6064

6165
assert.throws(function() {
6266
dns.lookup(true, noop);
63-
}, 'invalid arguments: hostname must be a string or falsey');
67+
}, errorReg);
6468

6569
assert.throws(function() {
6670
dns.lookup(1, noop);
67-
}, 'invalid arguments: hostname must be a string or falsey');
71+
}, errorReg);
6872

6973
assert.throws(function() {
7074
dns.lookup(noop, noop);
71-
}, 'invalid arguments: hostname must be a string or falsey');
75+
}, errorReg);
7276

7377
assert.doesNotThrow(function() {
7478
dns.lookup('', noop);
@@ -102,15 +106,15 @@ assert.doesNotThrow(function() {
102106
assert.throws(function() {
103107
dns.lookup('www.google.com', { hints: (dns.V4MAPPED | dns.ADDRCONFIG) + 1 },
104108
noop);
105-
});
109+
}, /^TypeError: invalid argument: hints must use valid flags$/);
106110

107111
assert.throws(function() {
108112
dns.lookup('www.google.com');
109-
}, 'invalid arguments: callback must be passed');
113+
}, /^TypeError: invalid arguments: callback must be passed$/);
110114

111115
assert.throws(function() {
112116
dns.lookup('www.google.com', 4);
113-
}, 'invalid arguments: callback must be passed');
117+
}, /^TypeError: invalid arguments: callback must be passed$/);
114118

115119
assert.doesNotThrow(function() {
116120
dns.lookup('www.google.com', 6, noop);
@@ -148,15 +152,15 @@ assert.doesNotThrow(function() {
148152

149153
assert.throws(function() {
150154
dns.lookupService('0.0.0.0');
151-
}, /invalid arguments/);
155+
}, /^Error: invalid arguments$/);
152156

153157
assert.throws(function() {
154158
dns.lookupService('fasdfdsaf', 0, noop);
155-
}, /host needs to be a valid IP address/);
159+
}, /^TypeError: host needs to be a valid IP address$/);
156160

157161
assert.throws(function() {
158162
dns.lookupService('0.0.0.0', '0', noop);
159-
}, /port argument must be a number, got "0"/);
163+
}, /^TypeError: port argument must be a number, got "0"$/);
160164

161165
assert.doesNotThrow(function() {
162166
dns.lookupService('0.0.0.0', 0, noop);

0 commit comments

Comments
 (0)