Qunit beforeEach, afterEach - asynchronous

Since start (), stop () will be removed in Qunit 2.0, what is the alternative for asynchronous settings and breaks via beforeEach, afterEach methods? For example, what if I want beforeEach to wait for the promise to complete?

+3


source to share


3 answers


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)

+7


source


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.

0


source


Ember Qunit, which once existed beforeEach

/ setup

, afterEach

/ teardown

does not coexist for long.

See PR: https://github.com/emberjs/ember-qunit/pull/125

0


source







All Articles