Skip to content

Commit a4cdf34

Browse files
authoredMay 6, 2023
fix: don't crash when printing recursive objects (#208)
1 parent 42a3801 commit a4cdf34

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed
 

‎src/complaining/formatComplaintCall.test.ts

+8
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,14 @@ describe("formatComplaintCall", () => {
77
[[true], "true"],
88
[[{}], "{}"],
99
[[{ inner: {} }], '{"inner":{}}'],
10+
[
11+
(() => {
12+
const x = { prop: {} };
13+
x.prop = x;
14+
return [x];
15+
})(),
16+
"A recursive object with keys: prop",
17+
],
1018
[[1, "two", [3]], '1, "two", [3]'],
1119
])(`formats %p as %p`, (args, expected) => {
1220
const actual = formatComplaintCall(args);

‎src/complaining/formatComplaintCall.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
import { SpyCallArgs } from "../spies/spyTypes";
22

33
const formatComplaintLineArg = (arg: unknown) => {
4-
return JSON.stringify(arg) || JSON.stringify(`${arg}`);
4+
try {
5+
return JSON.stringify(arg) || JSON.stringify(`${arg}`);
6+
} catch {
7+
return `A recursive object with keys: ${Object.keys(arg as {}).join(", ")}`;
8+
}
59
};
610

711
export const formatComplaintCall = (call: SpyCallArgs) =>

0 commit comments

Comments
 (0)
Please sign in to comment.