Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

assert: fix the string length check for printing the simple diff #55474

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading