Browse Source

test: various test improvements

* Favor strictEqual
* Use const where appropriate
* Modernize where possible

PR-URL: https://github.com/nodejs/node/pull/8468
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
v7.x
James M Snell 8 years ago
parent
commit
dd1c0539db
  1. 17
      test/parallel/test-c-ares.js
  2. 19
      test/parallel/test-dns-lookup-cb-error.js
  3. 6
      test/parallel/test-dns-regress-6244.js
  4. 8
      test/parallel/test-dns-regress-7070.js
  5. 144
      test/parallel/test-dns.js
  6. 16
      test/parallel/test-eval-require.js
  7. 8
      test/parallel/test-eval.js
  8. 19
      test/parallel/test-global.js

17
test/parallel/test-c-ares.js

@ -7,30 +7,29 @@ const dns = require('dns');
// Try resolution without callback // Try resolution without callback
dns.lookup(null, common.mustCall(function(error, result, addressType) { dns.lookup(null, common.mustCall((error, result, addressType) => {
assert.ifError(error);
assert.strictEqual(null, result); assert.strictEqual(null, result);
assert.strictEqual(4, addressType); assert.strictEqual(4, addressType);
})); }));
dns.lookup('127.0.0.1', common.mustCall(function(error, result, addressType) { dns.lookup('127.0.0.1', common.mustCall((error, result, addressType) => {
assert.ifError(error);
assert.strictEqual('127.0.0.1', result); assert.strictEqual('127.0.0.1', result);
assert.strictEqual(4, addressType); assert.strictEqual(4, addressType);
})); }));
dns.lookup('::1', common.mustCall(function(error, result, addressType) { dns.lookup('::1', common.mustCall((error, result, addressType) => {
assert.ifError(error);
assert.strictEqual('::1', result); assert.strictEqual('::1', result);
assert.strictEqual(6, addressType); assert.strictEqual(6, addressType);
})); }));
// Try calling resolve with an unsupported type. // Try calling resolve with an unsupported type.
assert.throws(function() { assert.throws(() => dns.resolve('www.google.com', 'HI'), /Unknown type/);
dns.resolve('www.google.com', 'HI');
}, /Unknown type/);
// Try calling resolve with an unsupported type that's an object key // Try calling resolve with an unsupported type that's an object key
assert.throws(function() { assert.throws(() => dns.resolve('www.google.com', 'toString'), /Unknown type/);
dns.resolve('www.google.com', 'toString');
}, /Unknown type/);
// Windows doesn't usually have an entry for localhost 127.0.0.1 in // Windows doesn't usually have an entry for localhost 127.0.0.1 in
// C:\Windows\System32\drivers\etc\hosts // C:\Windows\System32\drivers\etc\hosts

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

@ -1,22 +1,23 @@
'use strict'; 'use strict';
require('../common'); const common = require('../common');
var assert = require('assert'); const assert = require('assert');
var cares = process.binding('cares_wrap'); const cares = process.binding('cares_wrap');
var dns = require('dns'); const dns = require('dns');
// Stub `getaddrinfo` to *always* error. // Stub `getaddrinfo` to *always* error.
cares.getaddrinfo = function() { cares.getaddrinfo = function() {
return process.binding('uv').UV_ENOENT; return process.binding('uv').UV_ENOENT;
}; };
assert.doesNotThrow(function() { assert.doesNotThrow(() => {
var tickValue = 0; var tickValue = 0;
dns.lookup('example.com', function(error, result, addressType) { dns.lookup('example.com', common.mustCall((error, result, addressType) => {
assert.equal(tickValue, 1); assert(error);
assert.equal(error.code, 'ENOENT'); assert.strictEqual(tickValue, 1);
}); assert.strictEqual(error.code, 'ENOENT');
}));
// Make sure that the error callback is called // Make sure that the error callback is called
// on next tick. // on next tick.

6
test/parallel/test-dns-regress-6244.js

@ -1,6 +1,6 @@
'use strict'; 'use strict';
var common = require('../common'); const common = require('../common');
var dns = require('dns'); const dns = require('dns');
// Should not segfault, see #6244. // Should not segfault, see #6244.
dns.resolve4('127.0.0.1', common.mustCall(function() { })); dns.resolve4('127.0.0.1', common.mustCall(() => { }));

8
test/parallel/test-dns-regress-7070.js

@ -1,8 +1,8 @@
'use strict'; 'use strict';
require('../common'); require('../common');
var assert = require('assert'); const assert = require('assert');
var dns = require('dns'); const dns = require('dns');
// Should not raise assertion error. Issue #7070 // Should not raise assertion error. Issue #7070
assert.throws(function() { dns.resolveNs([]); }); // bad name assert.throws(() => dns.resolveNs([])); // bad name
assert.throws(function() { dns.resolveNs(''); }); // bad callback assert.throws(() => dns.resolveNs('')); // bad callback

144
test/parallel/test-dns.js

@ -4,7 +4,7 @@ const assert = require('assert');
const dns = require('dns'); const dns = require('dns');
var existing = dns.getServers(); const existing = dns.getServers();
assert(existing.length); assert(existing.length);
// Verify that setServers() handles arrays with holes and other oddities // Verify that setServers() handles arrays with holes and other oddities
@ -36,86 +36,71 @@ assert.doesNotThrow(() => {
function noop() {} function noop() {}
var goog = [ const goog = [
'8.8.8.8', '8.8.8.8',
'8.8.4.4', '8.8.4.4',
]; ];
assert.doesNotThrow(function() { dns.setServers(goog); }); assert.doesNotThrow(() => dns.setServers(goog));
assert.deepStrictEqual(dns.getServers(), goog); assert.deepStrictEqual(dns.getServers(), goog);
assert.throws(function() { dns.setServers(['foobar']); }); assert.throws(() => dns.setServers(['foobar']));
assert.deepStrictEqual(dns.getServers(), goog); assert.deepStrictEqual(dns.getServers(), goog);
var goog6 = [ const goog6 = [
'2001:4860:4860::8888', '2001:4860:4860::8888',
'2001:4860:4860::8844', '2001:4860:4860::8844',
]; ];
assert.doesNotThrow(function() { dns.setServers(goog6); }); assert.doesNotThrow(() => dns.setServers(goog6));
assert.deepStrictEqual(dns.getServers(), goog6); assert.deepStrictEqual(dns.getServers(), goog6);
goog6.push('4.4.4.4'); goog6.push('4.4.4.4');
dns.setServers(goog6); dns.setServers(goog6);
assert.deepStrictEqual(dns.getServers(), goog6); assert.deepStrictEqual(dns.getServers(), goog6);
var ports = [ const ports = [
'4.4.4.4:53', '4.4.4.4:53',
'[2001:4860:4860::8888]:53', '[2001:4860:4860::8888]:53',
]; ];
var portsExpected = [ const portsExpected = [
'4.4.4.4', '4.4.4.4',
'2001:4860:4860::8888', '2001:4860:4860::8888',
]; ];
dns.setServers(ports); dns.setServers(ports);
assert.deepStrictEqual(dns.getServers(), portsExpected); assert.deepStrictEqual(dns.getServers(), portsExpected);
assert.doesNotThrow(function() { dns.setServers([]); }); assert.doesNotThrow(() => dns.setServers([]));
assert.deepStrictEqual(dns.getServers(), []); assert.deepStrictEqual(dns.getServers(), []);
assert.throws(function() { assert.throws(() => {
dns.resolve('test.com', [], noop); dns.resolve('test.com', [], noop);
}, function(err) { }, function(err) {
return !(err instanceof TypeError); return !(err instanceof TypeError);
}, 'Unexpected error'); }, 'Unexpected error');
// dns.lookup should accept falsey and string values // dns.lookup should accept falsey and string values
assert.throws(function() { assert.throws(() => dns.lookup({}, noop),
dns.lookup({}, noop); 'invalid arguments: hostname must be a string or falsey');
}, 'invalid arguments: hostname must be a string or falsey');
assert.throws(function() { assert.throws(() => dns.lookup([], noop),
dns.lookup([], noop); 'invalid arguments: hostname must be a string or falsey');
}, 'invalid arguments: hostname must be a string or falsey');
assert.throws(function() { assert.throws(() => dns.lookup(true, noop),
dns.lookup(true, noop); 'invalid arguments: hostname must be a string or falsey');
}, 'invalid arguments: hostname must be a string or falsey');
assert.throws(function() { assert.throws(() => dns.lookup(1, noop),
dns.lookup(1, noop); 'invalid arguments: hostname must be a string or falsey');
}, 'invalid arguments: hostname must be a string or falsey');
assert.throws(function() { assert.throws(() => dns.lookup(noop, noop),
dns.lookup(noop, noop); 'invalid arguments: hostname must be a string or falsey');
}, 'invalid arguments: hostname must be a string or falsey');
assert.doesNotThrow(function() { assert.doesNotThrow(() => dns.lookup('', noop));
dns.lookup('', noop);
});
assert.doesNotThrow(function() { assert.doesNotThrow(() => dns.lookup(null, noop));
dns.lookup(null, noop);
});
assert.doesNotThrow(function() { assert.doesNotThrow(() => dns.lookup(undefined, noop));
dns.lookup(undefined, noop);
});
assert.doesNotThrow(function() { assert.doesNotThrow(() => dns.lookup(0, noop));
dns.lookup(0, noop);
});
assert.doesNotThrow(function() { assert.doesNotThrow(() => dns.lookup(NaN, noop));
dns.lookup(NaN, noop);
});
/* /*
* Make sure that dns.lookup throws if hints does not represent a valid flag. * Make sure that dns.lookup throws if hints does not represent a valid flag.
@ -126,85 +111,58 @@ assert.doesNotThrow(function() {
* - it's an odd number different than 1, and thus is invalid, because * - it's an odd number different than 1, and thus is invalid, because
* flags are either === 1 or even. * flags are either === 1 or even.
*/ */
assert.throws(function() { assert.throws(() => {
dns.lookup('www.google.com', { hints: (dns.V4MAPPED | dns.ADDRCONFIG) + 1 }, dns.lookup('www.google.com', { hints: (dns.V4MAPPED | dns.ADDRCONFIG) + 1 },
noop); noop);
}); });
assert.throws(function() { assert.throws(() => dns.lookup('www.google.com'),
dns.lookup('www.google.com'); 'invalid arguments: callback must be passed');
}, 'invalid arguments: callback must be passed');
assert.throws(function() { assert.throws(() => dns.lookup('www.google.com', 4),
dns.lookup('www.google.com', 4); 'invalid arguments: callback must be passed');
}, 'invalid arguments: callback must be passed');
assert.doesNotThrow(function() { assert.doesNotThrow(() => dns.lookup('www.google.com', 6, noop));
dns.lookup('www.google.com', 6, noop);
});
assert.doesNotThrow(function() { assert.doesNotThrow(() => dns.lookup('www.google.com', {}, noop));
dns.lookup('www.google.com', {}, noop);
});
assert.doesNotThrow(function() { assert.doesNotThrow(() => dns.lookup('', {family: 4, hints: 0}, noop));
dns.lookup('', {
family: 4,
hints: 0
}, noop);
});
assert.doesNotThrow(function() { assert.doesNotThrow(() => {
dns.lookup('', { dns.lookup('', {
family: 6, family: 6,
hints: dns.ADDRCONFIG hints: dns.ADDRCONFIG
}, noop); }, noop);
}); });
assert.doesNotThrow(function() { assert.doesNotThrow(() => dns.lookup('', {hints: dns.V4MAPPED}, noop));
dns.lookup('', {
hints: dns.V4MAPPED
}, noop);
});
assert.doesNotThrow(function() { assert.doesNotThrow(() => {
dns.lookup('', { dns.lookup('', {
hints: dns.ADDRCONFIG | dns.V4MAPPED hints: dns.ADDRCONFIG | dns.V4MAPPED
}, noop); }, noop);
}); });
assert.throws(function() { assert.throws(() => dns.lookupService('0.0.0.0'), /Invalid arguments/);
dns.lookupService('0.0.0.0');
}, /Invalid arguments/);
assert.throws(function() { assert.throws(() => dns.lookupService('fasdfdsaf', 0, noop),
dns.lookupService('fasdfdsaf', 0, noop); /"host" argument needs to be a valid IP address/);
}, /"host" argument needs to be a valid IP address/);
assert.doesNotThrow(function() { assert.doesNotThrow(() => dns.lookupService('0.0.0.0', '0', noop));
dns.lookupService('0.0.0.0', '0', noop);
});
assert.doesNotThrow(function() { assert.doesNotThrow(() => dns.lookupService('0.0.0.0', 0, noop));
dns.lookupService('0.0.0.0', 0, noop);
});
assert.throws(function() { assert.throws(() => dns.lookupService('0.0.0.0', null, noop),
dns.lookupService('0.0.0.0', null, noop); /"port" should be >= 0 and < 65536, got "null"/);
}, /"port" should be >= 0 and < 65536, got "null"/);
assert.throws(function() { assert.throws(() => dns.lookupService('0.0.0.0', undefined, noop),
dns.lookupService('0.0.0.0', undefined, noop); /"port" should be >= 0 and < 65536, got "undefined"/);
}, /"port" should be >= 0 and < 65536, got "undefined"/);
assert.throws(function() { assert.throws(() => dns.lookupService('0.0.0.0', 65538, noop),
dns.lookupService('0.0.0.0', 65538, noop); /"port" should be >= 0 and < 65536, got "65538"/);
}, /"port" should be >= 0 and < 65536, got "65538"/);
assert.throws(function() { assert.throws(() => dns.lookupService('0.0.0.0', 'test', noop),
dns.lookupService('0.0.0.0', 'test', noop); /"port" should be >= 0 and < 65536, got "test"/);
}, /"port" should be >= 0 and < 65536, got "test"/);
assert.throws(() => { assert.throws(() => dns.lookupService('0.0.0.0', 80, null),
dns.lookupService('0.0.0.0', 80, null); /^TypeError: "callback" argument must be a function$/);
}, /^TypeError: "callback" argument must be a function$/);

16
test/parallel/test-eval-require.js

@ -1,12 +1,12 @@
'use strict'; 'use strict';
var common = require('../common'); const common = require('../common');
var assert = require('assert'); const assert = require('assert');
var spawn = require('child_process').spawn; const spawn = require('child_process').spawn;
var options = { const options = {
cwd: common.fixturesDir cwd: common.fixturesDir
}; };
var child = spawn(process.execPath, ['-e', 'require("foo")'], options); const child = spawn(process.execPath, ['-e', 'require("foo")'], options);
child.on('exit', function(code) { child.on('exit', common.mustCall((code) => {
assert.equal(code, 0); assert.strictEqual(code, 0);
}); }));

8
test/parallel/test-eval.js

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

19
test/parallel/test-global.js

@ -1,22 +1,23 @@
/* eslint-disable strict */ /* eslint-disable strict */
var common = require('../common'); const common = require('../common');
var path = require('path'); const path = require('path');
var assert = require('assert'); const assert = require('assert');
common.globalCheck = false; common.globalCheck = false;
baseFoo = 'foo'; // eslint-disable-line no-undef baseFoo = 'foo'; // eslint-disable-line no-undef
global.baseBar = 'bar'; global.baseBar = 'bar';
assert.equal('foo', global.baseFoo, 'x -> global.x in base level not working'); assert.strictEqual('foo', global.baseFoo,
'x -> global.x in base level not working');
assert.equal('bar', assert.strictEqual('bar',
baseBar, // eslint-disable-line no-undef baseBar, // eslint-disable-line no-undef
'global.x -> x in base level not working'); 'global.x -> x in base level not working');
var module = require(path.join(common.fixturesDir, 'global', 'plain')); var module = require(path.join(common.fixturesDir, 'global', 'plain'));
const fooBar = module.fooBar; const fooBar = module.fooBar;
assert.equal('foo', fooBar.foo, 'x -> global.x in sub level not working'); assert.strictEqual('foo', fooBar.foo, 'x -> global.x in sub level not working');
assert.equal('bar', fooBar.bar, 'global.x -> x in sub level not working'); assert.strictEqual('bar', fooBar.bar, 'global.x -> x in sub level not working');

Loading…
Cancel
Save