DEV Community

Lumin
Lumin

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)