bitovi / bitovi/funcunit

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

Open
#234 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
JavaScript
Stars
571
Forks
370
PR merge metrics
No merged PRs in 30d

Description

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.

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.