Skip to content

Commit d949ead

Browse files
Trotttargos
authored andcommitted
test: check custom inspection truncation in assert
The assert module has some truncation logic in a custom inspect function. This was not covered in tests. Add tests to cover it. PR-URL: #28234 Reviewed-By: Ruben Bridgewater <[email protected]> Reviewed-By: Daniel Bevenius <[email protected]>
1 parent 990feaf commit d949ead

File tree

1 file changed

+25
-4
lines changed

1 file changed

+25
-4
lines changed

test/parallel/test-assert.js

+25-4
Original file line numberDiff line numberDiff line change
@@ -412,12 +412,33 @@ assert.throws(() => { throw new Error(); }, (err) => err instanceof Error);
412412
// Long values should be truncated for display.
413413
assert.throws(() => {
414414
assert.strictEqual('A'.repeat(1000), '');
415-
}, {
416-
code: 'ERR_ASSERTION',
417-
message: `${strictEqualMessageStart}+ actual - expected\n\n` +
418-
`+ '${'A'.repeat(1000)}'\n- ''`
415+
}, (err) => {
416+
assert.strictEqual(err.code, 'ERR_ASSERTION');
417+
assert.strictEqual(err.message,
418+
`${strictEqualMessageStart}+ actual - expected\n\n` +
419+
`+ '${'A'.repeat(1000)}'\n- ''`);
420+
assert.strictEqual(err.actual.length, 1000);
421+
assert.ok(inspect(err).includes(`actual: '${'A'.repeat(488)}...'`));
422+
return true;
419423
});
420424

425+
// Output that extends beyond 10 lines should also be truncated for display.
426+
{
427+
const multilineString = 'fhqwhgads\n'.repeat(15);
428+
assert.throws(() => {
429+
assert.strictEqual(multilineString, '');
430+
}, (err) => {
431+
assert.strictEqual(err.code, 'ERR_ASSERTION');
432+
assert.strictEqual(err.message.split('\n').length, 19);
433+
assert.strictEqual(err.actual.split('\n').length, 16);
434+
assert.ok(inspect(err).includes(
435+
"actual: 'fhqwhgads\\n' +\n" +
436+
" 'fhqwhgads\\n' +\n".repeat(9) +
437+
" '...'"));
438+
return true;
439+
});
440+
}
441+
421442
{
422443
// Bad args to AssertionError constructor should throw TypeError.
423444
const args = [1, true, false, '', null, Infinity, Symbol('test'), undefined];

0 commit comments

Comments
 (0)