Skip to content

Commit cd10e1a

Browse files
targosaddaleax
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 5ae549c commit cd10e1a

File tree

1 file changed

+24
-22
lines changed

1 file changed

+24
-22
lines changed

test/parallel/test-dns.js

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

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

1010
// Verify that setServers() handles arrays with holes and other oddities
1111
assert.doesNotThrow(() => {
@@ -42,7 +42,8 @@ const goog = [
4242
];
4343
assert.doesNotThrow(() => dns.setServers(goog));
4444
assert.deepStrictEqual(dns.getServers(), goog);
45-
assert.throws(() => dns.setServers(['foobar']));
45+
assert.throws(() => dns.setServers(['foobar']),
46+
/^Error: IP address is not properly formatted: foobar$/);
4647
assert.deepStrictEqual(dns.getServers(), goog);
4748

4849
const goog6 = [
@@ -77,20 +78,18 @@ assert.throws(() => {
7778
}, 'Unexpected error');
7879

7980
// dns.lookup should accept falsey and string values
80-
assert.throws(() => dns.lookup({}, noop),
81-
'invalid arguments: hostname must be a string or falsey');
81+
const errorReg =
82+
/^TypeError: Invalid arguments: hostname must be a string or falsey$/;
8283

83-
assert.throws(() => dns.lookup([], noop),
84-
'invalid arguments: hostname must be a string or falsey');
84+
assert.throws(() => dns.lookup({}, noop), errorReg);
8585

86-
assert.throws(() => dns.lookup(true, noop),
87-
'invalid arguments: hostname must be a string or falsey');
86+
assert.throws(() => dns.lookup([], noop), errorReg);
8887

89-
assert.throws(() => dns.lookup(1, noop),
90-
'invalid arguments: hostname must be a string or falsey');
88+
assert.throws(() => dns.lookup(true, noop), errorReg);
9189

92-
assert.throws(() => dns.lookup(noop, noop),
93-
'invalid arguments: hostname must be a string or falsey');
90+
assert.throws(() => dns.lookup(1, noop), errorReg);
91+
92+
assert.throws(() => dns.lookup(noop, noop), errorReg);
9493

9594
assert.doesNotThrow(() => dns.lookup('', noop));
9695

@@ -114,13 +113,13 @@ assert.doesNotThrow(() => dns.lookup(NaN, noop));
114113
assert.throws(() => {
115114
dns.lookup('www.google.com', { hints: (dns.V4MAPPED | dns.ADDRCONFIG) + 1 },
116115
noop);
117-
});
116+
}, /^TypeError: Invalid argument: hints must use valid flags$/);
118117

119118
assert.throws(() => dns.lookup('www.google.com'),
120-
'invalid arguments: callback must be passed');
119+
/^TypeError: Invalid arguments: callback must be passed$/);
121120

122121
assert.throws(() => dns.lookup('www.google.com', 4),
123-
'invalid arguments: callback must be passed');
122+
/^TypeError: Invalid arguments: callback must be passed$/);
124123

125124
assert.doesNotThrow(() => dns.lookup('www.google.com', 6, noop));
126125

@@ -143,26 +142,29 @@ assert.doesNotThrow(() => {
143142
}, noop);
144143
});
145144

146-
assert.throws(() => dns.lookupService('0.0.0.0'), /Invalid arguments/);
145+
assert.throws(() => dns.lookupService('0.0.0.0'),
146+
/^Error: Invalid arguments$/);
147147

148148
assert.throws(() => dns.lookupService('fasdfdsaf', 0, noop),
149-
/"host" argument needs to be a valid IP address/);
149+
/^TypeError: "host" argument needs to be a valid IP address$/);
150150

151151
assert.doesNotThrow(() => dns.lookupService('0.0.0.0', '0', noop));
152152

153153
assert.doesNotThrow(() => dns.lookupService('0.0.0.0', 0, noop));
154154

155155
assert.throws(() => dns.lookupService('0.0.0.0', null, noop),
156-
/"port" should be >= 0 and < 65536, got "null"/);
156+
/^TypeError: "port" should be >= 0 and < 65536, got "null"$/);
157157

158-
assert.throws(() => dns.lookupService('0.0.0.0', undefined, noop),
159-
/"port" should be >= 0 and < 65536, got "undefined"/);
158+
assert.throws(
159+
() => dns.lookupService('0.0.0.0', undefined, noop),
160+
/^TypeError: "port" should be >= 0 and < 65536, got "undefined"$/
161+
);
160162

161163
assert.throws(() => dns.lookupService('0.0.0.0', 65538, noop),
162-
/"port" should be >= 0 and < 65536, got "65538"/);
164+
/^TypeError: "port" should be >= 0 and < 65536, got "65538"$/);
163165

164166
assert.throws(() => dns.lookupService('0.0.0.0', 'test', noop),
165-
/"port" should be >= 0 and < 65536, got "test"/);
167+
/^TypeError: "port" should be >= 0 and < 65536, got "test"$/);
166168

167169
assert.throws(() => dns.lookupService('0.0.0.0', 80, null),
168170
/^TypeError: "callback" argument must be a function$/);

0 commit comments

Comments
 (0)