How to expect partial value in object with Jest
Lumin

Lumin @ilumin

Location:
Bangkok, Thailand
Joined:
Jan 25, 2018

How to expect partial value in object with Jest

Publish Date: Jun 1 '22
6 0

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

Comments 0 total

    Add comment