The common question that comes to mind is how to test asynchronous (Ajax) JavaScript code using QUnit. What has been mentioned in the chapter so far is how to perform unit testing for synchronous JavaScript code. QUnit provides two main APIs, namely stop()
and start()
, in order to perform real Ajax testing. Let me show you how to use them.
The stop()
API stops the QUnit test runner until the
start()
API is called or the test function is timed out. For example:
QUnit.config.testTimeout = 10000; test("test function1", function() { stop(); window.setTimeout(function() { ok(true); start(); }, 3000); });
As shown in the preceding code snippet, the "test function1"
function stops the QUnit test runner by calling the stop()
API. The window.setTimeout
function resumes the test runner by calling the start()
API after 3000
milliseconds.
In order to specify the test function timeout, you can set the global property QUnit...