Skip to content

Commit ca461bf

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 aebc8df commit ca461bf

File tree

1 file changed

+24
-22
lines changed

1 file changed

+24
-22
lines changed

‎test/parallel/test-dns.js‎

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const assert = require('assert');
55
constdns=require('dns');
66

77
constexisting=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:IPaddressisnotproperlyformatted:foobar$/);
4647
assert.deepStrictEqual(dns.getServers(),goog);
4748

4849
constgoog6=[
@@ -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+
consterrorReg=
82+
/^TypeError:Invalidarguments:hostnamemustbeastringorfalsey$/;
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:Invalidargument:hintsmustusevalidflags$/);
118117

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

122121
assert.throws(()=>dns.lookup('www.google.com',4),
123-
'invalid arguments: callback must be passed');
122+
/^TypeError:Invalidarguments:callbackmustbepassed$/);
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'),/Invalidarguments/);
145+
assert.throws(()=>dns.lookupService('0.0.0.0'),
146+
/^Error:Invalidarguments$/);
147147

148148
assert.throws(()=>dns.lookupService('fasdfdsaf',0,noop),
149-
/"host"argumentneedstobeavalidIPaddress/);
149+
/^TypeError:"host"argumentneedstobeavalidIPaddress$/);
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"shouldbe>=0and<65536,got"null"/);
156+
/^TypeError:"port"shouldbe>=0and<65536,got"null"$/);
157157

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

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

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

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

0 commit comments

Comments
(0)