@@ -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 , / 9 9 9 / ) ;
258+ assertNotMatch ( e . message , / e x t r a / ) ;
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 , / n o i s e / ) ;
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 , / 9 9 9 / ) ;
285+ assertMatch ( e . message , / 4 2 / ) ;
286+ // extra top-level key should be omitted
287+ assertNotMatch ( e . message , / e x t r a / ) ;
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 , / 9 9 9 / ) ;
300+ } ) ;
247301} ) ;
0 commit comments