Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
smcclure15 opened this issue Feb 8, 2019 · 0 comments
Open

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

smcclure15 opened this issue Feb 8, 2019 · 0 comments

Comments

@smcclure15
Copy link

smcclure15 commented Feb 8, 2019

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant