Ajax requests are some of the best things you can test. It’s very easy for them to go wrong (server issues? connection issues? etc.), so a bug in error handling can be very frustrating to users. They are also one of the boundaries of your code – places where data is loaded from the outside world – and those points are often prone to errors.
But testing them can be tricky. It’s asynchronous, and a good unit test must be isolated, so how can we do that when the code talks to the server?
Let’s take a look at some examples to learn how to test Ajax requests with ease.