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
PR-URL: nodejs#55474
Reviewed-By: Pietro Marchini <[email protected]>
Reviewed-By: Ruben Bridgewater <[email protected]>
  • Loading branch information
puskin94 authored and louwers committed Nov 2, 2024
1 parent 9aaefe8 commit 3226e50
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 16 deletions.
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 originalActual === 'string') {
stringsLen -= 2;
}
if (typeof originalExpected === '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
18 changes: 3 additions & 15 deletions test/parallel/test-assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -919,11 +919,7 @@ test('Additional asserts', () => {
{
code: 'ERR_ASSERTION',
constructor: assert.AssertionError,
message: 'Expected values to be strictly equal:\n' +
'+ actual - expected\n' +
'\n' +
"+ 'string'\n" +
'- false\n'
message: 'Expected values to be strictly equal:\n\n\'string\' !== false\n'
}
);

Expand All @@ -935,11 +931,7 @@ test('Additional asserts', () => {
{
code: 'ERR_ASSERTION',
constructor: assert.AssertionError,
message: 'Expected values to be strictly equal:\n' +
'+ actual - expected\n' +
'\n' +
"+ 'string'\n" +
'- false\n'
message: 'Expected values to be strictly equal:\n\n\'string\' !== false\n'
}
);

Expand All @@ -951,11 +943,7 @@ test('Additional asserts', () => {
}, {
code: 'ERR_ASSERTION',
constructor: assert.AssertionError,
message: 'Expected values to be strictly equal:\n' +
'+ actual - expected\n' +
'\n' +
"+ 'string'\n" +
'- false\n'
message: 'Expected values to be strictly equal:\n\n\'string\' !== false\n'
}
);
/* eslint-enable @stylistic/js/indent */
Expand Down

0 comments on commit 3226e50

Please sign in to comment.