Replace setTimeout with $.ajax if you must, it amounts to the same thing.
Obviously the following test won't work, since it needs a 2 second break:
Qunit gets around this by having a stop() and start() call
Jasmine has a similar thing, but with a waitsFor function
I don't know about you, but I don't particularly want to have to wait around for 2 seconds to see if this test succeeds. The whole point of unit testing is to get rapid feedback when things break - 2 seconds per test is not what I'd call rapid!
Simple! Best of all, it's synchronous! Debugging this sort of test, should you need to, is simple.
Basil comes with a Sinon adapter, which handles the setup and restoration of the timers for every test, so it boils down to:
For completeness, if you have web service calls as well:
Using async support (in this case, of Jasmine):
This bit actually has a few big problems, I'm not sure how people get around this:
1) What timeout should I use for waitsFor? What is a reasonable amount of time?
2) How do I know what the server returns? Why do I even care?
3) I cannot run this test locally (from a file:// URL) - it has a hard dependency on my web app running, which may not be desirable
So, let's let SinonJS do the heavy lifting
Again, Basil can take away the create() and restore() of the fake server, making it a whole lot leaner.
This test no longer has any connectivity constraints or timeout dilemmas and enabled control over expected return values.