Skip to content

Commit

Permalink
test: refactor common.expectsError()
Browse files Browse the repository at this point in the history
* Report values in assertions.
* Strict equality match if message is a string.
* instanceof/typeof instead of deprecated util.isRegExp()

PR-URL: nodejs#11381
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Joyee Cheung <[email protected]>
Reviewed-By: Luigi Pinca <[email protected]>
  • Loading branch information
Trott authored and italoacasas committed Feb 25, 2017
1 parent 5f10827 commit 46084e3
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
12 changes: 7 additions & 5 deletions test/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -626,11 +626,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;
};
Expand Down
4 changes: 2 additions & 2 deletions test/parallel/test-internal-errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -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/);

0 comments on commit 46084e3

Please sign in to comment.