Skip to content

Commit

Permalink
test: replace assert.throws w/ common.expectsError
Browse files Browse the repository at this point in the history
PR-URL: #17497
Reviewed-By: Ruben Bridgewater <[email protected]>
Reviewed-By: Jon Moss <[email protected]>
Reviewed-By: Luigi Pinca <[email protected]>
  • Loading branch information
mithunsasidharan authored and MylesBorins committed Dec 12, 2017
1 parent c2ff36e commit f399667
Show file tree
Hide file tree
Showing 11 changed files with 47 additions and 51 deletions.
28 changes: 14 additions & 14 deletions test/parallel/test-dns.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,16 +68,16 @@ const goog = [
];
assert.doesNotThrow(() => dns.setServers(goog));
assert.deepStrictEqual(dns.getServers(), goog);
assert.throws(() => dns.setServers(['foobar']), common.expectsError({
common.expectsError(() => dns.setServers(['foobar']), {
code: 'ERR_INVALID_IP_ADDRESS',
type: Error,
message: 'Invalid IP address: foobar'
}));
assert.throws(() => dns.setServers(['127.0.0.1:va']), common.expectsError({
});
common.expectsError(() => dns.setServers(['127.0.0.1:va']), {
code: 'ERR_INVALID_IP_ADDRESS',
type: Error,
message: 'Invalid IP address: 127.0.0.1:va'
}));
});
assert.deepStrictEqual(dns.getServers(), goog);

const goog6 = [
Expand Down Expand Up @@ -109,14 +109,14 @@ assert.deepStrictEqual(dns.getServers(), portsExpected);
assert.doesNotThrow(() => dns.setServers([]));
assert.deepStrictEqual(dns.getServers(), []);

assert.throws(() => {
common.expectsError(() => {
dns.resolve('example.com', [], common.mustNotCall());
}, common.expectsError({
}, {
code: 'ERR_INVALID_ARG_TYPE',
type: TypeError,
message: 'The "rrtype" argument must be of type string. ' +
'Received type object'
}));
});

// dns.lookup should accept only falsey and string values
{
Expand Down Expand Up @@ -167,24 +167,24 @@ assert.throws(() => {
* - it's an odd number different than 1, and thus is invalid, because
* flags are either === 1 or even.
*/
assert.throws(() => {
common.expectsError(() => {
dns.lookup('nodejs.org', { hints: (dns.V4MAPPED | dns.ADDRCONFIG) + 1 },
common.mustNotCall());
}, common.expectsError({
}, {
code: 'ERR_INVALID_OPT_VALUE',
type: TypeError,
message: /The value "\d+" is invalid for option "hints"/
}));
});

assert.throws(() => dns.lookup('nodejs.org'), common.expectsError({
common.expectsError(() => dns.lookup('nodejs.org'), {
code: 'ERR_INVALID_CALLBACK',
type: TypeError
}));
});

assert.throws(() => dns.lookup('nodejs.org', 4), common.expectsError({
common.expectsError(() => dns.lookup('nodejs.org', 4), {
code: 'ERR_INVALID_CALLBACK',
type: TypeError
}));
});

assert.doesNotThrow(() => dns.lookup('', { family: 4, hints: 0 },
common.mustCall()));
Expand Down
4 changes: 2 additions & 2 deletions test/parallel/test-fs-realpath.js
Original file line number Diff line number Diff line change
Expand Up @@ -225,9 +225,9 @@ function test_cyclic_link_protection(realpath, realpathSync, callback) {
fs.symlinkSync(t[1], t[0], 'dir');
unlink.push(t[0]);
});
assert.throws(() => {
common.expectsError(() => {
realpathSync(entry);
}, common.expectsError({ code: 'ELOOP', type: Error }));
}, { code: 'ELOOP', type: Error });
asynctest(
realpath, [entry], callback, common.mustCall(function(err, result) {
assert.strictEqual(err.path, entry);
Expand Down
4 changes: 2 additions & 2 deletions test/parallel/test-fs-watchfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ common.expectsError(
type: TypeError
});

assert.throws(function() {
common.expectsError(function() {
fs.watchFile(new Object(), common.mustNotCall());
}, common.expectsError({ code: 'ERR_INVALID_ARG_TYPE', type: TypeError }));
}, { code: 'ERR_INVALID_ARG_TYPE', type: TypeError });

const enoentFile = path.join(common.tmpDir, 'non-existent-file');
const expectedStatObject = new fs.Stats(
Expand Down
7 changes: 3 additions & 4 deletions test/parallel/test-http-client-check-http-token.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
'use strict';
const common = require('../common');
const assert = require('assert');
const http = require('http');
const Countdown = require('../common/countdown');

Expand All @@ -18,14 +17,14 @@ const server = http.createServer(common.mustCall((req, res) => {

server.listen(0, common.mustCall(() => {
expectedFails.forEach((method) => {
assert.throws(() => {
common.expectsError(() => {
http.request({ method, path: '/' }, common.mustNotCall());
}, common.expectsError({
}, {
code: 'ERR_INVALID_ARG_TYPE',
type: TypeError,
message: 'The "method" argument must be of type string. ' +
`Received type ${typeof method}`
}));
});
});

expectedSuccesses.forEach((method) => {
Expand Down
6 changes: 3 additions & 3 deletions test/parallel/test-http-client-reject-unexpected-agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,14 @@ server.listen(0, baseOptions.host, common.mustCall(function() {
baseOptions.port = this.address().port;

failingAgentOptions.forEach((agent) => {
assert.throws(
common.expectsError(
() => createRequest(agent),
common.expectsError({
{
code: 'ERR_INVALID_ARG_TYPE',
type: TypeError,
message: 'The "Agent option" argument must be one of type ' +
'Agent-like Object, undefined, or false'
})
}
);
});

Expand Down
7 changes: 3 additions & 4 deletions test/parallel/test-http-client-unescaped-path.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,16 @@

'use strict';
const common = require('../common');
const assert = require('assert');
const http = require('http');

for (let i = 0; i <= 32; i += 1) {
const path = `bad${String.fromCharCode(i)}path`;
assert.throws(
common.expectsError(
() => http.get({ path }, common.mustNotCall()),
common.expectsError({
{
code: 'ERR_UNESCAPED_CHARACTERS',
type: TypeError,
message: 'Request path contains unescaped characters'
})
}
);
}
12 changes: 6 additions & 6 deletions test/parallel/test-http-hostname-typechecking.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,26 @@ const http = require('http');
const vals = [{}, [], NaN, Infinity, -Infinity, true, false, 1, 0, new Date()];

vals.forEach((v) => {
assert.throws(
common.expectsError(
() => http.request({ hostname: v }),
common.expectsError({
{
code: 'ERR_INVALID_ARG_TYPE',
type: TypeError,
message: 'The "options.hostname" property must be one of ' +
'type string, undefined, or null. ' +
`Received type ${typeof v}`
})
}
);

assert.throws(
common.expectsError(
() => http.request({ host: v }),
common.expectsError({
{
code: 'ERR_INVALID_ARG_TYPE',
type: TypeError,
message: 'The "options.host" property must be one of ' +
'type string, undefined, or null. ' +
`Received type ${typeof v}`
})
}
);
});

Expand Down
7 changes: 3 additions & 4 deletions test/parallel/test-http-request-invalid-method-error.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
'use strict';
const common = require('../common');
const assert = require('assert');
const http = require('http');

assert.throws(
common.expectsError(
() => http.request({ method: '\0' }),
common.expectsError({
{
code: 'ERR_INVALID_HTTP_TOKEN',
type: TypeError,
message: 'Method must be a valid HTTP token ["\u0000"]'
})
}
);
12 changes: 6 additions & 6 deletions test/parallel/test-http-response-statuscode.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,12 @@ const server = http.Server(common.mustCall(function(req, res) {
test(res, '404 this is not valid either', '404 this is not valid either');
break;
case 12:
assert.throws(() => { res.writeHead(); },
common.expectsError({
code: 'ERR_HTTP_INVALID_STATUS_CODE',
type: RangeError,
message: 'Invalid status code: undefined'
}));
common.expectsError(() => { res.writeHead(); },
{
code: 'ERR_HTTP_INVALID_STATUS_CODE',
type: RangeError,
message: 'Invalid status code: undefined'
});
this.close();
break;
default:
Expand Down
4 changes: 2 additions & 2 deletions test/parallel/test-http-server-de-chunked-trailer.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ const server = http.createServer(common.mustCall(function(req, res) {
message: 'Trailers are invalid with this transfer encoding',
type: Error
};
assert.throws(() => res.writeHead(200, { 'Content-Length': '2' }),
common.expectsError(trailerInvalidErr));
common.expectsError(() => res.writeHead(200, { 'Content-Length': '2' }),
trailerInvalidErr);
res.removeHeader('Trailer');
res.end('ok');
}));
Expand Down
7 changes: 3 additions & 4 deletions test/parallel/test-http-write-head.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,13 @@ const s = http.createServer(common.mustCall((req, res) => {

res.writeHead(200, { Test: '2' });

assert.throws(() => {
common.expectsError(() => {
res.writeHead(100, {});
}, common.expectsError({
}, {
code: 'ERR_HTTP_HEADERS_SENT',
type: Error,
message: 'Cannot render headers after they are sent to the client'
})
);
});

res.end();
}));
Expand Down

0 comments on commit f399667

Please sign in to comment.