Skip to content

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

@smcclure15

Description

@smcclure15

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions