Skip to content

Commit 34b4818

Browse files
committed
assert: make sure throws is able to handle primitives
This fixes some possible issues with `assert.throws` in combination with an validation object. It will now properly handle primitive values being thrown as error. It also makes sure the `generatedMessage` property is properly set if `assert.throws` is used in combination with an validation object and improves the error performance in such cases by only creating the error once.
1 parent cf7be86 commit 34b4818

3 files changed

Lines changed: 72 additions & 7 deletions

File tree

lib/assert.js

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -385,16 +385,16 @@ function compareExceptionKey(actual, expected, key, message, keys) {
385385
const a = new Comparison(actual, keys);
386386
const b = new Comparison(expected, keys, actual);
387387

388-
const tmpLimit = Error.stackTraceLimit;
389-
Error.stackTraceLimit = 0;
390388
const err = new AssertionError({
391389
actual: a,
392390
expected: b,
393391
operator: 'deepStrictEqual',
394392
stackStartFn: assert.throws
395393
});
396-
Error.stackTraceLimit = tmpLimit;
397-
message = err.message;
394+
err.actual = actual;
395+
err.expected = expected;
396+
err.operator = 'throws';
397+
throw err;
398398
}
399399
innerFail({
400400
actual,
@@ -408,14 +408,34 @@ function compareExceptionKey(actual, expected, key, message, keys) {
408408

409409
function expectedException(actual, expected, msg) {
410410
if (typeof expected !== 'function') {
411-
if (expected instanceof RegExp)
411+
if (isRegExp(expected))
412412
return expected.test(actual);
413413
// assert.doesNotThrow does not accept objects.
414414
if (arguments.length === 2) {
415415
throw new ERR_INVALID_ARG_TYPE(
416416
'expected', ['Function', 'RegExp'], expected
417417
);
418418
}
419+
420+
// TODO: Disallow primitives as error argument.
421+
// This is here to prevent a breaking change.
422+
if (typeof expected !== 'object') {
423+
return true;
424+
}
425+
426+
// Handle primitives properly.
427+
if (typeof actual !== 'object' || actual === null) {
428+
const err = new AssertionError({
429+
actual,
430+
expected,
431+
message: msg,
432+
operator: 'deepStrictEqual',
433+
stackStartFn: assert.throws
434+
});
435+
err.operator = 'throws';
436+
throw err;
437+
}
438+
419439
const keys = Object.keys(expected);
420440
// Special handle errors to make sure the name and the message are compared
421441
// as well.

test/message/assert_throws_stack.out

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
assert.js:*
2-
throw new AssertionError(obj);
3-
^
2+
throw err;
3+
^
44

55
AssertionError [ERR_ASSERTION]: Input A expected to strictly deep-equal input B:
66
+ expected - actual

test/parallel/test-assert.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -740,7 +740,9 @@ common.expectsError(
740740
const frames = err.stack.split('\n');
741741
const [, filename, line, column] = frames[1].match(/\((.+):(\d+):(\d+)\)/);
742742
// Reset the cache to check again
743+
const size = errorCache.size;
743744
errorCache.delete(`${filename}${line - 1}${column - 1}`);
745+
assert.strictEqual(errorCache.size, size - 1);
744746
const data = `${'\n'.repeat(line - 1)}${' '.repeat(column - 1)}` +
745747
'ok(failed(badly));';
746748
try {
@@ -849,6 +851,7 @@ common.expectsError(
849851
{
850852
name: 'AssertionError [ERR_ASSERTION]',
851853
code: 'ERR_ASSERTION',
854+
generatedMessage: true,
852855
message: `${start}\n${actExp}\n\n` +
853856
" Comparison {\n name: 'Error',\n- message: 'foo'" +
854857
"\n+ message: ''\n }"
@@ -940,3 +943,45 @@ assert.throws(
940943
' }'
941944
}
942945
);
946+
947+
{
948+
let actual = null;
949+
const expected = { message: 'foo' };
950+
assert.throws(
951+
() => assert.throws(
952+
() => { throw actual; },
953+
expected
954+
),
955+
{
956+
operator: 'throws',
957+
actual,
958+
expected,
959+
generatedMessage: true,
960+
message: `${start}\n${actExp}\n\n` +
961+
'- null\n' +
962+
'+ {\n' +
963+
"+ message: 'foo'\n" +
964+
'+ }'
965+
}
966+
);
967+
968+
actual = 'foobar';
969+
const message = 'message';
970+
assert.throws(
971+
() => assert.throws(
972+
() => { throw actual; },
973+
{ message: 'foobar' },
974+
message
975+
),
976+
{
977+
actual,
978+
message,
979+
operator: 'throws',
980+
generatedMessage: false
981+
}
982+
);
983+
}
984+
985+
// TODO: This case is only there to make sure there is no breaking change.
986+
// eslint-disable-next-line no-restricted-syntax, no-throw-literal
987+
assert.throws(() => { throw 4; }, 4);

0 commit comments

Comments
 (0)