Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion _tools/node_test_runner/run_test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍


for (const testDef of testDefinitions) {
test(testDef.name, testDef.fn);
test(testDef.name, (t) =>
testDef.fn({ step: (name, fn) => t.test(name, fn) })
);
}
13 changes: 12 additions & 1 deletion internal/format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -33,5 +34,15 @@
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.

Check warning on line 43 in internal/format.ts

View check run for this annotation

Codecov / codecov/patch

internal/format.ts#L43

Added line #L43 was not covered by tests
getters: true,
strAbbreviateSize: Infinity,
})
: `"${String(v).replace(/(?=["\\])/g, "\\")}"`;
}
189 changes: 137 additions & 52 deletions internal/format_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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") {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you use ignore option of Deno.test instead of big if block to exclude some tests in some environment? ref https://docs.deno.com/api/deno/~/Deno.TestDefinition

// 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,
Expand All @@ -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",
Expand All @@ -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;
}
});
Loading