Skip to content

Commit

Permalink
fix: don't crash when printing recursive objects (#208)
Browse files Browse the repository at this point in the history
  • Loading branch information
JoshuaKGoldberg authored May 6, 2023
1 parent 42a3801 commit a4cdf34
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
8 changes: 8 additions & 0 deletions src/complaining/formatComplaintCall.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@ describe("formatComplaintCall", () => {
[[true], "true"],
[[{}], "{}"],
[[{ inner: {} }], '{"inner":{}}'],
[
(() => {
const x = { prop: {} };
x.prop = x;
return [x];
})(),
"A recursive object with keys: prop",
],
[[1, "two", [3]], '1, "two", [3]'],
])(`formats %p as %p`, (args, expected) => {
const actual = formatComplaintCall(args);
Expand Down
6 changes: 5 additions & 1 deletion src/complaining/formatComplaintCall.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import { SpyCallArgs } from "../spies/spyTypes";

const formatComplaintLineArg = (arg: unknown) => {
return JSON.stringify(arg) || JSON.stringify(`${arg}`);
try {
return JSON.stringify(arg) || JSON.stringify(`${arg}`);
} catch {
return `A recursive object with keys: ${Object.keys(arg as {}).join(", ")}`;
}
};

export const formatComplaintCall = (call: SpyCallArgs) =>
Expand Down

0 comments on commit a4cdf34

Please sign in to comment.