fetch-mock: Mocked fetch does not support a call with Request as one argument

Mocked fetch does not support a call with Request as one argument:

fetch(new Request(...))

Produces errors like request.url.headers are undefined.

I have to call it like this in my code so far:

var req = new Request(...);
fetch(req.url, req)

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Comments: 21 (7 by maintainers)

Most upvoted comments

I agree, but I’ve asked before in this thread for somebody to provide a reduced test case. Also pull requests are welcome. Why not turn your temporary solution into a permanent solution for everyone 😄

I have this problem as well. fetch normally supports invoking it with a single argument of type Request (according to this documentation).

But from the very definition of the mocked fetch implementation provided by this library here, it becomes evident that the mocked fetch always expect fetch to be called as fetch(url, options). No where in that method it is considered the possibility that url is not a string, or that options could be missing. In fact, the only thing done around option being missing is defaulting to method GET. I actually realized all this when fetch-mock was giving me an error that there was no fallback response for a GET request, in an example where I was only firing a single POST request. The only caveat is that in my app, the requests are built as a Request object, which is then passed to fetch.