we got a Vue application which calls an api endpoint using vueuses useFetch
. It is tested with Vitest and msw (mock service worker) to check our API calls. Our code looks like the following:
// api.ts
async function postEndpoint: Promise<void> {
const body = complexObject
const { error } = await useFetch<MyDto>('/api/endpoint').post(body)
if (error.value) throw new Error(error.value)
}
As you can see the happy path does not return any value. However, we need to make sure wheter the api endpoint has been called with the correct body. Therefore we want to check spy on fetch
:
describe('when the contact form is posted', () => {
beforeEach(() => {
fetchSpy = vi.spyOn(window, 'fetch')
server.use(
rest.post('/api/endpoint', (req, res, ctx) => {
return res(ctx.status(201))
})
)
})
it('should call the endpoint ', async () => {
const result = await postEndpoint(contactForm)
// TODO - assert fetch is using the correct body
expect(fetchSpy).toHaveBeenCalledWith(
'/api/endpoint',
expect.objectContaining({ body: JSON.stringify(complexObject)})
)
})
})
However our assertion does not work correctly, because the string serialization of the complex object does not fit with the actual posted body-string. It has the same content, but the order of the members differ.
How can we fix this test and ensure wheter the api has been called with the correct body even if our api-method postEndpoint
has no return value??
Although I generally discourage against request assertions, I leave you to be the judge of whether that's necessary in your tests.
Here's how you can get the request body and assert it in your test: