Skip to content

Commit de91221

Browse files
committed
add to node tests
1 parent 6a821c2 commit de91221

File tree

3 files changed

+96
-71
lines changed

3 files changed

+96
-71
lines changed

_tools/node_test_runner/run_test.mjs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,10 @@ import "../../fs/unstable_lstat_test.ts";
7373
import "../../fs/unstable_chmod_test.ts";
7474
import "../../fs/unstable_umask_test.ts";
7575
import "../../fs/unstable_utime_test.ts";
76+
import "../../internal/format_test.ts";
7677

7778
for (const testDef of testDefinitions) {
78-
test(testDef.name, testDef.fn);
79+
test(testDef.name, (t) =>
80+
testDef.fn({ step: (name, fn) => t.test(name, fn) })
81+
);
7982
}

internal/format.ts

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,20 +22,27 @@
2222
export function format(v: unknown): string {
2323
// deno-lint-ignore no-explicit-any
2424
const { Deno, process } = globalThis as any;
25-
const options = {
26-
depth: Infinity,
27-
sorted: true,
28-
trailingComma: true,
29-
compact: false,
30-
iterableLimit: Infinity,
31-
// getters should be true in assertEquals.
32-
getters: true,
33-
strAbbreviateSize: Infinity,
34-
};
3525

3626
return typeof Deno?.inspect === "function"
37-
? Deno.inspect(v, options)
27+
? Deno.inspect(v, {
28+
depth: Infinity,
29+
sorted: true,
30+
trailingComma: true,
31+
compact: false,
32+
iterableLimit: Infinity,
33+
// getters should be true in assertEquals.
34+
getters: true,
35+
strAbbreviateSize: Infinity,
36+
})
3837
: typeof process?.getBuiltinModule === "function"
39-
? process.getBuiltinModule("node:util").inspect(v, options)
38+
? process.getBuiltinModule("node:util").inspect(v, {
39+
depth: Infinity,
40+
sorted: true,
41+
compact: false,
42+
iterableLimit: Infinity,
43+
// getters should be true in assertEquals.
44+
getters: true,
45+
strAbbreviateSize: Infinity,
46+
})
4047
: `"${String(v).replace(/(?=["\\])/g, "\\")}"`;
4148
}

internal/format_test.ts

Lines changed: 73 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -21,46 +21,47 @@ ${red("- d")}
2121
);
2222
});
2323

24-
// Check that the diff formatter overrides some default behaviours of
25-
// `Deno.inspect()` which are problematic for diffing.
26-
Deno.test("format() overrides default behaviours of Deno.inspect", async (t) => {
27-
// Wraps objects into multiple lines even when they are small. Prints trailing
28-
// commas.
29-
await t.step(
30-
"format() always wraps objects into multiple lines and prints trailing commas",
31-
() =>
32-
assertEquals(
33-
stripAnsiCode(format({ a: 1, b: 2 })),
34-
`{
24+
if (typeof Deno.inspect === "function") {
25+
// Check that the diff formatter overrides some default behaviours of
26+
// `Deno.inspect()` which are problematic for diffing.
27+
Deno.test("format() overrides default behaviours of Deno.inspect", async (t) => {
28+
// Wraps objects into multiple lines even when they are small. Prints trailing
29+
// commas.
30+
await t.step(
31+
"format() always wraps objects into multiple lines and prints trailing commas",
32+
() =>
33+
assertEquals(
34+
stripAnsiCode(format({ a: 1, b: 2 })),
35+
`{
3536
a: 1,
3637
b: 2,
3738
}`,
38-
),
39-
);
39+
),
40+
);
4041

41-
await t.step("format() sorts properties", () =>
42-
assertEquals(
43-
format({ b: 2, a: 1 }),
44-
format({ a: 1, b: 2 }),
45-
));
42+
await t.step("format() sorts properties", () =>
43+
assertEquals(
44+
format({ b: 2, a: 1 }),
45+
format({ a: 1, b: 2 }),
46+
));
4647

47-
await t.step("format() wraps Object with getters", () =>
48-
assertEquals(
49-
format(Object.defineProperty({}, "a", {
50-
enumerable: true,
51-
get() {
52-
return 1;
53-
},
54-
})),
55-
`{
48+
await t.step("format() wraps Object with getters", () =>
49+
assertEquals(
50+
format(Object.defineProperty({}, "a", {
51+
enumerable: true,
52+
get() {
53+
return 1;
54+
},
55+
})),
56+
`{
5657
a: [Getter: 1],
5758
}`,
58-
));
59+
));
5960

60-
await t.step("format() wraps nested small objects", () =>
61-
assertEquals(
62-
stripAnsiCode(format([{ x: { a: 1, b: 2 }, y: ["a", "b"] }])),
63-
`[
61+
await t.step("format() wraps nested small objects", () =>
62+
assertEquals(
63+
stripAnsiCode(format([{ x: { a: 1, b: 2 }, y: ["a", "b"] }])),
64+
`[
6465
{
6566
x: {
6667
a: 1,
@@ -72,13 +73,13 @@ Deno.test("format() overrides default behaviours of Deno.inspect", async (t) =>
7273
],
7374
},
7475
]`,
75-
));
76+
));
7677

77-
// Grouping is disabled.
78-
await t.step("format() disables grouping", () =>
79-
assertEquals(
80-
stripAnsiCode(format(["i", "i", "i", "i", "i", "i", "i"])),
81-
`[
78+
// Grouping is disabled.
79+
await t.step("format() disables grouping", () =>
80+
assertEquals(
81+
stripAnsiCode(format(["i", "i", "i", "i", "i", "i", "i"])),
82+
`[
8283
"i",
8384
"i",
8485
"i",
@@ -87,30 +88,31 @@ Deno.test("format() overrides default behaviours of Deno.inspect", async (t) =>
8788
"i",
8889
"i",
8990
]`,
90-
));
91-
});
92-
93-
Deno.test("format() doesn't truncate long strings in object", () => {
94-
const str = format({
95-
foo:
96-
"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.",
91+
));
9792
});
98-
assertEquals(
99-
str,
100-
`{
93+
94+
Deno.test("format() doesn't truncate long strings in object", () => {
95+
const str = format({
96+
foo:
97+
"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.",
98+
});
99+
assertEquals(
100+
str,
101+
`{
101102
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.",
102103
}`,
103-
);
104-
});
104+
);
105+
});
106+
}
105107

106108
Deno.test("format() has fallback to util.inspect if Deno.inspect is not available", async (t) => {
107109
// Simulates the environment where Deno.inspect is not available
108110
const inspect = Deno.inspect;
109111
// deno-lint-ignore no-explicit-any
110112
delete (Deno as any).inspect;
111113
try {
112-
assertEquals(format([..."abcd"]), `[\n 'a',\n 'b',\n 'c',\n 'd',\n]`);
113-
assertEquals(format({ a: 1, b: 2 }), `{\n a: 1,\n b: 2,\n}`);
114+
assertEquals(format([..."abcd"]), `[\n 'a',\n 'b',\n 'c',\n 'd'\n]`);
115+
assertEquals(format({ a: 1, b: 2 }), `{\n a: 1,\n b: 2\n}`);
114116
await t.step("format() sorts properties", () =>
115117
assertEquals(
116118
format({ b: 2, a: 1 }),
@@ -126,7 +128,7 @@ Deno.test("format() has fallback to util.inspect if Deno.inspect is not availabl
126128
},
127129
})),
128130
`{
129-
a: [Getter: 1],
131+
a: [Getter: 1]
130132
}`,
131133
));
132134

@@ -137,13 +139,13 @@ Deno.test("format() has fallback to util.inspect if Deno.inspect is not availabl
137139
{
138140
x: {
139141
a: 1,
140-
b: 2,
142+
b: 2
141143
},
142144
y: [
143145
'a',
144-
'b',
145-
],
146-
},
146+
'b'
147+
]
148+
}
147149
]`,
148150
));
149151

@@ -158,9 +160,21 @@ Deno.test("format() has fallback to util.inspect if Deno.inspect is not availabl
158160
'i',
159161
'i',
160162
'i',
161-
'i',
163+
'i'
162164
]`,
163165
));
166+
await t.step("format() doesn't truncate long strings in object", () => {
167+
const str = format({
168+
foo:
169+
"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.",
170+
});
171+
assertEquals(
172+
str,
173+
`{
174+
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.'
175+
}`,
176+
);
177+
});
164178
} finally {
165179
Deno.inspect = inspect;
166180
}
@@ -174,6 +188,7 @@ Deno.test("format() has fallback to String if util.inspect and Deno.inspect are
174188
// deno-lint-ignore no-explicit-any
175189
const { process } = globalThis as any;
176190
const getBuiltinModule = process.getBuiltinModule;
191+
delete process.getBuiltinModule;
177192
try {
178193
assertEquals(format([..."abcd"]), `"a,b,c,d"`);
179194
assertEquals(format({ a: 1, b: 2 }), `"[object Object]"`);

0 commit comments

Comments
 (0)