Digiguru.co.uk

How to test async functions in qUnit

21 Mar 2015

Reading time: 1 minute

Writing a qUnit test is fairly straight forward. The syntax is as follows:

test("Here is a test", function() {
    ok(true, "A passing test.");
});

It doesn’t work when you have anything that replies on async behaviour (ajax, settimeout, animation ect…).

test("This won't work", function() {
    window.setTimeout(function () {
        ok(true, "This won't get associated with the test.");
    }, 1000);
});

The output is as follows:

Expected at least one assertion, but none were run - call expect(0) to accept zero assertions.

And then finds an assertion outside the test flow.

Uncaught Error: ok() assertion outside test context

If you want to write an asynchronous test, you will have to stop the test runner and tell it when to continue testing. This is the sytax to do so…

test("Here is an async", function() {
    stop();
    window.setTimeout(function () {
        start();
        ok(true, "A passing test.");
    }, 1000);
});

If you don’t like the concept of having to stop the test runner, there’s a shorthand way to write this test.

asyncTest("Another async test", function() {
    window.setTimeout(function () {
        start();
        ok(true, "Didn't have to say 'stop()'.");
    }, 1000);
});

This has been a primer for writing async tests in qUnit. In a later post, I will show a helper to help simulate ajax calls.