From f399667784cdf2b4d5334db06c2ee1bdade8872a Mon Sep 17 00:00:00 2001 From: Mithun Sasidharan Date: Wed, 6 Dec 2017 21:08:06 +0530 Subject: [PATCH] test: replace assert.throws w/ common.expectsError PR-URL: https://github.com/nodejs/node/pull/17497 Reviewed-By: Ruben Bridgewater Reviewed-By: Jon Moss Reviewed-By: Luigi Pinca --- test/parallel/test-dns.js | 28 +++++++++---------- test/parallel/test-fs-realpath.js | 4 +-- test/parallel/test-fs-watchfile.js | 4 +-- .../test-http-client-check-http-token.js | 7 ++--- ...est-http-client-reject-unexpected-agent.js | 6 ++-- .../test-http-client-unescaped-path.js | 7 ++--- .../test-http-hostname-typechecking.js | 12 ++++---- .../test-http-request-invalid-method-error.js | 7 ++--- .../parallel/test-http-response-statuscode.js | 12 ++++---- .../test-http-server-de-chunked-trailer.js | 4 +-- test/parallel/test-http-write-head.js | 7 ++--- 11 files changed, 47 insertions(+), 51 deletions(-) diff --git a/test/parallel/test-dns.js b/test/parallel/test-dns.js index 08a26ab2184dd2..f0e4b29d7c9b7f 100644 --- a/test/parallel/test-dns.js +++ b/test/parallel/test-dns.js @@ -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 = [ @@ -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 { @@ -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())); diff --git a/test/parallel/test-fs-realpath.js b/test/parallel/test-fs-realpath.js index 4ea9bfdf48eb6f..cee45b2c899cdf 100644 --- a/test/parallel/test-fs-realpath.js +++ b/test/parallel/test-fs-realpath.js @@ -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); diff --git a/test/parallel/test-fs-watchfile.js b/test/parallel/test-fs-watchfile.js index f980d8f3fcc0c0..fe0d89c42e031f 100644 --- a/test/parallel/test-fs-watchfile.js +++ b/test/parallel/test-fs-watchfile.js @@ -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( diff --git a/test/parallel/test-http-client-check-http-token.js b/test/parallel/test-http-client-check-http-token.js index 7a0c894504b1b9..828d7fa79a503c 100644 --- a/test/parallel/test-http-client-check-http-token.js +++ b/test/parallel/test-http-client-check-http-token.js @@ -1,6 +1,5 @@ 'use strict'; const common = require('../common'); -const assert = require('assert'); const http = require('http'); const Countdown = require('../common/countdown'); @@ -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) => { diff --git a/test/parallel/test-http-client-reject-unexpected-agent.js b/test/parallel/test-http-client-reject-unexpected-agent.js index 50d80fd32a2861..407084c030e67d 100644 --- a/test/parallel/test-http-client-reject-unexpected-agent.js +++ b/test/parallel/test-http-client-reject-unexpected-agent.js @@ -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' - }) + } ); }); diff --git a/test/parallel/test-http-client-unescaped-path.js b/test/parallel/test-http-client-unescaped-path.js index f3c680919df001..6d5a945dd5f893 100644 --- a/test/parallel/test-http-client-unescaped-path.js +++ b/test/parallel/test-http-client-unescaped-path.js @@ -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' - }) + } ); } diff --git a/test/parallel/test-http-hostname-typechecking.js b/test/parallel/test-http-hostname-typechecking.js index dbb98b7ae11c5e..12fe72005a41a6 100644 --- a/test/parallel/test-http-hostname-typechecking.js +++ b/test/parallel/test-http-hostname-typechecking.js @@ -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}` - }) + } ); }); diff --git a/test/parallel/test-http-request-invalid-method-error.js b/test/parallel/test-http-request-invalid-method-error.js index 59706e90c7ca12..ae11985e12ff87 100644 --- a/test/parallel/test-http-request-invalid-method-error.js +++ b/test/parallel/test-http-request-invalid-method-error.js @@ -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"]' - }) + } ); diff --git a/test/parallel/test-http-response-statuscode.js b/test/parallel/test-http-response-statuscode.js index 17fce6c2d0f11e..28b091a3e8eddb 100644 --- a/test/parallel/test-http-response-statuscode.js +++ b/test/parallel/test-http-response-statuscode.js @@ -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: diff --git a/test/parallel/test-http-server-de-chunked-trailer.js b/test/parallel/test-http-server-de-chunked-trailer.js index 72c40ed5592690..dad744209ee419 100644 --- a/test/parallel/test-http-server-de-chunked-trailer.js +++ b/test/parallel/test-http-server-de-chunked-trailer.js @@ -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'); })); diff --git a/test/parallel/test-http-write-head.js b/test/parallel/test-http-write-head.js index 70ad5935a92690..2d1b8ff5504df0 100644 --- a/test/parallel/test-http-write-head.js +++ b/test/parallel/test-http-write-head.js @@ -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(); }));