diff --git a/lib/internal/util/inspect.js b/lib/internal/util/inspect.js index 0279e5ecb155f1..e784b80b383d71 100644 --- a/lib/internal/util/inspect.js +++ b/lib/internal/util/inspect.js @@ -1913,8 +1913,11 @@ function reduceToSingleString( const start = output.length + ctx.indentationLvl + braces[0].length + base.length + 10; if (isBelowBreakLength(ctx, output, start, base)) { - return `${base ? `${base} ` : ''}${braces[0]} ${join(output, ', ')}` + - ` ${braces[1]}`; + const joinedOutput = join(output, ', ') + if (!joinedOutput.includes('\n')) { + return `${base ? `${base} ` : ''}${braces[0]} ${joinedOutput}` + + ` ${braces[1]}`; + } } } } diff --git a/test/parallel/test-util-inspect.js b/test/parallel/test-util-inspect.js index ca9d029e2f62b0..8a25edcd5b1c78 100644 --- a/test/parallel/test-util-inspect.js +++ b/test/parallel/test-util-inspect.js @@ -2572,6 +2572,24 @@ assert.strictEqual( assert.strictEqual(out, expected); + // Array grouping should prevent lining up outer elements on a single line. + obj = [[[1, 2, 3, 4, 5, 6, 7, 8, 9]]]; + + out = util.inspect(obj, { compact: 3 }); + + expected = [ + '[', + ' [', + ' [', + ' 1, 2, 3, 4, 5,', + ' 6, 7, 8, 9', + ' ]', + ' ]', + ']', + ].join('\n'); + + assert.strictEqual(out, expected); + // Verify that array grouping and line consolidation does not happen together. obj = { a: {