qUnit/mockjax - running multiple tests

I’m trying to write a test suite for a rather complicated js program with a lot of callbacks going on, and I’m hitting the wall of trying to run two tests within the same file. The first test checks behavior when the server returns an empty address book, the second when there is one address in the book…


/*
 * Testing Address Dialogs.
 */


$testStateList = function() { /*snip this is a sub test of assertions both tests need to test, that a select box was correctly built. */ }
$testCountryList = function() { /* ditto */}

/* This might be my problem, I'm trying to reuse this element.  I clone it here so that I
 * can start each test with a fresh copy
 */
var AddressesInOriginalState = document.getElementById('address').cloneNode(true);

asyncTest( "Empty Address Book", function() {	
    /*mockjax code here, omitted cause I doubt it's the problem */
	
   /* Attaching a new address div for this test to use */
	$('#address').remove();
	$('#loading').after(AddressesInOriginalState);

   // An ajax call is made here. I'm testing object state after the call. Need to wait a half a second.	
	TNT.init();

	setTimeout(function(){
		deepEqual(TNT.addressBook, [], 'Address Book should be an empty array');
		ok(!$('#loading').hasClass('Active'));
		ok($('#address').hasClass('Active'));
		ok(!$('#shippingMethod').hasClass('Active'));
		ok(!$('#paymentMethod').hasClass('Active'));		
		ok($('#address form').hasClass('Changed'));
		$testStateList();
		$testCountryList();
		
		ok($('#address .addressBook').is(':hidden'));
		
		start();
	}, 500);
});

asyncTest( "One Address In Book", function() {
 /*
  * snip - the only difference between the tests is that the response data they
  * receive and the assertions based on that data.
  */
});


What’s frustrating is these tests are atomic, they just cannot run simultaneously because they work on the same DOM object. I tried this guy’s plug in…

But it didn’t help.

Any ideas?

Bhueller? Bhueller??

(I ended up breaking the tests to multiple files for now, but that’s somewhat annoying).

I’m sure this is probably a daft question, but can’t you just create a separate copy of AddressesInOriginalState for each test?

EDIT: (reread post). No. That would require rewriting the code to satisfy the test where, in production, there’s only going to be one element, ever, and it is identified by a unique id. I suppose I could try to figure out a way to pass the element to the object as an argument… wait.

Isn’t it possible to run tests on a node or nodes that are detached from the DOM?

Exactly. I just need to rewrite the class being tested to take the object it is manipulating as a constructor object. Then each instance can be given their own element to work with.