-
Notifications
You must be signed in to change notification settings - Fork 4.1k
Better test doubles for Xhr #8020
Description
As far as my team's aware, the current state of the art for test doubles that mimic Xhr-initiated network requests is Sinon stubs on methods like Xhr.prototype.fetchJson. These are very fragile; a change to how the code under test consumes the Xhr API can break such a test, even if the code's actual behavior with respect to the network is unchanged. A proper test double for Xhr should allow the test author to specify network requests in terms of their behavior (e.g., HTTP method, URL, body) and control the corresponding response behavior (i.e., cause the request to yield a provided Response, or to fail, at a time of the test author's choosing). The test author should also be able to make assertions about the number and properties of network requests that have been sent as of a given time.
If there's existing tooling that does this, I'd be interested to hear about it. I think Sinon has some kind of proxy server but it doesn't work with native fetch. If there isn't, would it be feasible to implement?