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
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(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(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(6, addressType);
}));
// Try calling resolve with an unsupported type.
assert.throws(function() {
dns.resolve('www.google.com', 'HI');
}, /Unknown type/);
assert.throws(() => dns.resolve('www.google.com', 'HI'), /Unknown type/);
// Try calling resolve with an unsupported type that's an object key
assert.throws(function() {
dns.resolve('www.google.com', 'toString');
}, /Unknown type/);
assert.throws(() => dns.resolve('www.google.com', 'toString'), /Unknown type/);
// Windows doesn't usually have an entry for localhost 127.0.0.1 in
// C:\Windows\System32\drivers\etc\hosts

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

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

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

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

144
test/parallel/test-dns.js

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

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

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

8
test/parallel/test-eval.js

@ -4,10 +4,12 @@ const util = require('util');
const assert = require('assert');
const exec = require('child_process').exec;
const cmd = ['"' + process.execPath + '"', '-e',
'"console.error(process.argv)"', 'foo', 'bar'].join(' ');
const cmd = [
`"${process.execPath}"`, '-e',
'"console.error(process.argv)"',
'foo', 'bar'].join(' ');
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.strictEqual(stderr, expected);
}));

19
test/parallel/test-global.js

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