Skip to content

Commit 1a4207d

Browse files
jasnellFishrock123
authored andcommitted
test: various test improvements
* Favor strictEqual * Use const where appropriate * Modernize where possible PR-URL: #8468 Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
1 parent 443bede commit 1a4207d

File tree

8 files changed

+99
-138
lines changed

8 files changed

+99
-138
lines changed

test/parallel/test-c-ares.js

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,30 +7,29 @@ const dns = require('dns');
77

88
// Try resolution without callback
99

10-
dns.lookup(null, common.mustCall(function(error, result, addressType) {
10+
dns.lookup(null, common.mustCall((error, result, addressType) => {
11+
assert.ifError(error);
1112
assert.strictEqual(null, result);
1213
assert.strictEqual(4, addressType);
1314
}));
1415

15-
dns.lookup('127.0.0.1', common.mustCall(function(error, result, addressType) {
16+
dns.lookup('127.0.0.1', common.mustCall((error, result, addressType) => {
17+
assert.ifError(error);
1618
assert.strictEqual('127.0.0.1', result);
1719
assert.strictEqual(4, addressType);
1820
}));
1921

20-
dns.lookup('::1', common.mustCall(function(error, result, addressType) {
22+
dns.lookup('::1', common.mustCall((error, result, addressType) => {
23+
assert.ifError(error);
2124
assert.strictEqual('::1', result);
2225
assert.strictEqual(6, addressType);
2326
}));
2427

2528
// Try calling resolve with an unsupported type.
26-
assert.throws(function() {
27-
dns.resolve('www.google.com', 'HI');
28-
}, /Unknown type/);
29+
assert.throws(() => dns.resolve('www.google.com', 'HI'), /Unknown type/);
2930

3031
// Try calling resolve with an unsupported type that's an object key
31-
assert.throws(function() {
32-
dns.resolve('www.google.com', 'toString');
33-
}, /Unknown type/);
32+
assert.throws(() => dns.resolve('www.google.com', 'toString'), /Unknown type/);
3433

3534
// Windows doesn't usually have an entry for localhost 127.0.0.1 in
3635
// C:\Windows\System32\drivers\etc\hosts

test/parallel/test-dns-lookup-cb-error.js

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,23 @@
11
'use strict';
2-
require('../common');
3-
var assert = require('assert');
4-
var cares = process.binding('cares_wrap');
2+
const common = require('../common');
3+
const assert = require('assert');
4+
const cares = process.binding('cares_wrap');
55

6-
var dns = require('dns');
6+
const dns = require('dns');
77

88
// Stub `getaddrinfo` to *always* error.
99
cares.getaddrinfo = function() {
1010
return process.binding('uv').UV_ENOENT;
1111
};
1212

13-
assert.doesNotThrow(function() {
13+
assert.doesNotThrow(() => {
1414
var tickValue = 0;
1515

16-
dns.lookup('example.com', function(error, result, addressType) {
17-
assert.equal(tickValue, 1);
18-
assert.equal(error.code, 'ENOENT');
19-
});
16+
dns.lookup('example.com', common.mustCall((error, result, addressType) => {
17+
assert(error);
18+
assert.strictEqual(tickValue, 1);
19+
assert.strictEqual(error.code, 'ENOENT');
20+
}));
2021

2122
// Make sure that the error callback is called
2223
// on next tick.
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict';
2-
var common = require('../common');
3-
var dns = require('dns');
2+
const common = require('../common');
3+
const dns = require('dns');
44

55
// Should not segfault, see #6244.
6-
dns.resolve4('127.0.0.1', common.mustCall(function() { }));
6+
dns.resolve4('127.0.0.1', common.mustCall(() => { }));
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
'use strict';
22
require('../common');
3-
var assert = require('assert');
4-
var dns = require('dns');
3+
const assert = require('assert');
4+
const dns = require('dns');
55

66
// Should not raise assertion error. Issue #7070
7-
assert.throws(function() { dns.resolveNs([]); }); // bad name
8-
assert.throws(function() { dns.resolveNs(''); }); // bad callback
7+
assert.throws(() => dns.resolveNs([])); // bad name
8+
assert.throws(() => dns.resolveNs('')); // bad callback

test/parallel/test-dns.js

Lines changed: 51 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const assert = require('assert');
44

55
const dns = require('dns');
66

7-
var existing = dns.getServers();
7+
const existing = dns.getServers();
88
assert(existing.length);
99

1010
// Verify that setServers() handles arrays with holes and other oddities
@@ -36,86 +36,71 @@ assert.doesNotThrow(() => {
3636

3737
function noop() {}
3838

39-
var goog = [
39+
const goog = [
4040
'8.8.8.8',
4141
'8.8.4.4',
4242
];
43-
assert.doesNotThrow(function() { dns.setServers(goog); });
43+
assert.doesNotThrow(() => dns.setServers(goog));
4444
assert.deepStrictEqual(dns.getServers(), goog);
45-
assert.throws(function() { dns.setServers(['foobar']); });
45+
assert.throws(() => dns.setServers(['foobar']));
4646
assert.deepStrictEqual(dns.getServers(), goog);
4747

48-
var goog6 = [
48+
const goog6 = [
4949
'2001:4860:4860::8888',
5050
'2001:4860:4860::8844',
5151
];
52-
assert.doesNotThrow(function() { dns.setServers(goog6); });
52+
assert.doesNotThrow(() => dns.setServers(goog6));
5353
assert.deepStrictEqual(dns.getServers(), goog6);
5454

5555
goog6.push('4.4.4.4');
5656
dns.setServers(goog6);
5757
assert.deepStrictEqual(dns.getServers(), goog6);
5858

59-
var ports = [
59+
const ports = [
6060
'4.4.4.4:53',
6161
'[2001:4860:4860::8888]:53',
6262
];
63-
var portsExpected = [
63+
const portsExpected = [
6464
'4.4.4.4',
6565
'2001:4860:4860::8888',
6666
];
6767
dns.setServers(ports);
6868
assert.deepStrictEqual(dns.getServers(), portsExpected);
6969

70-
assert.doesNotThrow(function() { dns.setServers([]); });
70+
assert.doesNotThrow(() => dns.setServers([]));
7171
assert.deepStrictEqual(dns.getServers(), []);
7272

73-
assert.throws(function() {
73+
assert.throws(() => {
7474
dns.resolve('test.com', [], noop);
7575
}, function(err) {
7676
return !(err instanceof TypeError);
7777
}, 'Unexpected error');
7878

7979
// dns.lookup should accept falsey and string values
80-
assert.throws(function() {
81-
dns.lookup({}, noop);
82-
}, 'invalid arguments: hostname must be a string or falsey');
80+
assert.throws(() => dns.lookup({}, noop),
81+
'invalid arguments: hostname must be a string or falsey');
8382

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

88-
assert.throws(function() {
89-
dns.lookup(true, noop);
90-
}, 'invalid arguments: hostname must be a string or falsey');
86+
assert.throws(() => dns.lookup(true, noop),
87+
'invalid arguments: hostname must be a string or falsey');
9188

92-
assert.throws(function() {
93-
dns.lookup(1, noop);
94-
}, 'invalid arguments: hostname must be a string or falsey');
89+
assert.throws(() => dns.lookup(1, noop),
90+
'invalid arguments: hostname must be a string or falsey');
9591

96-
assert.throws(function() {
97-
dns.lookup(noop, noop);
98-
}, 'invalid arguments: hostname must be a string or falsey');
92+
assert.throws(() => dns.lookup(noop, noop),
93+
'invalid arguments: hostname must be a string or falsey');
9994

100-
assert.doesNotThrow(function() {
101-
dns.lookup('', noop);
102-
});
95+
assert.doesNotThrow(() => dns.lookup('', noop));
10396

104-
assert.doesNotThrow(function() {
105-
dns.lookup(null, noop);
106-
});
97+
assert.doesNotThrow(() => dns.lookup(null, noop));
10798

108-
assert.doesNotThrow(function() {
109-
dns.lookup(undefined, noop);
110-
});
99+
assert.doesNotThrow(() => dns.lookup(undefined, noop));
111100

112-
assert.doesNotThrow(function() {
113-
dns.lookup(0, noop);
114-
});
101+
assert.doesNotThrow(() => dns.lookup(0, noop));
115102

116-
assert.doesNotThrow(function() {
117-
dns.lookup(NaN, noop);
118-
});
103+
assert.doesNotThrow(() => dns.lookup(NaN, noop));
119104

120105
/*
121106
* Make sure that dns.lookup throws if hints does not represent a valid flag.
@@ -126,85 +111,58 @@ assert.doesNotThrow(function() {
126111
* - it's an odd number different than 1, and thus is invalid, because
127112
* flags are either === 1 or even.
128113
*/
129-
assert.throws(function() {
114+
assert.throws(() => {
130115
dns.lookup('www.google.com', { hints: (dns.V4MAPPED | dns.ADDRCONFIG) + 1 },
131116
noop);
132117
});
133118

134-
assert.throws(function() {
135-
dns.lookup('www.google.com');
136-
}, 'invalid arguments: callback must be passed');
119+
assert.throws(() => dns.lookup('www.google.com'),
120+
'invalid arguments: callback must be passed');
137121

138-
assert.throws(function() {
139-
dns.lookup('www.google.com', 4);
140-
}, 'invalid arguments: callback must be passed');
122+
assert.throws(() => dns.lookup('www.google.com', 4),
123+
'invalid arguments: callback must be passed');
141124

142-
assert.doesNotThrow(function() {
143-
dns.lookup('www.google.com', 6, noop);
144-
});
125+
assert.doesNotThrow(() => dns.lookup('www.google.com', 6, noop));
145126

146-
assert.doesNotThrow(function() {
147-
dns.lookup('www.google.com', {}, noop);
148-
});
127+
assert.doesNotThrow(() => dns.lookup('www.google.com', {}, noop));
149128

150-
assert.doesNotThrow(function() {
151-
dns.lookup('', {
152-
family: 4,
153-
hints: 0
154-
}, noop);
155-
});
129+
assert.doesNotThrow(() => dns.lookup('', {family: 4, hints: 0}, noop));
156130

157-
assert.doesNotThrow(function() {
131+
assert.doesNotThrow(() => {
158132
dns.lookup('', {
159133
family: 6,
160134
hints: dns.ADDRCONFIG
161135
}, noop);
162136
});
163137

164-
assert.doesNotThrow(function() {
165-
dns.lookup('', {
166-
hints: dns.V4MAPPED
167-
}, noop);
168-
});
138+
assert.doesNotThrow(() => dns.lookup('', {hints: dns.V4MAPPED}, noop));
169139

170-
assert.doesNotThrow(function() {
140+
assert.doesNotThrow(() => {
171141
dns.lookup('', {
172142
hints: dns.ADDRCONFIG | dns.V4MAPPED
173143
}, noop);
174144
});
175145

176-
assert.throws(function() {
177-
dns.lookupService('0.0.0.0');
178-
}, /Invalid arguments/);
146+
assert.throws(() => dns.lookupService('0.0.0.0'), /Invalid arguments/);
179147

180-
assert.throws(function() {
181-
dns.lookupService('fasdfdsaf', 0, noop);
182-
}, /"host" argument needs to be a valid IP address/);
148+
assert.throws(() => dns.lookupService('fasdfdsaf', 0, noop),
149+
/"host" argument needs to be a valid IP address/);
183150

184-
assert.doesNotThrow(function() {
185-
dns.lookupService('0.0.0.0', '0', noop);
186-
});
151+
assert.doesNotThrow(() => dns.lookupService('0.0.0.0', '0', noop));
187152

188-
assert.doesNotThrow(function() {
189-
dns.lookupService('0.0.0.0', 0, noop);
190-
});
153+
assert.doesNotThrow(() => dns.lookupService('0.0.0.0', 0, noop));
191154

192-
assert.throws(function() {
193-
dns.lookupService('0.0.0.0', null, noop);
194-
}, /"port" should be >= 0 and < 65536, got "null"/);
155+
assert.throws(() => dns.lookupService('0.0.0.0', null, noop),
156+
/"port" should be >= 0 and < 65536, got "null"/);
195157

196-
assert.throws(function() {
197-
dns.lookupService('0.0.0.0', undefined, noop);
198-
}, /"port" should be >= 0 and < 65536, got "undefined"/);
158+
assert.throws(() => dns.lookupService('0.0.0.0', undefined, noop),
159+
/"port" should be >= 0 and < 65536, got "undefined"/);
199160

200-
assert.throws(function() {
201-
dns.lookupService('0.0.0.0', 65538, noop);
202-
}, /"port" should be >= 0 and < 65536, got "65538"/);
161+
assert.throws(() => dns.lookupService('0.0.0.0', 65538, noop),
162+
/"port" should be >= 0 and < 65536, got "65538"/);
203163

204-
assert.throws(function() {
205-
dns.lookupService('0.0.0.0', 'test', noop);
206-
}, /"port" should be >= 0 and < 65536, got "test"/);
164+
assert.throws(() => dns.lookupService('0.0.0.0', 'test', noop),
165+
/"port" should be >= 0 and < 65536, got "test"/);
207166

208-
assert.throws(() => {
209-
dns.lookupService('0.0.0.0', 80, null);
210-
}, /^TypeError: "callback" argument must be a function$/);
167+
assert.throws(() => dns.lookupService('0.0.0.0', 80, null),
168+
/^TypeError: "callback" argument must be a function$/);

test/parallel/test-eval-require.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
'use strict';
2-
var common = require('../common');
3-
var assert = require('assert');
4-
var spawn = require('child_process').spawn;
2+
const common = require('../common');
3+
const assert = require('assert');
4+
const spawn = require('child_process').spawn;
55

6-
var options = {
6+
const options = {
77
cwd: common.fixturesDir
88
};
9-
var child = spawn(process.execPath, ['-e', 'require("foo")'], options);
10-
child.on('exit', function(code) {
11-
assert.equal(code, 0);
12-
});
9+
const child = spawn(process.execPath, ['-e', 'require("foo")'], options);
10+
child.on('exit', common.mustCall((code) => {
11+
assert.strictEqual(code, 0);
12+
}));

test/parallel/test-eval.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@ const util = require('util');
44
const assert = require('assert');
55
const exec = require('child_process').exec;
66

7-
const cmd = ['"' + process.execPath + '"', '-e',
8-
'"console.error(process.argv)"', 'foo', 'bar'].join(' ');
7+
const cmd = [
8+
`"${process.execPath}"`, '-e',
9+
'"console.error(process.argv)"',
10+
'foo', 'bar'].join(' ');
911
const expected = util.format([process.execPath, 'foo', 'bar']) + '\n';
10-
exec(cmd, common.mustCall(function(err, stdout, stderr) {
12+
exec(cmd, common.mustCall((err, stdout, stderr) => {
1113
assert.ifError(err);
1214
assert.strictEqual(stderr, expected);
1315
}));

test/parallel/test-global.js

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,23 @@
11
/* eslint-disable strict */
2-
var common = require('../common');
3-
var path = require('path');
4-
var assert = require('assert');
2+
const common = require('../common');
3+
const path = require('path');
4+
const assert = require('assert');
55

66
common.globalCheck = false;
77

88
baseFoo = 'foo'; // eslint-disable-line no-undef
99
global.baseBar = 'bar';
1010

11-
assert.equal('foo', global.baseFoo, 'x -> global.x in base level not working');
11+
assert.strictEqual('foo', global.baseFoo,
12+
'x -> global.x in base level not working');
1213

13-
assert.equal('bar',
14-
baseBar, // eslint-disable-line no-undef
15-
'global.x -> x in base level not working');
14+
assert.strictEqual('bar',
15+
baseBar, // eslint-disable-line no-undef
16+
'global.x -> x in base level not working');
1617

1718
var module = require(path.join(common.fixturesDir, 'global', 'plain'));
1819
const fooBar = module.fooBar;
1920

20-
assert.equal('foo', fooBar.foo, 'x -> global.x in sub level not working');
21+
assert.strictEqual('foo', fooBar.foo, 'x -> global.x in sub level not working');
2122

22-
assert.equal('bar', fooBar.bar, 'global.x -> x in sub level not working');
23+
assert.strictEqual('bar', fooBar.bar, 'global.x -> x in sub level not working');

0 commit comments

Comments
 (0)