diff --git a/_tools/node_test_runner/run_test.mjs b/_tools/node_test_runner/run_test.mjs index 187b53585eb2..364a5fa0285b 100644 --- a/_tools/node_test_runner/run_test.mjs +++ b/_tools/node_test_runner/run_test.mjs @@ -73,7 +73,10 @@ import "../../fs/unstable_lstat_test.ts"; import "../../fs/unstable_chmod_test.ts"; import "../../fs/unstable_umask_test.ts"; import "../../fs/unstable_utime_test.ts"; +import "../../internal/format_test.ts"; for (const testDef of testDefinitions) { - test(testDef.name, testDef.fn); + test(testDef.name, (t) => + testDef.fn({ step: (name, fn) => t.test(name, fn) }) + ); } diff --git a/internal/format.ts b/internal/format.ts index 6e186381e950..f04d826287fc 100644 --- a/internal/format.ts +++ b/internal/format.ts @@ -21,7 +21,8 @@ */ export function format(v: unknown): string { // deno-lint-ignore no-explicit-any - const { Deno } = globalThis as any; + const { Deno, process } = globalThis as any; + return typeof Deno?.inspect === "function" ? Deno.inspect(v, { depth: Infinity, @@ -33,5 +34,15 @@ export function format(v: unknown): string { getters: true, strAbbreviateSize: Infinity, }) + : typeof process?.getBuiltinModule === "function" + ? process.getBuiltinModule("node:util").inspect(v, { + depth: Infinity, + sorted: true, + compact: false, + iterableLimit: Infinity, + // getters should be true in assertEquals. + getters: true, + strAbbreviateSize: Infinity, + }) : `"${String(v).replace(/(?=["\\])/g, "\\")}"`; } diff --git a/internal/format_test.ts b/internal/format_test.ts index 777fcccc4859..ddbbaafcdb25 100644 --- a/internal/format_test.ts +++ b/internal/format_test.ts @@ -21,46 +21,47 @@ ${red("- d")} ); }); -// Check that the diff formatter overrides some default behaviours of -// `Deno.inspect()` which are problematic for diffing. -Deno.test("format() overrides default behaviours of Deno.inspect", async (t) => { - // Wraps objects into multiple lines even when they are small. Prints trailing - // commas. - await t.step( - "format() always wraps objects into multiple lines and prints trailing commas", - () => - assertEquals( - stripAnsiCode(format({ a: 1, b: 2 })), - `{ +if (typeof Deno.inspect === "function") { + // Check that the diff formatter overrides some default behaviours of + // `Deno.inspect()` which are problematic for diffing. + Deno.test("format() overrides default behaviours of Deno.inspect", async (t) => { + // Wraps objects into multiple lines even when they are small. Prints trailing + // commas. + await t.step( + "format() always wraps objects into multiple lines and prints trailing commas", + () => + assertEquals( + stripAnsiCode(format({ a: 1, b: 2 })), + `{ a: 1, b: 2, }`, - ), - ); + ), + ); - await t.step("format() sorts properties", () => - assertEquals( - format({ b: 2, a: 1 }), - format({ a: 1, b: 2 }), - )); + await t.step("format() sorts properties", () => + assertEquals( + format({ b: 2, a: 1 }), + format({ a: 1, b: 2 }), + )); - await t.step("format() wraps Object with getters", () => - assertEquals( - format(Object.defineProperty({}, "a", { - enumerable: true, - get() { - return 1; - }, - })), - `{ + await t.step("format() wraps Object with getters", () => + assertEquals( + format(Object.defineProperty({}, "a", { + enumerable: true, + get() { + return 1; + }, + })), + `{ a: [Getter: 1], }`, - )); + )); - await t.step("format() wraps nested small objects", () => - assertEquals( - stripAnsiCode(format([{ x: { a: 1, b: 2 }, y: ["a", "b"] }])), - `[ + await t.step("format() wraps nested small objects", () => + assertEquals( + stripAnsiCode(format([{ x: { a: 1, b: 2 }, y: ["a", "b"] }])), + `[ { x: { a: 1, @@ -72,13 +73,13 @@ Deno.test("format() overrides default behaviours of Deno.inspect", async (t) => ], }, ]`, - )); + )); - // Grouping is disabled. - await t.step("format() disables grouping", () => - assertEquals( - stripAnsiCode(format(["i", "i", "i", "i", "i", "i", "i"])), - `[ + // Grouping is disabled. + await t.step("format() disables grouping", () => + assertEquals( + stripAnsiCode(format(["i", "i", "i", "i", "i", "i", "i"])), + `[ "i", "i", "i", @@ -87,31 +88,115 @@ Deno.test("format() overrides default behaviours of Deno.inspect", async (t) => "i", "i", ]`, - )); -}); - -Deno.test("format() doesn't truncate long strings in object", () => { - const str = format({ - foo: - "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.", + )); }); - assertEquals( - str, - `{ + + Deno.test("format() doesn't truncate long strings in object", () => { + const str = format({ + foo: + "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.", + }); + assertEquals( + str, + `{ foo: "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.", }`, - ); -}); + ); + }); +} + +// deno-lint-ignore no-explicit-any +if (typeof (globalThis as any).process?.getBuiltinModule === "function") { + Deno.test("format() has fallback to util.inspect if Deno.inspect is not available", async (t) => { + // Simulates the environment where Deno.inspect is not available + const inspect = Deno.inspect; + // deno-lint-ignore no-explicit-any + delete (Deno as any).inspect; + try { + assertEquals(format([..."abcd"]), `[\n 'a',\n 'b',\n 'c',\n 'd'\n]`); + assertEquals(format({ a: 1, b: 2 }), `{\n a: 1,\n b: 2\n}`); + await t.step("format() sorts properties", () => + assertEquals( + format({ b: 2, a: 1 }), + format({ a: 1, b: 2 }), + )); + + await t.step("format() wraps Object with getters", () => + assertEquals( + format(Object.defineProperty({}, "a", { + enumerable: true, + get() { + return 1; + }, + })), + `{ + a: [Getter: 1] +}`, + )); + + await t.step("format() wraps nested small objects", () => + assertEquals( + stripAnsiCode(format([{ x: { a: 1, b: 2 }, y: ["a", "b"] }])), + `[ + { + x: { + a: 1, + b: 2 + }, + y: [ + 'a', + 'b' + ] + } +]`, + )); -Deno.test("format() has fallback to String if Deno.inspect is not available", () => { - // Simulates the environment where Deno.inspect is not available + // Grouping is disabled. + await t.step("format() disables grouping", () => + assertEquals( + stripAnsiCode(format(["i", "i", "i", "i", "i", "i", "i"])), + `[ + 'i', + 'i', + 'i', + 'i', + 'i', + 'i', + 'i' +]`, + )); + await t.step("format() doesn't truncate long strings in object", () => { + const str = format({ + foo: + "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.", + }); + assertEquals( + str, + `{ + foo: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.' +}`, + ); + }); + } finally { + Deno.inspect = inspect; + } + }); +} + +Deno.test("format() has fallback to String if util.inspect and Deno.inspect are not available", () => { + // Simulates the environment where Deno.inspect and util.inspect are not available const inspect = Deno.inspect; // deno-lint-ignore no-explicit-any delete (Deno as any).inspect; + // deno-lint-ignore no-explicit-any + const { process } = globalThis as any; + const getBuiltinModule = process?.getBuiltinModule; + delete process?.getBuiltinModule; try { assertEquals(format([..."abcd"]), `"a,b,c,d"`); assertEquals(format({ a: 1, b: 2 }), `"[object Object]"`); } finally { Deno.inspect = inspect; + if (process) process.getBuiltinModule = getBuiltinModule; } });