DEV Community

Lumin
Lumin

Posted on

3 3

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)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs