Skip to content

Commit 533cc53

Browse files
author
sandbox
committed
test(expect): add coverage for getObjectSubset branches
1 parent 7788a21 commit 533cc53

1 file changed

Lines changed: 54 additions & 0 deletions

File tree

expect/_to_match_object_test.ts

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,4 +244,58 @@ Deno.test("expect().toMatchObject() displays a diff", async (t) => {
244244
assertNotMatch(e.message, /c:/);
245245
assertNotMatch(e.message, /f:/);
246246
});
247+
248+
await t.step("omits keys not in expected with arrays", () => {
249+
const e = assertThrows(
250+
() =>
251+
expect([{ a: 1, extra: true }, { b: 2 }]).toMatchObject([
252+
{ a: 999 },
253+
{ b: 2 },
254+
]),
255+
AssertionError,
256+
);
257+
assertMatch(e.message, /999/);
258+
assertNotMatch(e.message, /extra/);
259+
});
260+
261+
await t.step("omits keys not in expected with Date values", () => {
262+
const e = assertThrows(
263+
() =>
264+
expect({
265+
d: new Date("2020-01-01"),
266+
extra: "noise",
267+
}).toMatchObject({ d: new Date("2025-01-01") }),
268+
AssertionError,
269+
);
270+
assertNotMatch(e.message, /noise/);
271+
});
272+
273+
await t.step("omits keys not in expected with equal nested subset", () => {
274+
const e = assertThrows(
275+
() =>
276+
expect({
277+
a: { x: 1, y: 2 },
278+
b: 999,
279+
extra: true,
280+
}).toMatchObject({ a: { x: 1 }, b: 42 }),
281+
AssertionError,
282+
);
283+
// b is the mismatch
284+
assertMatch(e.message, /999/);
285+
assertMatch(e.message, /42/);
286+
// extra top-level key should be omitted
287+
assertNotMatch(e.message, /extra/);
288+
// y should be omitted (not in expected.a)
289+
assertNotMatch(e.message, /y:/);
290+
});
291+
292+
await t.step("handles circular references without throwing", () => {
293+
const received: Record<string, unknown> = { a: 1 };
294+
received.self = received;
295+
const e = assertThrows(
296+
() => expect(received).toMatchObject({ a: 999, self: {} }),
297+
AssertionError,
298+
);
299+
assertMatch(e.message, /999/);
300+
});
247301
});

0 commit comments

Comments
 (0)