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 <jasnell@gmail.com>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
This commit is contained in:
Rich Trott 2017-02-14 10:35:27 -08:00
parent 32679c73c1
commit 749fac0bf6
2 changed files with 9 additions and 7 deletions

View File

@ -629,11 +629,13 @@ exports.expectsError = function expectsError(code, type, message) {
return function(error) { return function(error) {
assert.strictEqual(error.code, code); assert.strictEqual(error.code, code);
if (type !== undefined) if (type !== undefined)
assert(error instanceof type, 'error is not the expected type'); assert(error instanceof type,
if (message !== undefined) { `${error} is not the expected type ${type}`);
if (!util.isRegExp(message)) if (message instanceof RegExp) {
message = new RegExp(String(message)); assert(message.test(error.message),
assert(message.test(error.message), 'error.message does not match'); `${error.message} does not match ${message}`);
} else if (typeof message === 'string') {
assert.strictEqual(error.message, message);
} }
return true; return true;
}; };

View File

@ -107,10 +107,10 @@ assert.throws(() => {
assert.throws(() => { assert.throws(() => {
throw new errors.TypeError('TEST_ERROR_1', 'a'); throw new errors.TypeError('TEST_ERROR_1', 'a');
}, common.expectsError('TEST_ERROR_1', RangeError)); }, common.expectsError('TEST_ERROR_1', RangeError));
}, /^AssertionError: error is not the expected type/); }, /^AssertionError: .+ is not the expected type \S/);
assert.throws(() => { assert.throws(() => {
assert.throws(() => { assert.throws(() => {
throw new errors.TypeError('TEST_ERROR_1', 'a'); throw new errors.TypeError('TEST_ERROR_1', 'a');
}, common.expectsError('TEST_ERROR_1', TypeError, /^Error for testing 2/)); }, common.expectsError('TEST_ERROR_1', TypeError, /^Error for testing 2/));
}, /^AssertionError: error.message does not match/); }, /AssertionError: .+ does not match \S/);