Skip to content

Commit

Permalink
assert: fix the string length check for printing the simple diff
Browse files Browse the repository at this point in the history
  • Loading branch information
puskin94 committed Oct 20, 2024
1 parent c124cfb commit 02d82f2
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
9 changes: 8 additions & 1 deletion lib/internal/assert/assertion_error.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,14 @@ function getStackedDiff(actual, expected) {
}

function getSimpleDiff(originalActual, actual, originalExpected, expected) {
const stringsLen = actual.length + expected.length;
let stringsLen = actual.length + expected.length;
// Accounting for the quotes wrapping strings
if (typeof actual === 'string') {
stringsLen -= 2;
}
if (typeof expected === 'string') {
stringsLen -= 2;
}
if (stringsLen <= kMaxShortStringLength && (originalActual !== 0 || originalExpected !== 0)) {
return { message: `${actual} !== ${expected}`, header: '' };
}
Expand Down
20 changes: 20 additions & 0 deletions test/parallel/test-assert-deep.js
Original file line number Diff line number Diff line change
Expand Up @@ -846,6 +846,26 @@ test('Additional tests', () => {
}
);

assert.throws(
() => assert.strictEqual('apple', 'pear'),
{
name: 'AssertionError',
message: 'Expected values to be strictly equal:\n\n\'apple\' !== \'pear\'\n'
}
);

assert.throws(
() => assert.strictEqual('ABABABABABAB', 'BABABABABABA'),
{
name: 'AssertionError',
message: 'Expected values to be strictly equal:\n' +
'+ actual - expected\n' +
'\n' +
"+ 'ABABABABABAB'\n" +
"- 'BABABABABABA'\n"
}
);

assert.notDeepStrictEqual(new Date(), new Date(2000, 3, 14));

assert.throws(
Expand Down

0 comments on commit 02d82f2

Please sign in to comment.