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

PR-URL: https://github.com/nodejs/node/pull/55474
Reviewed-By: Pietro Marchini <pietro.marchini94@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
This commit is contained in:
Giovanni Bucci
2024-11-02 16:12:12 +01:00
committed by GitHub
parent 81859cf01f
commit 10cce655ca
3 changed files with 31 additions and 16 deletions

View File

@@ -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: '' };
}

View File

@@ -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(

View File

@@ -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'
}
);
@@ -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'
}
);
@@ -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 */