I've got a React custom hook that tries to avoid repeated calls to a URL. This is done by storing in state the isLoading prop, which is set to true whenever the fetch method is called, and is set to false once it receives the response from the server.
To test it, I need to be able to count how many times 'fetch' has been called, but I cannot seem to find any option or property or method inside nock library that gets me that.
There is a isDone method to know if all mocks have been fulfilled. There is a pendingMocks to know how many mocks haven't been fulfilled. But I can't find a way to count how many times fetch has been called, without caring about anything else (just the URL match).
They may be 100 times or just 2, just want to check how many times fetch has been called, just like toHaveFetchedTimes in fetch-mock-jest. Is there any way to do so in nock?
I find a way that works even it's not very clean.
In fact reply can take a callback function in second parameter that is called for every request that matches criteria.
let called = 0
nock
.post('/my/path')
.reply(200, () => {
called++
return { foo: 'bar' }
})
expect(called).toBe(1)
See: https://github.com/nock/nock#specifying-replies
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With