Qunit beforeEach, afterEach - asynchronous
QUnit basically wants people to stop using global methods (not only start()
and stop()
, but also test()
, expect()
etc.), so as of 1.16.0 you should always use the global namespace ( QUnit
) or the API argument assert
passed to functions test()
. This includes new asynchronous controls :
QUnit.test( "testing async action", function( assert ) { // <-- note the `assert` argument here
var done = assert.async(); // tell QUnit we're doing async actions and
// hold onto the function it returns for later
setTimeout(function() { // do some async stuff
assert.ok( true, "This happened 100 ms later!" );
done(); // using the function returned from `assert.async()` we
// tell QUnit we're don with async actions
}, 100);
});
If you are familiar with the old ways start()
and stop()
, you should see that it is very similar, but more decoupled and extensible.
Since the method call async()
is in an argument assert
in the test, it cannot be used in a function beforeEach()
. If you have an example of how you've done this before, please post it and we can try to figure out how to do it differently with git.
UPDATE
My mistake earlier, the object assert
is passed in callbacks beforeEach
and afterEach
on modules, so you should be able to do the same logic as for the test:
QUnit.module('set of tests', {
beforeEach: function(assert) {
var done = assert.async();
doSomethingAsync(function() {
done(); // tell QUnit you're good to go.
});
}
});
(tested in QUnit 1.17.1)
source to share
Seeing that nobody answered the beforeEach / afterEach part: The test suite is supposed to run right after the page loads. If this is not immediately possible, resort to setting QUnit:
QUnit.config.autostart = false;
and keep setting up your test suite (initializing tests, uploading them to QUnit, asynchronously waiting for some components to load, be it AJAX or whatever), your site and finally when ready, just run:
QUnit.start();
The QUnit docsite covers.
source to share