What is it?
Track AJAX requests that have not been responded to by the Sinon.JS fake XMLHttpRequest server.
Why is it important?
It can be difficult to figure out whether a request has been responded to. Figuring it out can be time consuming and troublesome. This method catches everything.
How do we do it?
I use a global
variable to create the Sinon.js server like this:
Because I want to capture every AJAX request I do not put this into a
with matching restoration in
. Any AJAX request made on my testing page ought to be intercepted by the fake server.
So after every test one can check the Sinon.js
that tells us what requests were made:
Note that we empty the server responses and queue for the next test.
Now whenever a request is unmet, we get:
>>> Unmet Requests: /test_url /test_url_2
There are two caveats to this strategy. First, we cannot match the test that calls
to the request. The queue may contain requests made by latent asynchronous calls generated in a prior test. I am not certain this is a solvable problem.
Second, and in a related vein, if any request is made after all the tests are complete those requests will not be printed to the screen.
One can solve this latter problem by changing the
callback after the test-runner completes. For example:
This is just a little trick to rein in and tame what can be a wild asynchronous world. I find it quite helpful, and hope you find it valuable.