wheresrhys/fetch-mock

Returning different values for different requests on the same URL

julienw opened this issue · 4 comments

Hi,

I'd like to do something like this:

  1. First request to : 404
  2. Second request to : actual result

So I tried this:

      fetch
        .getOnce(expectedUrl, 404)
        .get(expectedUrl, result);

But this throws because of Adding route with same name or matcher as existing route. See overwriteRoutes option.
It's not super clear which values of overwriteRoutes would work, so I tried first with true then false.

Finally this works:

      fetch
        .getOnce(expectedUrl, 404)
        .get(expectedUrl, result, {
          overwriteRoutes: false,
        });

But I wonder how this could be improved. Maybe this could be a default? Or some guide in the documentation?

Thanks!

Sorry for the slow response.

The recommended approach is to name your routes e.g.

      fetch
        .getOnce(expectedUrl, 404, {name: 'not found'})
        .get(expectedUrl, result, {name: 'found'});

Thanks for the answer!
Maybe the error Adding route with same name or matcher as existing route. See overwriteRoutes option should say something about name too:

'fetch-mock: Adding route with same name or matcher as existing route. See `overwriteRoutes` option.',

what do you think @wheresrhys ?

Yep good suggestion. I would daccept a PR to improve it

Closing due to inactivity. Will be addressed in a different way in @fetch-mock/core