DEV Community

Teerasak Vichadee
Teerasak Vichadee

Posted on

How to expect partial value in object with Jest

Bad ๐Ÿ‘Ž

expect(spyFunction).toReturn(expectReturn.id)
expect(spyFunction).toReturn(expectReturn.title)
expect(spyFunction).toHaveBeenCalledWith({ 
  ... /* very large object that I didn't care all of em */ 
})
Enter fullscreen mode Exit fullscreen mode

Good ๐Ÿ‘

expect(spyFunction).toReturn(expect.objectContain({
  id,
  title,
}))
expect(spyFunction).toHaveBeenCalledWith(
  expect.objectContain({ ... /* some meaningful value */ })
)
Enter fullscreen mode Exit fullscreen mode

Top comments (0)