From 0ed963bf891bf3651bff4947741be65588e84965 Mon Sep 17 00:00:00 2001 From: Ruben Bridgewater Date: Fri, 15 Dec 2017 21:10:35 -0200 Subject: [PATCH] test: refactor common.expectsError A combination of try catch and common.expectsError is not necessary as the latter already does everything on its own. PR-URL: https://github.com/nodejs/node/pull/17703 Reviewed-By: Timothy Gu Reviewed-By: James M Snell --- test/parallel/test-assert.js | 54 ++++++++++++------------------------ 1 file changed, 18 insertions(+), 36 deletions(-) diff --git a/test/parallel/test-assert.js b/test/parallel/test-assert.js index 95b4f9513296af..dd9b0906097643 100644 --- a/test/parallel/test-assert.js +++ b/test/parallel/test-assert.js @@ -476,33 +476,21 @@ common.expectsError( } ); -{ - let threw = false; - try { - assert.doesNotThrow(makeBlock(thrower, Error), 'user message'); - } catch (e) { - threw = true; - common.expectsError({ - code: 'ERR_ASSERTION', - message: /Got unwanted exception: user message\n\[object Object\]/ - })(e); +common.expectsError( + () => assert.doesNotThrow(makeBlock(thrower, Error), 'user message'), + { + code: 'ERR_ASSERTION', + message: /Got unwanted exception: user message\n\[object Object\]/ } - assert.ok(threw); -} +); -{ - let threw = false; - try { - assert.doesNotThrow(makeBlock(thrower, Error)); - } catch (e) { - threw = true; - common.expectsError({ - code: 'ERR_ASSERTION', - message: /Got unwanted exception\.\n\[object Object\]/ - })(e); +common.expectsError( + () => assert.doesNotThrow(makeBlock(thrower, Error)), + { + code: 'ERR_ASSERTION', + message: /Got unwanted exception\.\n\[object Object\]/ } - assert.ok(threw); -} +); // make sure that validating using constructor really works { @@ -691,21 +679,15 @@ try { } const testBlockTypeError = (method, block) => { - let threw = true; - - try { - method(block); - threw = false; - } catch (e) { - common.expectsError({ + common.expectsError( + () => method(block), + { code: 'ERR_INVALID_ARG_TYPE', type: TypeError, message: 'The "block" argument must be of type Function. Received ' + - `type ${typeName(block)}` - })(e); - } - - assert.ok(threw); + `type ${typeName(block)}` + } + ); }; testBlockTypeError(assert.throws, 'string');