views:

59

answers:

2

When QUnit adds the test result details to your HTML document, it thoughtfully wraps the numbers of tests taken, passed and failed inside span elements, each with its own class, to let you recover these three numbers programmatically. However, even though I can see the spans in the finished HTML, I can't find them when I search with

jQuery('span.failed');   // For example

They aren't there during the onload event, although they are for the onunload event. Nor can I get them just after the QUnit test() calls.

What am I doing wrong?

+2  A: 

Javascript timing can be a little tricky. Instead of doing:

test();
yourMethod();

You might want to try doing:

test();
window.setTimeout(yourMethod, 1000);

Depending on what the issue is exactly, you may even be able to get away with 1 instead of 1000 (but I figure 1 second isn't that terrible in any case).

machineghost
That fixed it! The cut off point seems to be about 60ms for my browser/web page, so I'll stick with 100ms for safety. TVM.
Charles Anderson
+1  A: 

QUnit offers a callback-method, which you need to overwrite: QUnit.done(failures, total)

it is called when the last test has finished, and gets both the number of failed tests as well we the total number of tests. so you simply define

QUnit.done = function(failures, total) {
   // do whatever here
}

and that's it.

greets, rob.

Robert Prosenc
A much cleaner solution! Thanks.
Charles Anderson