From 11115fe401044d56ca3e3640f11b0cbae1169396 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tobias=20Nie=C3=9Fen?= Date: Fri, 30 Jun 2017 21:30:23 +0200 Subject: [PATCH 1/3] assert: fix incorrect use of ERR_INVALID_ARG_TYPE PR-URL: https://github.com/nodejs/node/pull/14011 Reviewed-By: Refael Ackermann Reviewed-By: James M Snell Reviewed-By: Luigi Pinca Reviewed-By: Colin Ihrig --- lib/assert.js | 2 +- test/parallel/test-assert.js | 14 +++++++++----- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/lib/assert.js b/lib/assert.js index a751ab2f2089d9..855c79c868a5ef 100644 --- a/lib/assert.js +++ b/lib/assert.js @@ -521,7 +521,7 @@ function innerThrows(shouldThrow, block, expected, message) { if (typeof block !== 'function') { const errors = lazyErrors(); throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'block', 'function', - typeof block); + block); } if (typeof expected === 'string') { diff --git a/test/parallel/test-assert.js b/test/parallel/test-assert.js index e24997ec65d18c..382123ef870256 100644 --- a/test/parallel/test-assert.js +++ b/test/parallel/test-assert.js @@ -670,10 +670,9 @@ try { { // Verify that throws() and doesNotThrow() throw on non-function block - const validationFunction = common.expectsError({ - code: 'ERR_INVALID_ARG_TYPE', - type: TypeError - }); + function typeName(value) { + return value === null ? 'null' : typeof value; + } const testBlockTypeError = (method, block) => { let threw = true; @@ -682,7 +681,12 @@ try { method(block); threw = false; } catch (e) { - validationFunction(e); + common.expectsError({ + code: 'ERR_INVALID_ARG_TYPE', + type: TypeError, + message: 'The "block" argument must be of type function. Received ' + + 'type ' + typeName(block) + })(e); } assert.ok(threw); From 71a779292d9168a9f3f20180d1b8388581346cbb Mon Sep 17 00:00:00 2001 From: Anna Henningsen Date: Mon, 24 Jul 2017 22:24:53 +0200 Subject: [PATCH 2/3] test: bump test/common to master ``` git checkout master test/common && \ git commit -m 'test: bump test/common to master' ``` --- test/common/README.md | 192 ++++++++++++++++++++++++------------------ test/common/index.js | 80 +++++++++++------- 2 files changed, 162 insertions(+), 110 deletions(-) diff --git a/test/common/README.md b/test/common/README.md index 57f607153aa8c1..32ca6711573daa 100644 --- a/test/common/README.md +++ b/test/common/README.md @@ -13,8 +13,8 @@ The `common` module is used by tests for consistency across repeated tasks. ### allowGlobals(...whitelist) -* `whitelist` [<Array>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array) Array of Globals -* return [<Array>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array) +* `whitelist` [<Array>] Array of Globals +* return [<Array>] Takes `whitelist` and concats that with predefined `knownGlobals`. @@ -22,7 +22,7 @@ Takes `whitelist` and concats that with predefined `knownGlobals`. A stream to push an array into a REPL ### busyLoop(time) -* `time` [<Number>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type) +* `time` [<Number>] Blocks for `time` amount of time. @@ -41,93 +41,113 @@ no unexpected rejections occur, because currently they result in silent failures. ### ddCommand(filename, kilobytes) -* return [<Object>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object) +* return [<Object>] Platform normalizes the `dd` command ### enoughTestMem -* return [<Boolean>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type) +* return [<Boolean>] Check if there is more than 1gb of total memory. -### expectsError(settings) -* `settings` [<Object>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object) - with the following optional properties: - * `code` [<String>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type) - expected error must have this value for its `code` property - * `type` [<Function>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function) - expected error must be an instance of `type` - * `message` [<String>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type) - or [<RegExp>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp) +### expectsError([fn, ]settings[, exact]) +* `fn` [<Function>] a function that should throw. +* `settings` [<Object>] + that must contain the `code` property plus any of the other following + properties (some properties only apply for `AssertionError`): + * `code` [<String>] + expected error must have this value for its `code` property. + * `type` [<Function>] + expected error must be an instance of `type` and must be an Error subclass. + * `message` [<String>] or [<RegExp>] if a string is provided for `message`, expected error must have it for its `message` property; if a regular expression is provided for `message`, the - regular expression must match the `message` property of the expected error - -* return function suitable for use as a validation function passed as the second - argument to `assert.throws()` - -The expected error should be [subclassed by the `internal/errors` module](https://github.com/nodejs/node/blob/master/doc/guides/using-internal-errors.md#api). + regular expression must match the `message` property of the expected error. + * `name` [<String>] + expected error must have this value for its `name` property. + * `generatedMessage` [<String>] + (`AssertionError` only) expected error must have this value for its + `generatedMessage` property. + * `actual` <any> + (`AssertionError` only) expected error must have this value for its + `actual` property. + * `expected` <any> + (`AssertionError` only) expected error must have this value for its + `expected` property. + * `operator` <any> + (`AssertionError` only) expected error must have this value for its + `operator` property. +* `exact` [<Number>] default = 1 +* return [<Function>] + + If `fn` is provided, it will be passed to `assert.throws` as first argument + and `undefined` will be returned. + Otherwise a function suitable as callback or for use as a validation function + passed as the second argument to `assert.throws()` will be returned. If the + returned function has not been called exactly `exact` number of times when the + test is complete, then the test will fail. ### expectWarning(name, expected) -* `name` [<String>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type) -* `expected` [<String>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type) | [<Array>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array) +* `name` [<String>] +* `expected` [<String>] | [<Array>] Tests whether `name` and `expected` are part of a raised warning. ### fileExists(pathname) -* pathname [<String>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type) -* return [<Boolean>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type) +* pathname [<String>] +* return [<Boolean>] Checks if `pathname` exists ### fixturesDir -* return [<String>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type) +* return [<String>] Path to the 'fixtures' directory. ### getArrayBufferViews(buf) -* `buf` [<Buffer>](https://nodejs.org/api/buffer.html#buffer_class_buffer) -* return [<ArrayBufferView[]>](https://developer.mozilla.org/en-US/docs/Web/API/ArrayBufferView) +* `buf` [<Buffer>] +* return [<ArrayBufferView[]>] Returns an instance of all possible `ArrayBufferView`s of the provided Buffer. ### globalCheck -* return [<Boolean>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type) +* return [<Boolean>] Turn this off if the test should not check for global leaks. ### hasCrypto -* return [<Boolean>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type) +* return [<Boolean>] Checks for 'openssl'. ### hasFipsCrypto -* return [<Boolean>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type) +* return [<Boolean>] Checks `hasCrypto` and `crypto` with fips. ### hasIntl -* return [<Boolean>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type) +* return [<Boolean>] Checks if [internationalization] is supported. ### hasSmallICU -* return [<Boolean>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type) +* return [<Boolean>] Checks `hasIntl` and `small-icu` is supported. ### hasIPv6 -* return [<Boolean>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type) +* return [<Boolean>] Checks whether `IPv6` is supported on this platform. ### hasMultiLocalhost -* return [<Boolean>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type) +* return [<Boolean>] Checks if there are multiple localhosts available. ### hijackStderr(listener) -* `listener` [<Function>][MDN-Function]: a listener with a single parameter called `data`. +* `listener` [<Function>]: a listener with a single parameter + called `data`. Eavesdrop to `process.stderr.write` calls. Once `process.stderr.write` is called, `listener` will also be called and the `data` of `write` function will @@ -135,7 +155,8 @@ be passed to `listener`. What's more, `process.stderr.writeTimes` is a count of the number of calls. ### hijackStdout(listener) -* `listener` [<Function>][MDN-Function]: a listener with a single parameter called `data`. +* `listener` [<Function>]: a listener with a single parameter + called `data`. Eavesdrop to `process.stdout.write` calls. Once `process.stdout.write` is called, `listener` will also be called and the `data` of `write` function will @@ -143,86 +164,86 @@ be passed to `listener`. What's more, `process.stdout.writeTimes` is a count of the number of calls. ### inFreeBSDJail -* return [<Boolean>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type) +* return [<Boolean>] Checks whether free BSD Jail is true or false. ### isAIX -* return [<Boolean>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type) +* return [<Boolean>] Platform check for Advanced Interactive eXecutive (AIX). ### isAlive(pid) -* `pid` [<Number>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type) -* return [<Boolean>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type) +* `pid` [<Number>] +* return [<Boolean>] Attempts to 'kill' `pid` ### isFreeBSD -* return [<Boolean>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type) +* return [<Boolean>] Platform check for Free BSD. ### isLinux -* return [<Boolean>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type) +* return [<Boolean>] Platform check for Linux. ### isLinuxPPCBE -* return [<Boolean>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type) +* return [<Boolean>] Platform check for Linux on PowerPC. ### isOSX -* return [<Boolean>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type) +* return [<Boolean>] Platform check for macOS. ### isSunOS -* return [<Boolean>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type) +* return [<Boolean>] Platform check for SunOS. ### isWindows -* return [<Boolean>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type) +* return [<Boolean>] Platform check for Windows. ### isWOW64 -* return [<Boolean>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type) +* return [<Boolean>] Platform check for Windows 32-bit on Windows 64-bit. ### leakedGlobals -* return [<Array>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array) +* return [<Array>] Checks whether any globals are not on the `knownGlobals` list. ### localhostIPv4 -* return [<String>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type) +* return [<String>] Gets IP of localhost ### localIPv6Hosts -* return [<Array>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array) +* return [<Array>] Array of IPV6 hosts. ### mustCall([fn][, exact]) -* `fn` [<Function>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function) default = () => {} -* `exact` [<Number>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type) default = 1 -* return [<Function>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function) +* `fn` [<Function>] default = () => {} +* `exact` [<Number>] default = 1 +* return [<Function>] Returns a function that calls `fn`. If the returned function has not been called -exactly `expected` number of times when the test is complete, then the test will +exactly `exact` number of times when the test is complete, then the test will fail. If `fn` is not provided, an empty function will be used. ### mustCallAtLeast([fn][, minimum]) -* `fn` [<Function>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function) default = () => {} -* `minimum` [<Number>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type) default = 1 -* return [<Function>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function) +* `fn` [<Function>] default = () => {} +* `minimum` [<Number>] default = 1 +* return [<Function>] Returns a function that calls `fn`. If the returned function has not been called at least `minimum` number of times when the test is complete, then the test will @@ -231,46 +252,49 @@ fail. If `fn` is not provided, an empty function will be used. ### mustNotCall([msg]) -* `msg` [<String>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type) default = 'function should not have been called' -* return [<Function>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function) +* `msg` [<String>] default = 'function should not have been called' +* return [<Function>] -Returns a function that triggers an `AssertionError` if it is invoked. `msg` is used as the error message for the `AssertionError`. +Returns a function that triggers an `AssertionError` if it is invoked. `msg` is +used as the error message for the `AssertionError`. ### nodeProcessAborted(exitCode, signal) -* `exitCode` [<Number>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type) -* `signal` [<String>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type) -* return [<Boolean>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type) +* `exitCode` [<Number>] +* `signal` [<String>] +* return [<Boolean>] -Returns `true` if the exit code `exitCode` and/or signal name `signal` represent the exit code and/or signal name of a node process that aborted, `false` otherwise. +Returns `true` if the exit code `exitCode` and/or signal name `signal` represent +the exit code and/or signal name of a node process that aborted, `false` +otherwise. ### opensslCli -* return [<Boolean>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type) +* return [<Boolean>] Checks whether 'opensslCli' is supported. ### platformTimeout(ms) -* `ms` [<Number>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type) -* return [<Number>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type) +* `ms` [<Number>] +* return [<Number>] Platform normalizes timeout. ### PIPE -* return [<String>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type) +* return [<String>] Path to the test sock. ### PORT -* return [<Number>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type) default = `12346` +* return [<Number>] default = `12346` Port tests are running on. ### printSkipMessage(msg) -* `msg` [<String>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type) +* `msg` [<String>] Logs '1..0 # Skipped: ' + `msg` ### refreshTmpDir -* return [<String>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type) +* return [<String>] Deletes the 'tmp' dir and recreates it @@ -283,34 +307,34 @@ Restore the original `process.stderr.write`. Restore the original `process.stdout.write`. ### rootDir -* return [<String>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type) +* return [<String>] Path to the 'root' directory. either `/` or `c:\\` (windows) ### skip(msg) -* `msg` [<String>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type) +* `msg` [<String>] Logs '1..0 # Skipped: ' + `msg` and exits with exit code `0`. ### spawnPwd(options) -* `options` [<Object>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object) -* return [<Object>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object) +* `options` [<Object>] +* return [<Object>] Platform normalizes the `pwd` command. ### spawnSyncPwd(options) -* `options` [<Object>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object) -* return [<Object>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object) +* `options` [<Object>] +* return [<Object>] Synchronous version of `spawnPwd`. ### tmpDir -* return [<String>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type) +* return [<String>] The realpath of the 'tmp' directory. ### tmpDirName -* return [<String>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type) +* return [<String>] Name of the temp directory used by tests. @@ -323,5 +347,13 @@ Node.js implementation with tests from [W3C Web Platform Tests](https://github.com/w3c/web-platform-tests). -[MDN-Function]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Normal_objects_and_functions +[<Array>]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array +[<ArrayBufferView[]>]: https://developer.mozilla.org/en-US/docs/Web/API/ArrayBufferView +[<Boolean>]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type +[<Buffer>]: https://nodejs.org/api/buffer.html#buffer_class_buffer +[<Function>]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function +[<Number>]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type +[<Object>]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object +[<RegExp>]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp +[<String>]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type [internationalization]: https://github.com/nodejs/node/wiki/Intl diff --git a/test/common/index.js b/test/common/index.js index 2b90ed66780f75..fc14cdacacc587 100644 --- a/test/common/index.js +++ b/test/common/index.js @@ -25,12 +25,10 @@ const path = require('path'); const fs = require('fs'); const assert = require('assert'); const os = require('os'); -const child_process = require('child_process'); +const { exec, execSync, spawn, spawnSync } = require('child_process'); const stream = require('stream'); -const buffer = require('buffer'); const util = require('util'); const Timer = process.binding('timer_wrap').Timer; -const execSync = require('child_process').execSync; const testRoot = process.env.NODE_TEST_DIR ? fs.realpathSync(process.env.NODE_TEST_DIR) : path.resolve(__dirname, '..'); @@ -54,8 +52,6 @@ exports.isLinux = process.platform === 'linux'; exports.isOSX = process.platform === 'darwin'; exports.enoughTestMem = os.totalmem() > 0x40000000; /* 1 Gb */ -exports.bufferMaxSizeMsg = new RegExp( - `^RangeError: "size" argument must not be larger than ${buffer.kMaxLength}$`); const cpus = os.cpus(); exports.enoughTestCpu = Array.isArray(cpus) && (cpus.length > 1 || cpus[0].speed > 999); @@ -189,7 +185,7 @@ Object.defineProperty(exports, 'inFreeBSDJail', { if (inFreeBSDJail !== null) return inFreeBSDJail; if (exports.isFreeBSD && - child_process.execSync('sysctl -n security.jail.jailed').toString() === + execSync('sysctl -n security.jail.jailed').toString() === '1\n') { inFreeBSDJail = true; } else { @@ -223,7 +219,7 @@ Object.defineProperty(exports, 'localhostIPv4', { }); // opensslCli defined lazily to reduce overhead of spawnSync -Object.defineProperty(exports, 'opensslCli', {get: function() { +Object.defineProperty(exports, 'opensslCli', { get: function() { if (opensslCli !== null) return opensslCli; if (process.config.variables.node_shared_openssl) { @@ -236,13 +232,13 @@ Object.defineProperty(exports, 'opensslCli', {get: function() { if (exports.isWindows) opensslCli += '.exe'; - const opensslCmd = child_process.spawnSync(opensslCli, ['version']); + const opensslCmd = spawnSync(opensslCli, ['version']); if (opensslCmd.status !== 0 || opensslCmd.error !== undefined) { // openssl command cannot be executed opensslCli = false; } return opensslCli; -}, enumerable: true}); +}, enumerable: true }); Object.defineProperty(exports, 'hasCrypto', { get: function() { @@ -286,7 +282,7 @@ exports.childShouldThrowAndAbort = function() { } testCmd += `"${process.argv[0]}" --abort-on-uncaught-exception `; testCmd += `"${process.argv[1]}" child`; - const child = child_process.exec(testCmd); + const child = exec(testCmd); child.on('exit', function onExit(exitCode, signal) { const errMsg = 'Test should have aborted ' + `but instead exited with exit code ${exitCode}` + @@ -306,8 +302,6 @@ exports.ddCommand = function(filename, kilobytes) { exports.spawnPwd = function(options) { - const spawn = require('child_process').spawn; - if (exports.isWindows) { return spawn('cmd.exe', ['/d', '/c', 'cd'], options); } else { @@ -317,8 +311,6 @@ exports.spawnPwd = function(options) { exports.spawnSyncPwd = function(options) { - const spawnSync = require('child_process').spawnSync; - if (exports.isWindows) { return spawnSync('cmd.exe', ['/d', '/c', 'cd'], options); } else { @@ -487,7 +479,7 @@ exports.mustCallAtLeast = function(fn, minimum) { return _mustCallInner(fn, minimum, 'minimum'); }; -function _mustCallInner(fn, criteria, field) { +function _mustCallInner(fn, criteria = 1, field) { if (typeof fn === 'number') { criteria = fn; fn = noop; @@ -495,9 +487,7 @@ function _mustCallInner(fn, criteria, field) { fn = noop; } - if (criteria === undefined) - criteria = 1; - else if (typeof criteria !== 'number') + if (typeof criteria !== 'number') throw new TypeError(`Invalid ${field} value: ${criteria}`); const context = { @@ -701,21 +691,51 @@ Object.defineProperty(exports, 'hasSmallICU', { }); // Useful for testing expected internal/error objects -exports.expectsError = function expectsError({code, type, message}) { - return function(error) { - assert.strictEqual(error.code, code); - if (type !== undefined) { +exports.expectsError = function expectsError(fn, settings, exact) { + if (typeof fn !== 'function') { + exact = settings; + settings = fn; + fn = undefined; + } + const innerFn = exports.mustCall(function(error) { + assert.strictEqual(error.code, settings.code); + if ('type' in settings) { + const type = settings.type; + if (type !== Error && !Error.isPrototypeOf(type)) { + throw new TypeError('`settings.type` must inherit from `Error`'); + } assert(error instanceof type, - `${error} is not the expected type ${type}`); + `${error.name} is not instance of ${type.name}`); + } + if ('message' in settings) { + const message = settings.message; + if (typeof message === 'string') { + assert.strictEqual(error.message, message); + } else { + assert(message.test(error.message), + `${error.message} does not match ${message}`); + } + } + if ('name' in settings) { + assert.strictEqual(error.name, settings.name); } - if (message instanceof RegExp) { - assert(message.test(error.message), - `${error.message} does not match ${message}`); - } else if (typeof message === 'string') { - assert.strictEqual(error.message, message); + if (error.constructor.name === 'AssertionError') { + ['generatedMessage', 'actual', 'expected', 'operator'].forEach((key) => { + if (key in settings) { + const actual = error[key]; + const expected = settings[key]; + assert.strictEqual(actual, expected, + `${key}: expected ${expected}, not ${actual}`); + } + }); } return true; - }; + }, exact); + if (fn) { + assert.throws(fn, innerFn); + return; + } + return innerFn; }; exports.skipIfInspectorDisabled = function skipIfInspectorDisabled() { @@ -764,7 +784,7 @@ exports.getTTYfd = function getTTYfd() { else if (!tty.isatty(tty_fd)) tty_fd++; else { try { - tty_fd = require('fs').openSync('/dev/tty'); + tty_fd = fs.openSync('/dev/tty'); } catch (e) { // There aren't any tty fd's available to use. return -1; From 0c04bbf990af51c7514326110f0dac11b3cec6e4 Mon Sep 17 00:00:00 2001 From: Anna Henningsen Date: Mon, 24 Jul 2017 22:56:21 +0200 Subject: [PATCH 3/3] =?UTF-8?q?test:=20upgrade=20tests=20to=20work=20with?= =?UTF-8?q?=20master=E2=80=99s=20`common`?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- test/parallel/test-assert-fail.js | 2 +- test/parallel/test-assert.js | 6 +--- ...ild-process-spawnsync-validation-errors.js | 2 +- .../test-child-process-validate-stdio.js | 4 +-- test/parallel/test-internal-errors.js | 2 +- test/parallel/test-process-emitwarning.js | 2 +- test/parallel/test-url-format-whatwg.js | 32 +++++++++---------- test/parallel/test-whatwg-url-domainto.js | 2 +- test/parallel/test-whatwg-url-parsing.js | 2 +- ...est-whatwg-url-searchparams-constructor.js | 2 +- test/parallel/test-whatwg-url-searchparams.js | 2 +- 11 files changed, 27 insertions(+), 31 deletions(-) diff --git a/test/parallel/test-assert-fail.js b/test/parallel/test-assert-fail.js index d64947fa1000a6..dac8ce49903f5f 100644 --- a/test/parallel/test-assert-fail.js +++ b/test/parallel/test-assert-fail.js @@ -23,7 +23,7 @@ assert.throws( code: 'ERR_ASSERTION', type: assert.AssertionError, operator: undefined, - actual: undefined, + actual: 'custom message', expected: undefined, message: 'custom message' }) diff --git a/test/parallel/test-assert.js b/test/parallel/test-assert.js index 382123ef870256..1033eaf1ac1330 100644 --- a/test/parallel/test-assert.js +++ b/test/parallel/test-assert.js @@ -143,11 +143,7 @@ assert.throws(makeBlock(a.deepEqual, /a/igm, /a/im), { const re1 = /a/g; re1.lastIndex = 3; - assert.doesNotThrow(makeBlock(a.deepEqual, re1, /a/g), - common.expectsError({ - code: 'ERR_ASSERTION', - message: /^\/a\/g deepEqual \/a\/g$/ - })); + assert.doesNotThrow(makeBlock(a.deepEqual, re1, /a/g)); } assert.doesNotThrow(makeBlock(a.deepEqual, 4, '4'), 'deepEqual(4, \'4\')'); diff --git a/test/parallel/test-child-process-spawnsync-validation-errors.js b/test/parallel/test-child-process-spawnsync-validation-errors.js index dab4b1d37d80f9..b966375288e29c 100644 --- a/test/parallel/test-child-process-spawnsync-validation-errors.js +++ b/test/parallel/test-child-process-spawnsync-validation-errors.js @@ -186,7 +186,7 @@ if (!common.isWindows) { // Validate the killSignal option const typeErr = /^TypeError: "killSignal" must be a string or number$/; const unknownSignalErr = - common.expectsError({ code: 'ERR_UNKNOWN_SIGNAL' }); + common.expectsError({ code: 'ERR_UNKNOWN_SIGNAL' }, 17); pass('killSignal', undefined); pass('killSignal', null); diff --git a/test/parallel/test-child-process-validate-stdio.js b/test/parallel/test-child-process-validate-stdio.js index c6a9bd8e19129c..129ca9822afcd5 100644 --- a/test/parallel/test-child-process-validate-stdio.js +++ b/test/parallel/test-child-process-validate-stdio.js @@ -6,7 +6,7 @@ const assert = require('assert'); const _validateStdio = require('internal/child_process')._validateStdio; const expectedError = - common.expectsError({code: 'ERR_INVALID_OPT_VALUE', type: TypeError}); + common.expectsError({ code: 'ERR_INVALID_OPT_VALUE', type: TypeError }, 2); // should throw if string and not ignore, pipe, or inherit assert.throws(() => _validateStdio('foo'), expectedError); @@ -27,7 +27,7 @@ assert.throws(() => _validateStdio(600), expectedError); // should throw if stdio has ipc and sync is true const stdio2 = ['ipc', 'ipc', 'ipc']; assert.throws(() => _validateStdio(stdio2, true), - common.expectsError({code: 'ERR_IPC_SYNC_FORK', type: Error})); + common.expectsError({ code: 'ERR_IPC_SYNC_FORK', type: Error })); { const stdio3 = [process.stdin, process.stdout, process.stderr]; diff --git a/test/parallel/test-internal-errors.js b/test/parallel/test-internal-errors.js index 8e06bab34944a1..b8cdc840938ca1 100644 --- a/test/parallel/test-internal-errors.js +++ b/test/parallel/test-internal-errors.js @@ -166,7 +166,7 @@ assert.throws(() => { }, common.expectsError({ code: 'TEST_ERROR_1', type: RangeError })); }, common.expectsError({ code: 'ERR_ASSERTION', - message: /^.+ is not the expected type \S/ + message: /^.+ is not instance of \S/ })); assert.throws(() => { diff --git a/test/parallel/test-process-emitwarning.js b/test/parallel/test-process-emitwarning.js index d2d090eae339ee..06772c7be6c210 100644 --- a/test/parallel/test-process-emitwarning.js +++ b/test/parallel/test-process-emitwarning.js @@ -58,7 +58,7 @@ warningThrowToString.toString = function() { process.emitWarning(warningThrowToString); const expectedError = - common.expectsError({code: 'ERR_INVALID_ARG_TYPE', type: TypeError}); + common.expectsError({ code: 'ERR_INVALID_ARG_TYPE', type: TypeError }, 11); // TypeError is thrown on invalid input assert.throws(() => process.emitWarning(1), expectedError); diff --git a/test/parallel/test-url-format-whatwg.js b/test/parallel/test-url-format-whatwg.js index d484760c808584..9174c5a2740ec3 100644 --- a/test/parallel/test-url-format-whatwg.js +++ b/test/parallel/test-url-format-whatwg.js @@ -25,7 +25,7 @@ assert.strictEqual( code: 'ERR_INVALID_ARG_TYPE', type: TypeError, message: 'The "options" argument must be of type object' - }); + }, 4); assert.throws(() => url.format(myURL, true), expectedErr); assert.throws(() => url.format(myURL, 1), expectedErr); assert.throws(() => url.format(myURL, 'test'), expectedErr); @@ -36,76 +36,76 @@ assert.strictEqual( // Any truthy value will be treated as true. assert.strictEqual( - url.format(myURL, {fragment: false}), + url.format(myURL, { fragment: false }), 'http://xn--lck1c3crb1723bpq4a.com/a?a=b' ); assert.strictEqual( - url.format(myURL, {fragment: ''}), + url.format(myURL, { fragment: '' }), 'http://xn--lck1c3crb1723bpq4a.com/a?a=b' ); assert.strictEqual( - url.format(myURL, {fragment: 0}), + url.format(myURL, { fragment: 0 }), 'http://xn--lck1c3crb1723bpq4a.com/a?a=b' ); assert.strictEqual( - url.format(myURL, {fragment: 1}), + url.format(myURL, { fragment: 1 }), 'http://xn--lck1c3crb1723bpq4a.com/a?a=b#c' ); assert.strictEqual( - url.format(myURL, {fragment: {}}), + url.format(myURL, { fragment: {} }), 'http://xn--lck1c3crb1723bpq4a.com/a?a=b#c' ); assert.strictEqual( - url.format(myURL, {search: false}), + url.format(myURL, { search: false }), 'http://xn--lck1c3crb1723bpq4a.com/a#c' ); assert.strictEqual( - url.format(myURL, {search: ''}), + url.format(myURL, { search: '' }), 'http://xn--lck1c3crb1723bpq4a.com/a#c' ); assert.strictEqual( - url.format(myURL, {search: 0}), + url.format(myURL, { search: 0 }), 'http://xn--lck1c3crb1723bpq4a.com/a#c' ); assert.strictEqual( - url.format(myURL, {search: 1}), + url.format(myURL, { search: 1 }), 'http://xn--lck1c3crb1723bpq4a.com/a?a=b#c' ); assert.strictEqual( - url.format(myURL, {search: {}}), + url.format(myURL, { search: {} }), 'http://xn--lck1c3crb1723bpq4a.com/a?a=b#c' ); assert.strictEqual( - url.format(myURL, {unicode: true}), + url.format(myURL, { unicode: true }), 'http://理容ナカムラ.com/a?a=b#c' ); assert.strictEqual( - url.format(myURL, {unicode: 1}), + url.format(myURL, { unicode: 1 }), 'http://理容ナカムラ.com/a?a=b#c' ); assert.strictEqual( - url.format(myURL, {unicode: {}}), + url.format(myURL, { unicode: {} }), 'http://理容ナカムラ.com/a?a=b#c' ); assert.strictEqual( - url.format(myURL, {unicode: false}), + url.format(myURL, { unicode: false }), 'http://xn--lck1c3crb1723bpq4a.com/a?a=b#c' ); assert.strictEqual( - url.format(myURL, {unicode: 0}), + url.format(myURL, { unicode: 0 }), 'http://xn--lck1c3crb1723bpq4a.com/a?a=b#c' ); diff --git a/test/parallel/test-whatwg-url-domainto.js b/test/parallel/test-whatwg-url-domainto.js index bfd5e94d2e5dd8..9e22068a9261f8 100644 --- a/test/parallel/test-whatwg-url-domainto.js +++ b/test/parallel/test-whatwg-url-domainto.js @@ -13,7 +13,7 @@ const wptToASCIITests = require('../fixtures/url-toascii.js'); { const expectedError = common.expectsError( - { code: 'ERR_MISSING_ARGS', type: TypeError }); + { code: 'ERR_MISSING_ARGS', type: TypeError }, 2); assert.throws(() => domainToASCII(), expectedError); assert.throws(() => domainToUnicode(), expectedError); assert.strictEqual(domainToASCII(undefined), 'undefined'); diff --git a/test/parallel/test-whatwg-url-parsing.js b/test/parallel/test-whatwg-url-parsing.js index 39756449c08c6a..2c1d15d10ce351 100644 --- a/test/parallel/test-whatwg-url-parsing.js +++ b/test/parallel/test-whatwg-url-parsing.js @@ -26,7 +26,7 @@ const failureTests = tests.filter((test) => test.failure).concat([ ]); const expectedError = common.expectsError( - { code: 'ERR_INVALID_URL', type: TypeError }); + { code: 'ERR_INVALID_URL', type: TypeError }, 102); for (const test of failureTests) { assert.throws( diff --git a/test/parallel/test-whatwg-url-searchparams-constructor.js b/test/parallel/test-whatwg-url-searchparams-constructor.js index 8920a03943bb5d..b6e720cc7fb04c 100644 --- a/test/parallel/test-whatwg-url-searchparams-constructor.js +++ b/test/parallel/test-whatwg-url-searchparams-constructor.js @@ -210,7 +210,7 @@ function makeIterableFunc(array) { code: 'ERR_INVALID_TUPLE', type: TypeError, message: 'Each query pair must be an iterable [name, value] tuple' - }); + }, 6); let params; params = new URLSearchParams(undefined); diff --git a/test/parallel/test-whatwg-url-searchparams.js b/test/parallel/test-whatwg-url-searchparams.js index 5bb9cf407dc1e9..db7f9a491719dc 100644 --- a/test/parallel/test-whatwg-url-searchparams.js +++ b/test/parallel/test-whatwg-url-searchparams.js @@ -76,7 +76,7 @@ sp.forEach(function() { const callbackErr = common.expectsError({ code: 'ERR_INVALID_CALLBACK', type: TypeError - }); + }, 2); assert.throws(() => sp.forEach(), callbackErr); assert.throws(() => sp.forEach(1), callbackErr); }