Skip to main content

All Questions

Tagged with
Filter by
Sorted by
Tagged with
4 votes
3 answers
8k views

How do I mock Node.js fetch HTTP requests/responses in Node 18?

I am using the new (as of version 18) Node.js "fetch" API to perform HTTP requests e.g. const response = await fetch(SOMEURL) const json = await response.json() This works, but I want to &...
Glynn Bird's user avatar
  • 5,627
4 votes
1 answer
2k views

Nock - Get number of calls performed to fetch

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 ...
Unapedra's user avatar
  • 2,363
0 votes
1 answer
809 views

nock handles fetch req but does not prevent http call

I am using nock to intercept a fetch call to https://www.youtube.com/oembed?url=https://youtu.be/m4hklkGvTGQ . Like so: it(`properly responds to YouTube URL ${url}`, async () => { nock('https:/...
Lukas's user avatar
  • 10.3k
3 votes
1 answer
4k views

Using nock to test POST request in async action creator

I am using nock to mock my asynchronous thunk action creators. Everything works as expected for GET requests, but I haven't been able to make a POST request work. I have read everything I can find ...
withintheruins14's user avatar
5 votes
1 answer
2k views

Nock intercepts request but returns empty object

I'm using mocha as the testing framework and I'm trying to mock up a DELETE request that uses fetch against an end point that returns HTTP status code 204. Here is the test code: it('should logout ...
rfc1484's user avatar
  • 9,819