diff --git a/lib/internal/util/inspect.js b/lib/internal/util/inspect.js index 563ab00262a356..ceaf8e133788ca 100644 --- a/lib/internal/util/inspect.js +++ b/lib/internal/util/inspect.js @@ -1931,8 +1931,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 65736cd62a8111..34cc5cc56af48b 100644 --- a/test/parallel/test-util-inspect.js +++ b/test/parallel/test-util-inspect.js @@ -2631,6 +2631,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: {