From 094bfaf769f5b6bc8ca6fbcc289cdf5e0f07073f Mon Sep 17 00:00:00 2001 From: Anatoli Papirovski Date: Fri, 8 Dec 2017 16:41:38 -0500 Subject: [PATCH] test: replace assert.throws w/ common.expectsError PR-URL: https://github.com/nodejs/node/pull/17557 Reviewed-By: Ruben Bridgewater Reviewed-By: Colin Ihrig --- .../test-embedder.api.async-resource.js | 6 +- test/parallel/test-assert-fail.js | 2 + test/parallel/test-assert.js | 3 + test/parallel/test-buffer-alloc.js | 5 +- test/parallel/test-buffer-fill.js | 10 ++-- test/parallel/test-console-instance.js | 6 +- test/parallel/test-dns.js | 16 ++--- test/parallel/test-http-outgoing-proto.js | 60 +++++++++---------- ...est-http2-client-request-options-errors.js | 7 +-- test/parallel/test-http2-connect-method.js | 18 +++--- test/parallel/test-http2-respond-file-204.js | 7 +-- .../test-http2-server-push-disabled.js | 6 +- .../test-http2-server-rst-before-respond.js | 6 +- .../test-https-options-boolean-check.js | 12 ++-- test/parallel/test-internal-errors.js | 20 +++---- test/parallel/test-performance.js | 13 ++-- test/parallel/test-performanceobserver.js | 39 ++++++------ test/parallel/test-readline-interface.js | 24 ++++---- test/sequential/test-tls-lookup.js | 6 +- 19 files changed, 134 insertions(+), 132 deletions(-) diff --git a/test/async-hooks/test-embedder.api.async-resource.js b/test/async-hooks/test-embedder.api.async-resource.js index eeeaa447c9668c..89a889b20192e3 100644 --- a/test/async-hooks/test-embedder.api.async-resource.js +++ b/test/async-hooks/test-embedder.api.async-resource.js @@ -17,12 +17,12 @@ common.expectsError( code: 'ERR_INVALID_ARG_TYPE', type: TypeError, }); -assert.throws(() => { +common.expectsError(() => { new AsyncResource('invalid_trigger_id', { triggerAsyncId: null }); -}, common.expectsError({ +}, { code: 'ERR_INVALID_ASYNC_ID', type: RangeError, -})); +}); assert.strictEqual( new AsyncResource('default_trigger_id').triggerAsyncId(), diff --git a/test/parallel/test-assert-fail.js b/test/parallel/test-assert-fail.js index 14d28e5cd0045f..8d67a6e63f51f5 100644 --- a/test/parallel/test-assert-fail.js +++ b/test/parallel/test-assert-fail.js @@ -1,5 +1,7 @@ 'use strict'; +/* eslint-disable prefer-common-expectserror */ + const common = require('../common'); const assert = require('assert'); diff --git a/test/parallel/test-assert.js b/test/parallel/test-assert.js index 8c4d38ea8264ae..8bf9b0e6d341fb 100644 --- a/test/parallel/test-assert.js +++ b/test/parallel/test-assert.js @@ -20,6 +20,9 @@ // USE OR OTHER DEALINGS IN THE SOFTWARE. 'use strict'; + +/* eslint-disable prefer-common-expectserror */ + const common = require('../common'); const assert = require('assert'); const a = assert; diff --git a/test/parallel/test-buffer-alloc.js b/test/parallel/test-buffer-alloc.js index 272f8c20e1cdd8..4d16f1ba3d85d3 100644 --- a/test/parallel/test-buffer-alloc.js +++ b/test/parallel/test-buffer-alloc.js @@ -974,12 +974,11 @@ assert.strictEqual(SlowBuffer.prototype.offset, undefined); } // ParseArrayIndex() should reject values that don't fit in a 32 bits size_t. -assert.throws(() => { +common.expectsError(() => { const a = Buffer.alloc(1); const b = Buffer.alloc(1); a.copy(b, 0, 0x100000000, 0x100000001); -}, common.expectsError( - { code: undefined, type: RangeError, message: 'Index out of range' })); +}, { code: undefined, type: RangeError, message: 'Index out of range' }); // Unpooled buffer (replaces SlowBuffer) { diff --git a/test/parallel/test-buffer-fill.js b/test/parallel/test-buffer-fill.js index d33af46f724f0b..f72c26fd48ee28 100644 --- a/test/parallel/test-buffer-fill.js +++ b/test/parallel/test-buffer-fill.js @@ -424,21 +424,19 @@ common.expectsError(() => { // Testing process.binding. Make sure "end" is properly checked for -1 wrap // around. -assert.throws(() => { +common.expectsError(() => { process.binding('buffer').fill(Buffer.alloc(1), 1, 1, -2, 1); -}, common.expectsError( - { code: undefined, type: RangeError, message: 'Index out of range' })); +}, { code: undefined, type: RangeError, message: 'Index out of range' }); // Test that bypassing 'length' won't cause an abort. -assert.throws(() => { +common.expectsError(() => { const buf = new Buffer('w00t'); Object.defineProperty(buf, 'length', { value: 1337, enumerable: true }); buf.fill(''); -}, common.expectsError( - { code: undefined, type: RangeError, message: 'Index out of range' })); +}, { code: undefined, type: RangeError, message: 'Index out of range' }); assert.deepStrictEqual( Buffer.allocUnsafeSlow(16).fill('ab', 'utf16le'), diff --git a/test/parallel/test-console-instance.js b/test/parallel/test-console-instance.js index 9f31ebf3afcb58..7e931e0d3b30de 100644 --- a/test/parallel/test-console-instance.js +++ b/test/parallel/test-console-instance.js @@ -47,17 +47,17 @@ common.expectsError( ); // Console constructor should throw if stderr exists but is not writable -assert.throws( +common.expectsError( () => { out.write = () => {}; err.write = undefined; new Console(out, err); }, - common.expectsError({ + { code: 'ERR_CONSOLE_WRITABLE_STREAM', type: TypeError, message: /stderr/ - }) + } ); out.write = err.write = (d) => {}; diff --git a/test/parallel/test-dns.js b/test/parallel/test-dns.js index f0e4b29d7c9b7f..dba14b397bc27c 100644 --- a/test/parallel/test-dns.js +++ b/test/parallel/test-dns.js @@ -205,20 +205,20 @@ assert.doesNotThrow(() => { }, common.mustCall()); }); -assert.throws(() => dns.lookupService('0.0.0.0'), common.expectsError({ +common.expectsError(() => dns.lookupService('0.0.0.0'), { code: 'ERR_MISSING_ARGS', type: TypeError, message: 'The "host", "port", and "callback" arguments must be specified' -})); +}); const invalidHost = 'fasdfdsaf'; -assert.throws(() => { +common.expectsError(() => { dns.lookupService(invalidHost, 0, common.mustNotCall()); -}, common.expectsError({ +}, { code: 'ERR_INVALID_OPT_VALUE', type: TypeError, message: `The value "${invalidHost}" is invalid for option "host"` -})); +}); const portErr = (port) => { common.expectsError( @@ -238,9 +238,9 @@ portErr(undefined); portErr(65538); portErr('test'); -assert.throws(() => { +common.expectsError(() => { dns.lookupService('0.0.0.0', 80, null); -}, common.expectsError({ +}, { code: 'ERR_INVALID_CALLBACK', type: TypeError -})); +}); diff --git a/test/parallel/test-http-outgoing-proto.js b/test/parallel/test-http-outgoing-proto.js index 47dbb07e2befb1..6b7987db10d726 100644 --- a/test/parallel/test-http-outgoing-proto.js +++ b/test/parallel/test-http-outgoing-proto.js @@ -21,80 +21,80 @@ assert.strictEqual( typeof ServerResponse.prototype._implicitHeader, 'function'); // validateHeader -assert.throws(() => { +common.expectsError(() => { const outgoingMessage = new OutgoingMessage(); outgoingMessage.setHeader(); -}, common.expectsError({ +}, { code: 'ERR_INVALID_HTTP_TOKEN', type: TypeError, message: 'Header name must be a valid HTTP token ["undefined"]' -})); +}); -assert.throws(() => { +common.expectsError(() => { const outgoingMessage = new OutgoingMessage(); outgoingMessage.setHeader('test'); -}, common.expectsError({ +}, { code: 'ERR_HTTP_INVALID_HEADER_VALUE', type: TypeError, message: 'Invalid value "undefined" for header "test"' -})); +}); -assert.throws(() => { +common.expectsError(() => { const outgoingMessage = new OutgoingMessage(); outgoingMessage.setHeader(404); -}, common.expectsError({ +}, { code: 'ERR_INVALID_HTTP_TOKEN', type: TypeError, message: 'Header name must be a valid HTTP token ["404"]' -})); +}); -assert.throws(() => { +common.expectsError(() => { const outgoingMessage = new OutgoingMessage(); outgoingMessage.setHeader.call({ _header: 'test' }, 'test', 'value'); -}, common.expectsError({ +}, { code: 'ERR_HTTP_HEADERS_SENT', type: Error, message: 'Cannot set headers after they are sent to the client' -})); +}); -assert.throws(() => { +common.expectsError(() => { const outgoingMessage = new OutgoingMessage(); outgoingMessage.setHeader('200', 'あ'); -}, common.expectsError({ +}, { code: 'ERR_INVALID_CHAR', type: TypeError, message: 'Invalid character in header content ["200"]' -})); +}); // write -assert.throws(() => { +common.expectsError(() => { const outgoingMessage = new OutgoingMessage(); outgoingMessage.write(); -}, common.expectsError({ +}, { code: 'ERR_METHOD_NOT_IMPLEMENTED', type: Error, message: 'The _implicitHeader() method is not implemented' -})); +}); assert(OutgoingMessage.prototype.write.call({ _header: 'test' })); -assert.throws(() => { +common.expectsError(() => { const outgoingMessage = new OutgoingMessage(); outgoingMessage.write.call({ _header: 'test', _hasBody: 'test' }); -}, common.expectsError({ +}, { code: 'ERR_INVALID_ARG_TYPE', type: TypeError, message: 'The first argument must be one of type string or Buffer' -})); +}); -assert.throws(() => { +common.expectsError(() => { const outgoingMessage = new OutgoingMessage(); outgoingMessage.write.call({ _header: 'test', _hasBody: 'test' }, 1); -}, common.expectsError({ +}, { code: 'ERR_INVALID_ARG_TYPE', type: TypeError, message: 'The first argument must be one of type string or Buffer' -})); +}); // addTrailers() // The `Error` comes from the JavaScript engine so confirm that it is a @@ -105,20 +105,20 @@ assert.throws(() => { outgoingMessage.addTrailers(); }, TypeError); -assert.throws(() => { +common.expectsError(() => { const outgoingMessage = new OutgoingMessage(); outgoingMessage.addTrailers({ 'あ': 'value' }); -}, common.expectsError({ +}, { code: 'ERR_INVALID_HTTP_TOKEN', type: TypeError, message: 'Trailer name must be a valid HTTP token ["あ"]' -})); +}); -assert.throws(() => { +common.expectsError(() => { const outgoingMessage = new OutgoingMessage(); outgoingMessage.addTrailers({ 404: 'あ' }); -}, common.expectsError({ +}, { code: 'ERR_INVALID_CHAR', type: TypeError, message: 'Invalid character in trailer content ["404"]' -})); +}); diff --git a/test/parallel/test-http2-client-request-options-errors.js b/test/parallel/test-http2-client-request-options-errors.js index 5d3fc0ab5a1fd8..4b146a1bc8c2e4 100644 --- a/test/parallel/test-http2-client-request-options-errors.js +++ b/test/parallel/test-http2-client-request-options-errors.js @@ -3,7 +3,6 @@ const common = require('../common'); if (!common.hasCrypto) common.skip('missing crypto'); -const assert = require('assert'); const http2 = require('http2'); // Check if correct errors are emitted when wrong type of data is passed @@ -40,19 +39,19 @@ server.listen(0, common.mustCall(() => { return; } - assert.throws( + common.expectsError( () => client.request({ ':method': 'CONNECT', ':authority': `localhost:${port}` }, { [option]: types[type] }), - common.expectsError({ + { type: TypeError, code: 'ERR_INVALID_OPT_VALUE', message: `The value "${String(types[type])}" is invalid ` + `for option "${option}"` - }) + } ); }); }); diff --git a/test/parallel/test-http2-connect-method.js b/test/parallel/test-http2-connect-method.js index 78c9a345293c12..4d443d5c217421 100644 --- a/test/parallel/test-http2-connect-method.js +++ b/test/parallel/test-http2-connect-method.js @@ -55,36 +55,36 @@ server.listen(0, common.mustCall(() => { const client = http2.connect(`http://localhost:${proxy.address().port}`); // confirm that :authority is required and :scheme & :path are forbidden - assert.throws( + common.expectsError( () => client.request({ [HTTP2_HEADER_METHOD]: 'CONNECT' }), - common.expectsError({ + { code: 'ERR_HTTP2_CONNECT_AUTHORITY', message: ':authority header is required for CONNECT requests' - }) + } ); - assert.throws( + common.expectsError( () => client.request({ [HTTP2_HEADER_METHOD]: 'CONNECT', [HTTP2_HEADER_AUTHORITY]: `localhost:${port}`, [HTTP2_HEADER_SCHEME]: 'http' }), - common.expectsError({ + { code: 'ERR_HTTP2_CONNECT_SCHEME', message: 'The :scheme header is forbidden for CONNECT requests' - }) + } ); - assert.throws( + common.expectsError( () => client.request({ [HTTP2_HEADER_METHOD]: 'CONNECT', [HTTP2_HEADER_AUTHORITY]: `localhost:${port}`, [HTTP2_HEADER_PATH]: '/' }), - common.expectsError({ + { code: 'ERR_HTTP2_CONNECT_PATH', message: 'The :path header is forbidden for CONNECT requests' - }) + } ); // valid CONNECT request diff --git a/test/parallel/test-http2-respond-file-204.js b/test/parallel/test-http2-respond-file-204.js index 8181dbb317dab2..4be2d42c779a5e 100644 --- a/test/parallel/test-http2-respond-file-204.js +++ b/test/parallel/test-http2-respond-file-204.js @@ -5,7 +5,6 @@ if (!common.hasCrypto) common.skip('missing crypto'); const fixtures = require('../common/fixtures'); const http2 = require('http2'); -const assert = require('assert'); const { HTTP2_HEADER_CONTENT_TYPE, @@ -16,16 +15,16 @@ const fname = fixtures.path('elipses.txt'); const server = http2.createServer(); server.on('stream', (stream) => { - assert.throws(() => { + common.expectsError(() => { stream.respondWithFile(fname, { [HTTP2_HEADER_STATUS]: 204, [HTTP2_HEADER_CONTENT_TYPE]: 'text/plain' }); - }, common.expectsError({ + }, { code: 'ERR_HTTP2_PAYLOAD_FORBIDDEN', type: Error, message: 'Responses with 204 status must not have a payload' - })); + }); stream.respond({}); stream.end(); }); diff --git a/test/parallel/test-http2-server-push-disabled.js b/test/parallel/test-http2-server-push-disabled.js index c0148fe63b672e..33390f2ecae886 100644 --- a/test/parallel/test-http2-server-push-disabled.js +++ b/test/parallel/test-http2-server-push-disabled.js @@ -21,16 +21,16 @@ server.on('stream', common.mustCall((stream) => { // and pushStream() must throw. assert.strictEqual(stream.pushAllowed, false); - assert.throws(() => { + common.expectsError(() => { stream.pushStream({ ':scheme': 'http', ':path': '/foobar', ':authority': `localhost:${server.address().port}`, }, common.mustNotCall()); - }, common.expectsError({ + }, { code: 'ERR_HTTP2_PUSH_DISABLED', type: Error - })); + }); stream.respond({ ':status': 200 }); stream.end('test'); diff --git a/test/parallel/test-http2-server-rst-before-respond.js b/test/parallel/test-http2-server-rst-before-respond.js index 47ba68bd29ed81..950beea4eb39ab 100644 --- a/test/parallel/test-http2-server-rst-before-respond.js +++ b/test/parallel/test-http2-server-rst-before-respond.js @@ -14,15 +14,15 @@ server.on('stream', common.mustCall(onStream)); function onStream(stream, headers, flags) { stream.rstStream(); - assert.throws(() => { + common.expectsError(() => { stream.additionalHeaders({ ':status': 123, abc: 123 }); - }, common.expectsError({ + }, { code: 'ERR_HTTP2_INVALID_STREAM', message: /^The stream has been destroyed$/ - })); + }); } server.listen(0); diff --git a/test/parallel/test-https-options-boolean-check.js b/test/parallel/test-https-options-boolean-check.js index eae319988e7c6a..ed124d43fbb74f 100644 --- a/test/parallel/test-https-options-boolean-check.js +++ b/test/parallel/test-https-options-boolean-check.js @@ -101,16 +101,16 @@ const invalidCertRE = /^The "cert" argument must be one of type string, Buffer, [[keyStr, keyStr2], true, invalidCertRE], [true, [certBuff, certBuff2], invalidKeyRE] ].map((params) => { - assert.throws(() => { + common.expectsError(() => { https.createServer({ key: params[0], cert: params[1] }); - }, common.expectsError({ + }, { code: 'ERR_INVALID_ARG_TYPE', type: TypeError, message: params[2] - })); + }); }); // Checks to ensure https.createServer works with the CA parameter @@ -142,15 +142,15 @@ const invalidCertRE = /^The "cert" argument must be one of type string, Buffer, [keyBuff, certBuff, true], [keyBuff, certBuff, [caCert, true]] ].map((params) => { - assert.throws(() => { + common.expectsError(() => { https.createServer({ key: params[0], cert: params[1], ca: params[2] }); - }, common.expectsError({ + }, { code: 'ERR_INVALID_ARG_TYPE', type: TypeError, message: /^The "ca" argument must be one of type string, Buffer, TypedArray, or DataView$/ - })); + }); }); diff --git a/test/parallel/test-internal-errors.js b/test/parallel/test-internal-errors.js index 1b4abcf72c9ab3..78582a53504748 100644 --- a/test/parallel/test-internal-errors.js +++ b/test/parallel/test-internal-errors.js @@ -141,29 +141,29 @@ common.expectsError( // Tests for common.expectsError assert.doesNotThrow(() => { - assert.throws(() => { + common.expectsError(() => { throw new errors.TypeError('TEST_ERROR_1', 'a'); - }, common.expectsError({ code: 'TEST_ERROR_1' })); + }, { code: 'TEST_ERROR_1' }); }); assert.doesNotThrow(() => { - assert.throws(() => { + common.expectsError(() => { throw new errors.TypeError('TEST_ERROR_1', 'a'); - }, common.expectsError({ code: 'TEST_ERROR_1', - type: TypeError, - message: /^Error for testing/ })); + }, { code: 'TEST_ERROR_1', + type: TypeError, + message: /^Error for testing/ }); }); assert.doesNotThrow(() => { - assert.throws(() => { + common.expectsError(() => { throw new errors.TypeError('TEST_ERROR_1', 'a'); - }, common.expectsError({ code: 'TEST_ERROR_1', type: TypeError })); + }, { code: 'TEST_ERROR_1', type: TypeError }); }); assert.doesNotThrow(() => { - assert.throws(() => { + common.expectsError(() => { throw new errors.TypeError('TEST_ERROR_1', 'a'); - }, common.expectsError({ code: 'TEST_ERROR_1', type: Error })); + }, { code: 'TEST_ERROR_1', type: Error }); }); common.expectsError(() => { diff --git a/test/parallel/test-performance.js b/test/parallel/test-performance.js index 23ae4370c3cd7a..ba15479050f9e1 100644 --- a/test/parallel/test-performance.js +++ b/test/parallel/test-performance.js @@ -68,12 +68,13 @@ assert.strictEqual(typeof performance.timeOrigin, 'number'); }); [undefined, null, 'foo', 1].forEach((i) => { - assert.throws(() => performance.measure('test', 'A', i), - common.expectsError({ - code: 'ERR_INVALID_PERFORMANCE_MARK', - type: Error, - message: `The "${i}" performance mark has not been set` - })); + common.expectsError( + () => performance.measure('test', 'A', i), + { + code: 'ERR_INVALID_PERFORMANCE_MARK', + type: Error, + message: `The "${i}" performance mark has not been set` + }); }); performance.clearMeasures(); diff --git a/test/parallel/test-performanceobserver.js b/test/parallel/test-performanceobserver.js index 9b7dba5eb2a6f5..779e9740d7cc62 100644 --- a/test/parallel/test-performanceobserver.js +++ b/test/parallel/test-performanceobserver.js @@ -28,33 +28,34 @@ assert.strictEqual(counts[NODE_PERFORMANCE_ENTRY_TYPE_FUNCTION], 0); { [1, null, undefined, {}, [], Infinity].forEach((i) => { - assert.throws(() => new PerformanceObserver(i), - common.expectsError({ - code: 'ERR_INVALID_CALLBACK', - type: TypeError, - message: 'Callback must be a function' - })); + common.expectsError(() => new PerformanceObserver(i), + { + code: 'ERR_INVALID_CALLBACK', + type: TypeError, + message: 'Callback must be a function' + }); }); const observer = new PerformanceObserver(common.mustNotCall()); [1, null, undefined].forEach((i) => { //observer.observe(i); - assert.throws(() => observer.observe(i), - common.expectsError({ - code: 'ERR_INVALID_ARG_TYPE', - type: TypeError, - message: 'The "options" argument must be of type Object' - })); + common.expectsError( + () => observer.observe(i), + { + code: 'ERR_INVALID_ARG_TYPE', + type: TypeError, + message: 'The "options" argument must be of type Object' + }); }); [1, undefined, null, {}, Infinity].forEach((i) => { - assert.throws(() => observer.observe({ entryTypes: i }), - common.expectsError({ - code: 'ERR_INVALID_OPT_VALUE', - type: TypeError, - message: 'The value "[object Object]" is invalid for ' + - 'option "entryTypes"' - })); + common.expectsError(() => observer.observe({ entryTypes: i }), + { + code: 'ERR_INVALID_OPT_VALUE', + type: TypeError, + message: 'The value "[object Object]" is invalid ' + + 'for option "entryTypes"' + }); }); } diff --git a/test/parallel/test-readline-interface.js b/test/parallel/test-readline-interface.js index fd622f8cabc564..22cb5891bb5d74 100644 --- a/test/parallel/test-readline-interface.js +++ b/test/parallel/test-readline-interface.js @@ -357,46 +357,46 @@ function isWarned(emitter) { // constructor throws if completer is not a function or undefined { const fi = new FakeInput(); - assert.throws(function() { + common.expectsError(function() { readline.createInterface({ input: fi, completer: 'string is not valid' }); - }, common.expectsError({ + }, { type: TypeError, code: 'ERR_INVALID_OPT_VALUE' - })); + }); } // constructor throws if historySize is not a positive number { const fi = new FakeInput(); - assert.throws(function() { + common.expectsError(function() { readline.createInterface({ input: fi, historySize: 'not a number' }); - }, common.expectsError({ + }, { type: RangeError, code: 'ERR_INVALID_OPT_VALUE' - })); + }); - assert.throws(function() { + common.expectsError(function() { readline.createInterface({ input: fi, historySize: -1 }); - }, common.expectsError({ + }, { type: RangeError, code: 'ERR_INVALID_OPT_VALUE' - })); + }); - assert.throws(function() { + common.expectsError(function() { readline.createInterface({ input: fi, historySize: NaN }); - }, common.expectsError({ + }, { type: RangeError, code: 'ERR_INVALID_OPT_VALUE' - })); + }); } // duplicate lines are removed from history when diff --git a/test/sequential/test-tls-lookup.js b/test/sequential/test-tls-lookup.js index fddbb20ee00f36..ff759cf2fe6bea 100644 --- a/test/sequential/test-tls-lookup.js +++ b/test/sequential/test-tls-lookup.js @@ -13,12 +13,12 @@ const tls = require('tls'); lookup: input }; - assert.throws(function() { + common.expectsError(function() { tls.connect(opts); - }, common.expectsError({ + }, { code: 'ERR_INVALID_ARG_TYPE', type: TypeError - })); + }); }); connectDoesNotThrow(common.mustCall(() => {}));