bitovi / funcunit

A functional test suite based on jQuery
https://funcunit.com/
MIT License
569 stars 378 forks source link

Can't do async operations in QUnit2 before/after hooks #234

Open smcclure15 opened 5 years ago

smcclure15 commented 5 years ago

The current implementation of the qunit2 adaptor wraps up the QUnit.test method to cache away the assert object so that it can call it's "async" method. This needs to be generalized to allow such operations in all test context scopes, particularly in the before/beforeEach/after/afterEach module hooks; for example:

    F.attach(QUnit);
    QUnit.module("my mod", {
        before: () => {
            F.wait(1, ()=>{});
        }
    });
    QUnit.test("hello world", (assert) => {
        assert.ok(1); // trivial test
    });

Will throw the following: TypeError: Cannot read property 'async' of undefined

I've tried out the following workaround in the qunit2 adapter (getting the current assert on-demand without caching), and it seems to work well:

    module.exports = function (runner) {
        var done;
        var getCurrentAssert = function () {
            if (runner.config.current) {
                return runner.config.current.assert;
            }
            throw new Error("Outside of test context");
        }
        return {
            pauseTest: function () {
                done = getCurrentAssert().async();
            },
            resumeTest: function () {
                done();
            },
            assertOK: function (assertion, message) {
                getCurrentAssert().ok(assertion, message);
            },
            equiv: function (expected, actual) {
                return runner.equiv(expected, actual);
            }
        };
    };

One gotcha that I'm not sure about is if we need a simple "done" variable, or if that should be a stack to push/pop.