msw: allow passthrough for undefined routes / graphql interferes with passthrough

Describe the solution you’d like

I’d like to be able to allow certain matching urls to be passed through and handled normally instead of intercepting and giving 404.

Describe alternatives you’ve considered

Seems like a lot of work to manually define ctx.fetch for each request I would want to pass through on some wildcard domain / string match.

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Comments: 16 (9 by maintainers)

Most upvoted comments

thanks @kettanaito , just upgraded to 0.20.5 and it works without the empty handlers!

@kettanaito I could use this feature as well for testcafe support. testcafe has some internal posts it makes back to it’s proxy server, and we can’t add headers to those requests. I noticed that if i just add resolve(getOriginalResponse()); to the INTERNAL_ERROR handler in mockServiceWorker.js it works, but that felt wrong to do.

Thanks @kettanaito ! Yes looks like I was also searching for “passthrough” and not “bypass” 😦

returning nothing definitely works! – although I did not understand this from the docs, and also I get type errors using your solution – I got around it by:

  (rest as any).post('https://my.auth0.com/oauth/token', () => {
    return;
  }),

Here’s the type error with using any

 Type 'void' is not assignable to type 'MockedResponse | Promise<MockedResponse>'.ts(2345)

A couple things about the docs:

  • some how I ended up on https://redd.gitbook.io/msw/api/compose-mocks thinking that was the official docs? it does have search functionality which is nice

  • after finding mswjs.io/docs – I think bypass and (as I was searching “passthrough”) could be more prevalent.

I might also make it a default option on .start({ bypassUnmocked: true }) or something to that effect.

Returning nothing to bypass was not clear to me – I thought I needed to return ctx.fetch(req) as per https://redd.gitbook.io/msw/recipes/query-parameters#conditional-mocking

To answer your Q:

If I understand correctly, the absence of a request handler is what you mean by “for undefined routes”.

Yes, undefined routes might automatically be allowed passthrough based on that config setting. Similar to axios-mock-adapter has the passthrough option.

IE if absent handlers, then allow the original request to go through

Hey, @ilovett. Thanks for raising this.

Just to clarify: if your response resolver doesn’t return anything, that request will be bypassed, even if it matches a predicate. To illustrate, this should log out each GET request your app makes, while performing them as-is:

rest.get('*', (req, res, ctx) => {
  console.log(req.url.href)
  // Notice no `return res()` statement
})

You can utilize that behavior to conditionally short circuit in a resolver:

res.get('/auth', (req, res, ctx) => {
  if (someCondition) {
    // Return nothing, thus, bypassing this request
    return null
  }

  // Return a mocked response
  return res(...)
})

Also, any request that does not have a corresponding request handler is performed passthrough by default. If I understand correctly, the absence of a request handler is what you mean by “for undefined routes”. Did I get it correctly? (It would be very helpful if you included an example of what you are talking about, even if in pseudo-code).

An auth0 recipe may be helpful.

A good point 👍 We may have to list such recipe on the docs. Perhaps, even as an outcome of our discussion here!