diff --git a/lib/internal/util/inspect.js b/lib/internal/util/inspect.js index c611eb97fc0755..1f282abcff4aec 100644 --- a/lib/internal/util/inspect.js +++ b/lib/internal/util/inspect.js @@ -2660,7 +2660,10 @@ function reduceToSingleString( // Consolidate all entries of the local most inner depth up to // `ctx.compact`, as long as the properties are smaller than // `ctx.breakLength`. - if (ctx.currentDepth - recurseTimes < ctx.compact && + if (((ctx.breakLength === Infinity && + (ctx.depth === Infinity || ctx.depth === null) && + ctx.compact === 3) || + ctx.currentDepth - recurseTimes < ctx.compact) && entries === output.length) { // Line up all entries on a single line in case the entries do not // exceed `breakLength`. Add 10 as constant to start next to all other diff --git a/test/parallel/test-util-inspect.js b/test/parallel/test-util-inspect.js index e60320d0591233..5af2708813d197 100644 --- a/test/parallel/test-util-inspect.js +++ b/test/parallel/test-util-inspect.js @@ -1615,6 +1615,36 @@ if (typeof Symbol !== 'undefined') { assert.strictEqual(twoLines, "{\n foo: 'abc',\n bar: 'xyz'\n}"); } +{ + const obj = { + a: { + b: { + c: { + d: 1, + }, + }, + }, + }; + + assert.strictEqual( + util.inspect(obj, { breakLength: Infinity, depth: Infinity }), + '{ a: { b: { c: { d: 1 } } } }', + ); + assert.strictEqual( + util.inspect(obj, { breakLength: Infinity, depth: null }), + '{ a: { b: { c: { d: 1 } } } }', + ); + assert.strictEqual( + util.inspect(obj, { breakLength: Infinity, depth: Infinity, compact: 3 }), + '{ a: { b: { c: { d: 1 } } } }', + ); + + assert.strictEqual( + util.inspect(obj, { breakLength: Infinity, depth: Infinity, compact: 1 }), + '{\n a: {\n b: {\n c: { d: 1 }\n }\n }\n}', + ); +} + // util.inspect.defaultOptions tests. { const arr = new Array(101).fill(); @@ -1631,6 +1661,12 @@ if (typeof Symbol !== 'undefined') { assert.doesNotMatch(util.inspect(obj), /Object/); util.inspect.defaultOptions.depth = oldOptions.depth; assert.match(util.inspect(obj), /Object/); + util.inspect.defaultOptions.compact = 1; + assert.strictEqual( + util.inspect(obj, { breakLength: Infinity, depth: Infinity }), + '{\n a: {\n a: {\n a: { a: 1 }\n }\n }\n}', + ); + util.inspect.defaultOptions.compact = oldOptions.compact; assert.strictEqual( JSON.stringify(util.inspect.defaultOptions), JSON.stringify(oldOptions)