This is an example, adapted from your original code, that executes the setup method for each test method:
function doExpensiveSetupForModuleA() {
console.log("setup A");
}
function testA1() {
console.log("testA1");
start();
}
function testA2() {
console.log("testA2");
start();
}
function testA3() {
console.log("testA3");
start();
}
function doExpensiveSetupForModuleB() {
console.log("setup B");
}
function testB1() {
console.log("testB1");
start();
}
function testB2() {
console.log("testB2");
start();
}
function testB3() {
console.log("testB3");
start();
}
QUnit.module("A", { setup: doExpensiveSetupForModuleA });
asyncTest("A.1", testA1);
asyncTest("A.2", testA2);
asyncTest("A.3", testA3);
QUnit.module("B", { setup: doExpensiveSetupForModuleB });
asyncTest("B.1", testB1);
asyncTest("B.2", testB2);
asyncTest("B.3", testB3);
This will work independent of the order in which the tests are executed and also independent of the time spent by each method to terminate.
The calls to start() will assure that the test results will be collected only in that point of the method.
More detailed examples can be found in the QUnit Cookbook:
http://qunitjs.com/cookbook/#asynchronous-callbacks
Updated:
if you don't want your expensive methods to be executed before each test method, but actually only once per module, just add control variables to your code to check if the module was already set up:
var moduleAsetUp = false;
var moduleBsetUp = false;
function doExpensiveSetupForModuleA() {
if (!moduleAsetUp) {
console.log("setting up module A");
moduleAsetUp = true;
}
}
...
function doExpensiveSetupForModuleB() {
if (!moduleBsetUp) {
console.log("setting up module B");
moduleBsetUp = true;
}
}
...
In this sample, the output would be:
setting up module A
testA1
testA2
testA3
setting up module B
testB1
testB2
testB3
This way you are using your expensive methods as module setup instead of test method setup.
Unit tests are supposed to be atomic, independent, isolated, and thus the order in which they run shouldn't be relevant.
Qunit doesn't always run tests in the same order, anyway, if you want your tests to run in specific order, you can just tell QUnit to don't reorder them:
QUnit.config.reorder = false;
This way you can ensure that testA will run before testB.