From 1717cb9a404f386088d1d80c8febeac05932af97 Mon Sep 17 00:00:00 2001 From: Ruben Bridgewater Date: Wed, 21 Jun 2017 02:32:37 +0200 Subject: [PATCH] test: fix tests for changed error messages --- lib/internal/child_process.js | 4 +- lib/path.js | 4 +- test/parallel/test-assert.js | 13 ++++- .../test-child-process-constructor.js | 21 ++++--- .../parallel/test-dgram-send-address-types.js | 2 +- test/parallel/test-dgram-sendto.js | 8 +-- test/parallel/test-internal-errors.js | 58 ++++++++++++------- test/parallel/test-path-parse-format.js | 13 ++++- test/parallel/test-process-hrtime.js | 3 +- .../parallel/test-url-format-invalid-input.js | 14 ++--- test/parallel/test-url-format-whatwg.js | 2 +- test/parallel/test-url-parse-invalid-input.js | 18 +++--- test/parallel/test-util-callbackify.js | 4 +- test/parallel/test-util-inherits.js | 6 +- test/parallel/test-util-inspect.js | 4 +- 15 files changed, 107 insertions(+), 67 deletions(-) diff --git a/lib/internal/child_process.js b/lib/internal/child_process.js index 216a2fb85a0efe..5afb01b652e4c4 100644 --- a/lib/internal/child_process.js +++ b/lib/internal/child_process.js @@ -284,7 +284,7 @@ ChildProcess.prototype.spawn = function(options) { options.envPairs = []; else if (!Array.isArray(options.envPairs)) { throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'options.envPairs', - 'array', options.envPairs); + 'Array', options.envPairs); } options.envPairs.push('NODE_CHANNEL_FD=' + ipcFd); @@ -301,7 +301,7 @@ ChildProcess.prototype.spawn = function(options) { else if (options.args === undefined) this.spawnargs = []; else - throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'options.args', 'array', + throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'options.args', 'Array', options.args); var err = this._handle.spawn(options); diff --git a/lib/path.js b/lib/path.js index 456510b2817e16..90f73a6f7c2809 100644 --- a/lib/path.js +++ b/lib/path.js @@ -959,7 +959,7 @@ const win32 = { format: function format(pathObject) { if (pathObject === null || typeof pathObject !== 'object') { - throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'pathObject', 'Object', + throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'pathObject', 'object', pathObject); } return _format('\\', pathObject); @@ -1502,7 +1502,7 @@ const posix = { format: function format(pathObject) { if (pathObject === null || typeof pathObject !== 'object') { - throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'pathObject', 'Object', + throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'pathObject', 'object', pathObject); } return _format('/', pathObject); diff --git a/test/parallel/test-assert.js b/test/parallel/test-assert.js index d4a1b277bcc195..854924bf461ee8 100644 --- a/test/parallel/test-assert.js +++ b/test/parallel/test-assert.js @@ -676,7 +676,14 @@ try { { // Verify that throws() and doesNotThrow() throw on non-function block function typeName(value) { - return value === null ? 'null' : typeof value; + if (value == null) { + return value; + } + const type = typeof value; + if (type !== 'object') { + return `type ${type}`; + } + return `instance of ${value.constructor.name}`; } const testBlockTypeError = (method, block) => { @@ -690,7 +697,7 @@ try { code: 'ERR_INVALID_ARG_TYPE', type: TypeError, message: 'The "block" argument must be of type function. Received ' + - 'type ' + typeName(block) + typeName(block) })(e); } @@ -732,7 +739,7 @@ assert.throws(() => { { // bad args to AssertionError constructor should throw TypeError const args = [1, true, false, '', null, Infinity, Symbol('test'), undefined]; - const re = /^The "options" argument must be of type object$/; + const re = /^The "options" argument must be of type object\. Received /; args.forEach((input) => { assert.throws( () => new assert.AssertionError(input), diff --git a/test/parallel/test-child-process-constructor.js b/test/parallel/test-child-process-constructor.js index ea81f806060cb6..b11f9e900cd6fc 100644 --- a/test/parallel/test-child-process-constructor.js +++ b/test/parallel/test-child-process-constructor.js @@ -6,7 +6,14 @@ const { ChildProcess } = require('child_process'); assert.strictEqual(typeof ChildProcess, 'function'); function typeName(value) { - return value === null ? 'null' : typeof value; + if (value == null) { + return value; + } + const type = typeof value; + if (type !== 'object') { + return `type ${type}`; + } + return `instance of ${value.constructor.name}`; } { @@ -19,7 +26,7 @@ function typeName(value) { }, common.expectsError({ code: 'ERR_INVALID_ARG_TYPE', type: TypeError, - message: 'The "options" argument must be of type object. Received type ' + + message: 'The "options" argument must be of type object. Received ' + typeName(options) })); }); @@ -36,7 +43,7 @@ function typeName(value) { code: 'ERR_INVALID_ARG_TYPE', type: TypeError, message: 'The "options.file" property must be of type string. Received ' + - 'type ' + typeName(file) + typeName(file) })); }); } @@ -51,8 +58,8 @@ function typeName(value) { }, common.expectsError({ code: 'ERR_INVALID_ARG_TYPE', type: TypeError, - message: 'The "options.envPairs" property must be of type array. ' + - 'Received type ' + typeName(envPairs) + message: 'The "options.envPairs" property must be instance of Array. ' + + 'Received ' + typeName(envPairs) })); }); } @@ -67,8 +74,8 @@ function typeName(value) { }, common.expectsError({ code: 'ERR_INVALID_ARG_TYPE', type: TypeError, - message: 'The "options.args" property must be of type array. Received ' + - 'type ' + typeName(args) + message: 'The "options.args" property must be instance of Array. ' + + 'Received ' + typeName(args) })); }); } diff --git a/test/parallel/test-dgram-send-address-types.js b/test/parallel/test-dgram-send-address-types.js index 6b26c23a266558..5bc8a0fb98a593 100644 --- a/test/parallel/test-dgram-send-address-types.js +++ b/test/parallel/test-dgram-send-address-types.js @@ -13,7 +13,7 @@ const onMessage = common.mustCall((err, bytes) => { const expectedError = { code: 'ERR_INVALID_ARG_TYPE', type: TypeError, message: - /^The "address" argument must be one of type string or falsy$/ + /^The "address" argument must be of type string or falsy\. Received / }; const client = dgram.createSocket('udp4').bind(0, () => { diff --git a/test/parallel/test-dgram-sendto.js b/test/parallel/test-dgram-sendto.js index c922dc1039e732..7866e1b1068d15 100644 --- a/test/parallel/test-dgram-sendto.js +++ b/test/parallel/test-dgram-sendto.js @@ -5,7 +5,7 @@ const dgram = require('dgram'); const socket = dgram.createSocket('udp4'); const errorMessageOffset = - /^The "offset" argument must be of type number$/; + /^The "offset" argument must be of type number\. Received /; assert.throws(() => { socket.sendto(); @@ -20,7 +20,7 @@ assert.throws(() => { }, common.expectsError({ code: 'ERR_INVALID_ARG_TYPE', type: TypeError, - message: /^The "length" argument must be of type number$/ + message: /^The "length" argument must be of type number\. Received / })); assert.throws(() => { @@ -36,7 +36,7 @@ assert.throws(() => { }, common.expectsError({ code: 'ERR_INVALID_ARG_TYPE', type: TypeError, - message: /^The "address" argument must be of type string$/ + message: /^The "address" argument must be of type string\. Received / })); assert.throws(() => { @@ -44,5 +44,5 @@ assert.throws(() => { }, common.expectsError({ code: 'ERR_INVALID_ARG_TYPE', type: TypeError, - message: /^The "port" argument must be of type number$/ + message: /^The "port" argument must be of type number\. Received / })); diff --git a/test/parallel/test-internal-errors.js b/test/parallel/test-internal-errors.js index 8e06bab34944a1..d01617736d89ff 100644 --- a/test/parallel/test-internal-errors.js +++ b/test/parallel/test-internal-errors.js @@ -181,24 +181,42 @@ assert.throws(() => { })); // // Test ERR_INVALID_ARG_TYPE -assert.strictEqual(errors.message('ERR_INVALID_ARG_TYPE', ['a', 'b']), - 'The "a" argument must be of type b'); -assert.strictEqual(errors.message('ERR_INVALID_ARG_TYPE', ['a', ['b']]), - 'The "a" argument must be of type b'); -assert.strictEqual(errors.message('ERR_INVALID_ARG_TYPE', ['a', ['b', 'c']]), - 'The "a" argument must be one of type b or c'); -assert.strictEqual(errors.message('ERR_INVALID_ARG_TYPE', - ['a', ['b', 'c', 'd']]), - 'The "a" argument must be one of type b, c, or d'); -assert.strictEqual(errors.message('ERR_INVALID_ARG_TYPE', ['a', 'b', 'c']), - 'The "a" argument must be of type b. Received type string'); -assert.strictEqual(errors.message('ERR_INVALID_ARG_TYPE', - ['a', 'b', undefined]), - 'The "a" argument must be of type b. Received type ' + - 'undefined'); -assert.strictEqual(errors.message('ERR_INVALID_ARG_TYPE', - ['a', 'b', null]), - 'The "a" argument must be of type b. Received type null'); +const specialArray = []; +Object.defineProperty(specialArray, 'constructor', { + enumerable: false, + configurable: false, + writable: false, + value: null +}); +assert.strictEqual( + errors.message('ERR_INVALID_ARG_TYPE', ['a', 'b', specialArray]), + 'The "a" argument must be of type b. Received a plain object' +); +assert.strictEqual( + errors.message('ERR_INVALID_ARG_TYPE', ['a', ['b'], new Error()]), + 'The "a" argument must be of type b. Received instance of Error' +); +assert.strictEqual( + errors.message('ERR_INVALID_ARG_TYPE', + ['a', ['b', 'Array'], Object.create(null)]), + 'The "a" argument must be of type b or Array. Received a plain object' +); +assert.strictEqual( + errors.message('ERR_INVALID_ARG_TYPE', ['a', ['Array', 'c', 'd'], null]), + 'The "a" argument must be instance of Array, c, or d. Received null' +); +assert.strictEqual( + errors.message('ERR_INVALID_ARG_TYPE', ['a', 'B', 'c']), + 'The "a" argument must be instance of B. Received type string' +); +assert.strictEqual( + errors.message('ERR_INVALID_ARG_TYPE', ['a', 'b', undefined]), + 'The "a" argument must be of type b. Received undefined' +); +assert.strictEqual( + errors.message('ERR_INVALID_ARG_TYPE', ['a', 'b', /a/]), + 'The "a" argument must be of type b. Received instance of RegExp' +); // Test ERR_INVALID_URL_SCHEME assert.strictEqual(errors.message('ERR_INVALID_URL_SCHEME', ['file']), @@ -206,9 +224,9 @@ assert.strictEqual(errors.message('ERR_INVALID_URL_SCHEME', ['file']), assert.strictEqual(errors.message('ERR_INVALID_URL_SCHEME', [['file']]), 'The URL must be of scheme file'); assert.strictEqual(errors.message('ERR_INVALID_URL_SCHEME', [['http', 'ftp']]), - 'The URL must be one of scheme http or ftp'); + 'The URL must be of scheme http or ftp'); assert.strictEqual(errors.message('ERR_INVALID_URL_SCHEME', [['a', 'b', 'c']]), - 'The URL must be one of scheme a, b, or c'); + 'The URL must be of scheme a, b, or c'); assert.throws( () => errors.message('ERR_INVALID_URL_SCHEME', [[]]), common.expectsError({ diff --git a/test/parallel/test-path-parse-format.js b/test/parallel/test-path-parse-format.js index b253249472cb08..5e44e35f6616ab 100644 --- a/test/parallel/test-path-parse-format.js +++ b/test/parallel/test-path-parse-format.js @@ -209,7 +209,14 @@ function checkFormat(path, testCases) { }); function typeName(value) { - return value === null ? 'null' : typeof value; + if (value == null) { + return value; + } + const type = typeof value; + if (type !== 'object') { + return `type ${type}`; + } + return `instance of ${value.constructor.name}`; } [null, undefined, 1, true, false, 'string'].forEach((pathObject) => { @@ -218,8 +225,8 @@ function checkFormat(path, testCases) { }, common.expectsError({ code: 'ERR_INVALID_ARG_TYPE', type: TypeError, - message: 'The "pathObject" argument must be of type Object. Received ' + - 'type ' + typeName(pathObject) + message: 'The "pathObject" argument must be of type object. Received ' + + typeName(pathObject) })); }); } diff --git a/test/parallel/test-process-hrtime.js b/test/parallel/test-process-hrtime.js index faf05fef112cb7..5ce5da6bba665e 100644 --- a/test/parallel/test-process-hrtime.js +++ b/test/parallel/test-process-hrtime.js @@ -38,7 +38,8 @@ assert.throws(() => { }, common.expectsError({ code: 'ERR_INVALID_ARG_TYPE', type: TypeError, - message: 'The "time" argument must be of type Array. Received type number' + message: 'The "time" argument must be instance of Array. ' + + 'Received type number' })); assert.throws(() => { process.hrtime([]); diff --git a/test/parallel/test-url-format-invalid-input.js b/test/parallel/test-url-format-invalid-input.js index cc4f6bdc0f178c..b641762b6a52b8 100644 --- a/test/parallel/test-url-format-invalid-input.js +++ b/test/parallel/test-url-format-invalid-input.js @@ -6,19 +6,19 @@ const url = require('url'); const throwsObjsAndReportTypes = new Map([ [undefined, 'undefined'], [null, 'null'], - [true, 'boolean'], - [false, 'boolean'], - [0, 'number'], - [function() {}, 'function'], - [Symbol('foo'), 'symbol'] + [true, 'type boolean'], + [false, 'type boolean'], + [0, 'type number'], + [function() {}, 'type function'], + [Symbol('foo'), 'type symbol'] ]); for (const [urlObject, type] of throwsObjsAndReportTypes) { const error = common.expectsError({ code: 'ERR_INVALID_ARG_TYPE', type: TypeError, - message: 'The "urlObject" argument must be one of type object or string. ' + - `Received type ${type}` + message: 'The "urlObject" argument must be of type object or string. ' + + `Received ${type}` }); assert.throws(function() { url.format(urlObject); }, error); } diff --git a/test/parallel/test-url-format-whatwg.js b/test/parallel/test-url-format-whatwg.js index d6afe46426b2ac..8c30258c2153a6 100644 --- a/test/parallel/test-url-format-whatwg.js +++ b/test/parallel/test-url-format-whatwg.js @@ -24,7 +24,7 @@ assert.strictEqual( const expectedErr = common.expectsError({ code: 'ERR_INVALID_ARG_TYPE', type: TypeError, - message: 'The "options" argument must be of type object' + message: /^The "options" argument must be of type object\. Received / }, 4); assert.throws(() => url.format(myURL, true), expectedErr); assert.throws(() => url.format(myURL, 1), expectedErr); diff --git a/test/parallel/test-url-parse-invalid-input.js b/test/parallel/test-url-parse-invalid-input.js index 86751a2935a7bf..1e44ef0e5b2731 100644 --- a/test/parallel/test-url-parse-invalid-input.js +++ b/test/parallel/test-url-parse-invalid-input.js @@ -7,19 +7,19 @@ const url = require('url'); [ [undefined, 'undefined'], [null, 'null'], - [true, 'boolean'], - [false, 'boolean'], - [0.0, 'number'], - [0, 'number'], - [[], 'object'], - [{}, 'object'], - [() => {}, 'function'], - [Symbol('foo'), 'symbol'] + [true, 'type boolean'], + [false, 'type boolean'], + [0.0, 'type number'], + [0, 'type number'], + [[], 'instance of Array'], + [{}, 'instance of Object'], + [() => {}, 'type function'], + [Symbol('foo'), 'type symbol'] ].forEach(([val, type]) => { const error = common.expectsError({ code: 'ERR_INVALID_ARG_TYPE', type: TypeError, - message: `The "url" argument must be of type string. Received type ${type}` + message: `The "url" argument must be of type string. Received ${type}` }); assert.throws(() => { url.parse(val); }, error); }); diff --git a/test/parallel/test-util-callbackify.js b/test/parallel/test-util-callbackify.js index 68ffc6e9207e23..18872d20bc2842 100644 --- a/test/parallel/test-util-callbackify.js +++ b/test/parallel/test-util-callbackify.js @@ -234,7 +234,7 @@ const values = [ }, common.expectsError({ code: 'ERR_INVALID_ARG_TYPE', type: TypeError, - message: 'The "original" argument must be of type function' + message: /^The "original" argument must be of type function\. Received / })); }); } @@ -255,7 +255,7 @@ const values = [ }, common.expectsError({ code: 'ERR_INVALID_ARG_TYPE', type: TypeError, - message: 'The "last argument" argument must be of type function' + message: /^The "last argument" argument must be of type function\./ })); }); } diff --git a/test/parallel/test-util-inherits.js b/test/parallel/test-util-inherits.js index 31b7e6ad3d6b15..380d607e343993 100644 --- a/test/parallel/test-util-inherits.js +++ b/test/parallel/test-util-inherits.js @@ -6,7 +6,7 @@ const inherits = require('util').inherits; const errCheck = common.expectsError({ code: 'ERR_INVALID_ARG_TYPE', type: TypeError, - message: 'The "superCtor" argument must be of type function' + message: /The "superCtor" argument must be of type function/ }); // super constructor @@ -85,7 +85,7 @@ assert.throws(function() { }, common.expectsError({ code: 'ERR_INVALID_ARG_TYPE', type: TypeError, - message: 'The "superCtor.prototype" property must be of type function' + message: /^The "superCtor\.prototype" property must be of type function\./ }) ); assert.throws(function() { @@ -96,6 +96,6 @@ assert.throws(function() { }, common.expectsError({ code: 'ERR_INVALID_ARG_TYPE', type: TypeError, - message: 'The "ctor" argument must be of type function' + message: 'The "ctor" argument must be of type function. Received null' }) ); diff --git a/test/parallel/test-util-inspect.js b/test/parallel/test-util-inspect.js index 3b3b93dc00353b..4dd74f19f045ef 100644 --- a/test/parallel/test-util-inspect.js +++ b/test/parallel/test-util-inspect.js @@ -1032,7 +1032,7 @@ if (typeof Symbol !== 'undefined') { }, common.expectsError({ code: 'ERR_INVALID_ARG_TYPE', type: TypeError, - message: 'The "options" argument must be of type object' + message: 'The "options" argument must be of type object. Received null' }) ); @@ -1041,7 +1041,7 @@ if (typeof Symbol !== 'undefined') { }, common.expectsError({ code: 'ERR_INVALID_ARG_TYPE', type: TypeError, - message: 'The "options" argument must be of type object' + message: /^The "options" argument must be of type object\. Received / }) ); }