runner icon indicating copy to clipboard operation
runner copied to clipboard

Question - How can I test the call of a method with toHaveBeenCalled()?

Open diegormb06 opened this issue 1 year ago • 2 comments

When I tried to run the test with toHaveBeenCalled, I got an error saying that the argument must be a mock or a spy. How can I create a spy to test a method call?

the test function is:

  test('updates a product', async ({ expect }) => {
    const updateData = { title: 'Updated Product' }
    mockProductRepository.update = async () => true

    const result = await productService.updateProduct(1, updateData)

    expect(result).toEqual(true)
    expect(mockProductRepository.update).toHaveBeenCalledWith(1, updateData)
  })

te error is: image

diegormb06 avatar Sep 29 '24 15:09 diegormb06

Not sure if this is directory related to Japa, since the mocking capabilities are part of the expect library. However, how do you create the mockProductRepository object?

thetutlage avatar Oct 09 '24 04:10 thetutlage

I just create a object with the methods, like I use to do with jest.

const mockProductRepository = {
  getAll: async () => ({}) as Paginated<Product>,
  update: async () => true,
  delete: async () => true,
}

diegormb06 avatar Oct 16 '24 00:10 diegormb06

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Dec 18 '24 05:12 stale[bot]