Asserting that a request has not been made #1735
Replies: 1 comment 2 replies
-
Hi, @EduardoSimon. Thanks for raising this. Currently, MSW does not provide any native APIs to perform request assertions. In general, request assertions are discouraged as they often indicate implementation detail testing. That includes your use case as well but in the library publishing world testing implementation details is oftentimes unavoidable. First things first, this isn't really an MSW issue/feature to cover. Still, let's talk about a test like this one. In a nutshell, it will consist of two parts:
Such negative network assertions are always tricky because the test itself has a limited timeframe while a request may still happen at You can achieve a test like that by attaching a runtime handler to the resource you don't wish to request, giving a mock function as its resolver, then running your hook, awaiting that timeout window, and asserting that the resolver function hasn't been called. test('skips the request if no "param" was provided', async () => {
const responseResolver = jest.fn()
server.use(
rest.get('/resource', responseResolver)
)
const { result } = renderHook(() => useTestHook())
// act and do actions
await delay(2000)
expect(responseResolver).not.toHaveBeenCalled()
}) |
Beta Was this translation helpful? Give feedback.
-
Hi,
We are developing a react library in which a hook is exposed. As part of the testing strategy, we need to make sure that a fetch request is not performed when a parameter is provided in the hook, something like:
I would like to implement something like:
Is it possible to approach the issue like I am trying to? I was having a look at the lifecycle events but I can't wrap my head around a possible solution.
Regards,
Edu
Beta Was this translation helpful? Give feedback.
All reactions