From d6fece14369ab5d9d64e48ed58cea168f5084a80 Mon Sep 17 00:00:00 2001 From: Ruben Bridgewater Date: Wed, 5 Jul 2017 12:05:24 -0400 Subject: [PATCH] test: add optional throw fn to expectsError MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit PR-URL: https://github.com/nodejs/node/pull/14089 Reviewed-By: Benjamin Gruenbaum Reviewed-By: Tobias Nießen Reviewed-By: Refael Ackermann --- test/common/README.md | 5 +- test/common/index.js | 16 ++++-- test/parallel/test-assert-deep.js | 12 ++--- test/parallel/test-assert-fail.js | 85 +++++++++++++++---------------- 4 files changed, 63 insertions(+), 55 deletions(-) diff --git a/test/common/README.md b/test/common/README.md index 07f9bc6583d918..21d89591ae453e 100644 --- a/test/common/README.md +++ b/test/common/README.md @@ -50,7 +50,8 @@ Platform normalizes the `dd` command Check if there is more than 1gb of total memory. -### expectsError(settings) +### expectsError([fn, ]settings) +* `fn` [<Function>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function) * `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) @@ -66,6 +67,8 @@ Check if there is more than 1gb of total memory. * return function suitable for use as a validation function passed as the second argument to `assert.throws()` +If `fn` is provided, it will be passed to `assert.throws` as first argument. + 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). ### expectWarning(name, expected) diff --git a/test/common/index.js b/test/common/index.js index 3caed88e203158..308696fba5faf1 100644 --- a/test/common/index.js +++ b/test/common/index.js @@ -702,8 +702,13 @@ Object.defineProperty(exports, 'hasSmallICU', { }); // Useful for testing expected internal/error objects -exports.expectsError = function expectsError({code, type, message}) { - return function(error) { +exports.expectsError = function expectsError(fn, options) { + if (typeof fn !== 'function') { + options = fn; + fn = undefined; + } + const { code, type, message } = options; + function innerFn(error) { assert.strictEqual(error.code, code); if (type !== undefined) { assert(error instanceof type, @@ -716,7 +721,12 @@ exports.expectsError = function expectsError({code, type, message}) { assert.strictEqual(error.message, message); } return true; - }; + } + if (fn) { + assert.throws(fn, innerFn); + return; + } + return innerFn; }; exports.skipIfInspectorDisabled = function skipIfInspectorDisabled() { diff --git a/test/parallel/test-assert-deep.js b/test/parallel/test-assert-deep.js index 9f1c8dadf94325..23f24e7f4fbd19 100644 --- a/test/parallel/test-assert-deep.js +++ b/test/parallel/test-assert-deep.js @@ -114,12 +114,12 @@ for (const a of similar) { } } -assert.throws( - () => { assert.deepEqual(new Set([{a: 0}]), new Set([{a: 1}])); }, - common.expectsError({ - code: 'ERR_ASSERTION', - message: /^Set { { a: 0 } } deepEqual Set { { a: 1 } }$/ - })); +common.expectsError(() => { + assert.deepEqual(new Set([{a: 0}]), new Set([{a: 1}])); +}, { + code: 'ERR_ASSERTION', + message: /^Set { { a: 0 } } deepEqual Set { { a: 1 } }$/ +}); function assertDeepAndStrictEqual(a, b) { assert.deepEqual(a, b); diff --git a/test/parallel/test-assert-fail.js b/test/parallel/test-assert-fail.js index 99f7c4e3da8d5d..12cff99e66c88d 100644 --- a/test/parallel/test-assert-fail.js +++ b/test/parallel/test-assert-fail.js @@ -17,57 +17,52 @@ assert.throws( ); // One arg = message -assert.throws( - () => { assert.fail('custom message'); }, - common.expectsError({ - code: 'ERR_ASSERTION', - type: assert.AssertionError, - message: 'custom message', - operator: undefined, - actual: undefined, - expected: undefined - }) -); +common.expectsError(() => { + assert.fail('custom message'); +}, { + code: 'ERR_ASSERTION', + type: assert.AssertionError, + message: 'custom message', + operator: undefined, + actual: undefined, + expected: undefined +}); // Two args only, operator defaults to '!=' -assert.throws( - () => { assert.fail('first', 'second'); }, - common.expectsError({ - code: 'ERR_ASSERTION', - type: assert.AssertionError, - message: '\'first\' != \'second\'', - operator: '!=', - actual: 'first', - expected: 'second' - - }) -); +common.expectsError(() => { + assert.fail('first', 'second'); +}, { + code: 'ERR_ASSERTION', + type: assert.AssertionError, + message: '\'first\' != \'second\'', + operator: '!=', + actual: 'first', + expected: 'second' +}); // Three args -assert.throws( - () => { assert.fail('ignored', 'ignored', 'another custom message'); }, - common.expectsError({ - code: 'ERR_ASSERTION', - type: assert.AssertionError, - message: 'another custom message', - operator: undefined, - actual: 'ignored', - expected: 'ignored' - }) -); +common.expectsError(() => { + assert.fail('ignored', 'ignored', 'another custom message'); +}, { + code: 'ERR_ASSERTION', + type: assert.AssertionError, + message: 'another custom message', + operator: undefined, + actual: 'ignored', + expected: 'ignored' +}); // No third arg (but a fourth arg) -assert.throws( - () => { assert.fail('first', 'second', undefined, 'operator'); }, - common.expectsError({ - code: 'ERR_ASSERTION', - type: assert.AssertionError, - message: '\'first\' operator \'second\'', - operator: 'operator', - actual: 'first', - expected: 'second' - }) -); +common.expectsError(() => { + assert.fail('first', 'second', undefined, 'operator'); +}, { + code: 'ERR_ASSERTION', + type: assert.AssertionError, + message: '\'first\' operator \'second\'', + operator: 'operator', + actual: 'first', + expected: 'second' +}); // The stackFrameFunction should exclude the foo frame assert.throws(