From 749fac0bf60f470c25c5421d4fe659d0efd982af Mon Sep 17 00:00:00 2001 From: Rich Trott Date: Tue, 14 Feb 2017 10:35:27 -0800 Subject: [PATCH] test: refactor common.expectsError() * Report values in assertions. * Strict equality match if message is a string. * instanceof/typeof instead of deprecated util.isRegExp() PR-URL: https://github.com/nodejs/node/pull/11381 Reviewed-By: James M Snell Reviewed-By: Joyee Cheung Reviewed-By: Luigi Pinca --- test/common.js | 12 +++++++----- test/parallel/test-internal-errors.js | 4 ++-- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/test/common.js b/test/common.js index 6b39fa22bf..69d930ec24 100644 --- a/test/common.js +++ b/test/common.js @@ -629,11 +629,13 @@ exports.expectsError = function expectsError(code, type, message) { return function(error) { assert.strictEqual(error.code, code); if (type !== undefined) - assert(error instanceof type, 'error is not the expected type'); - if (message !== undefined) { - if (!util.isRegExp(message)) - message = new RegExp(String(message)); - assert(message.test(error.message), 'error.message does not match'); + assert(error instanceof type, + `${error} is not the expected type ${type}`); + 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); } return true; }; diff --git a/test/parallel/test-internal-errors.js b/test/parallel/test-internal-errors.js index 6844ce539e..526e6befaf 100644 --- a/test/parallel/test-internal-errors.js +++ b/test/parallel/test-internal-errors.js @@ -107,10 +107,10 @@ assert.throws(() => { assert.throws(() => { throw new errors.TypeError('TEST_ERROR_1', 'a'); }, common.expectsError('TEST_ERROR_1', RangeError)); -}, /^AssertionError: error is not the expected type/); +}, /^AssertionError: .+ is not the expected type \S/); assert.throws(() => { assert.throws(() => { throw new errors.TypeError('TEST_ERROR_1', 'a'); }, common.expectsError('TEST_ERROR_1', TypeError, /^Error for testing 2/)); -}, /^AssertionError: error.message does not match/); +}, /AssertionError: .+ does not match \S/);