jameslnewell / xhr-mock

Utility for mocking XMLHttpRequest.
196 stars 48 forks source link

Replace the global XMLHttpRequest only for one request #38

Closed maksugr closed 6 years ago

maksugr commented 6 years ago

Do we can replace the global XMLHttpRequest only for one request? Right now, if you make .setup() all others XMLHttpRequest, that doesn't have mocks, doesn't work.

jameslnewell commented 6 years ago

No, the current implementation requires you to call .teardown() when you want to stop mocking requests. And I think this is the optimal behaviour (because who's to say it shouldn't be two requests or three requests?).

Instead, you should call .teardown(). e.g.

mock.get('/first/request', (req, res) => {
  mock.teardown();
  return res;
});

Alternatively, you could try proxying unmocked requests to the real XHR e.g (albeit untested) https://github.com/jameslnewell/xhr-mock#proxying-requests