Skip to content

Commit

Permalink
assert: handle cause when cutting off stack frames
Browse files Browse the repository at this point in the history
  • Loading branch information
geeksilva97 committed Oct 19, 2024
1 parent 4c72dda commit 2eb8889
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 7 deletions.
13 changes: 12 additions & 1 deletion lib/internal/assert/assertion_error.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,15 @@ const kMaxShortLength = 12;
function copyError(source) {
const target = ObjectAssign({ __proto__: ObjectGetPrototypeOf(source) }, source);
ObjectDefineProperty(target, 'message', { __proto__: null, value: source.message });
if (source.cause !== undefined) {
let cause = source.cause;

if (cause instanceof Error) {
cause = copyError(cause);
}

ObjectDefineProperty(target, 'cause', { __proto__: null, value: cause });
}
return target;
}

Expand Down Expand Up @@ -82,8 +91,9 @@ function createErrDiff(actual, expected, operator, message = '') {
let end = '';
let skipped = false;
const actualInspected = inspectValue(actual);
const expectedInspected = inspectValue(expected);
const actualLines = StringPrototypeSplit(actualInspected, '\n');
const expectedLines = StringPrototypeSplit(inspectValue(expected), '\n');
const expectedLines = StringPrototypeSplit(expectedInspected, '\n');

let i = 0;
let indicator = '';
Expand Down Expand Up @@ -360,6 +370,7 @@ class AssertionError extends Error {
expected = copyError(expected);
}


if (operator === 'deepStrictEqual' || operator === 'strictEqual') {
super(createErrDiff(actual, expected, operator));
} else if (operator === 'notDeepStrictEqual' ||
Expand Down
26 changes: 26 additions & 0 deletions test/parallel/test-assert-deep-with-error.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
'use strict';
require('../common');
const assert = require('assert');
const { test } = require('node:test');

const defaultStartMessage = 'Expected values to be strictly deep-equal:\n' +
'+ actual - expected\n' +
'\n';

test('Handle error causes', () => {
assert.throws(() => {
assert.deepStrictEqual(new Error('a', { cause: new Error('x') }), new Error('a', { cause: new Error('y') }));
}, { message: defaultStartMessage + ' [Error: a] {\n' +
'+ [cause]: [Error: x]\n' +
'- [cause]: [Error: y]\n' +
' }' });

assert.throws(() => {
assert.deepStrictEqual(new Error('a'), new Error('a', { cause: new Error('y') }));
}, { message: defaultStartMessage + '+ [Error: a]\n' +
'- [Error: a] {\n' +
'- [cause]: [Error: y]\n' +
'- }' });

assert.notDeepStrictEqual(new Error('a', { cause: new Error('x') }), new Error('a', { cause: new Error('y') }));
});
6 changes: 0 additions & 6 deletions test/parallel/test-assert-deep.js
Original file line number Diff line number Diff line change
Expand Up @@ -611,12 +611,6 @@ test('Handle different error messages', () => {
assertNotDeepOrStrict(err1, {}, AssertionError);
});

test.only('Handle error causes', () => {
// This should fail as in assert.notEqual(1, 1) (?)
assert.notEqual(new Error("a"), new Error("b", { cause: new Error("y") }));
assert.deepStrictEqual(new Error("a"), new Error("a", { cause: new Error("y") }))
});

test('Handle NaN', () => {
assertDeepAndStrictEqual(NaN, NaN);
assertDeepAndStrictEqual({ a: NaN }, { a: NaN });
Expand Down

0 comments on commit 2eb8889

Please sign in to comment.