I realized that the QUnit.module
provides setup and teardown callbacks surrounding each tests.
QUnit.module("unrelated test", {
setup: function() {
var usedAcrossTests = "hello";
}
});
QUnit.test("some test", function(assert) {
assert.deepEqual(usedAcrossTests, "hello", "uh oh");
});
QUnit.test("another test", function(assert) {
assert.deepEqual(usedAcrossTests.length, 5, "uh oh");
});
As seen in setup
, I want to declare a variable to use across the following QUnit.test
s. However, since the variable only has function scope, the two tests fail, saying usedAcrossTests is undefined
.
I could remove the var
declaration, but then that would pollute the global scope. Especially if I will have multiple modules, I'd rather not be declaring test-specific variables as global.
Is there a way to specify, in setup
a variable to be used in the tests within the module, without polluting the global scope?