Async Improvement to check if it's completed from external code
Hi, I'd like to propose a change to the QUnit async function that I found useful. Specifically testing if another async function has not completed this could be a cause for issues. Therefore this change proposes a way to check for this by attaching a state variable to the returning function:
- // Increment this Test's semaphore counter, then return a single-use function that
- // decrements that counter a maximum of once.
- async: function() {
- var test = this.test,
- popped = false;
-
- test.semaphore += 1;
- test.usedAsync = true;
- pauseProcessing();
-
- //the point of this change is to check if an async has been triggered from
- //within a unit test; if we find it isn't we can raise an assert failure
- //create our state object within scope; note this needs a wrapper arround the
- //complete; i tried to do without it but couldn't figure it out
- //this might be a better solution anyways
- var state = {
- complete: false,
- calltime: null,
- };
-
- //the orriginal QUnit function; modified
- fn = function () {
- state.complete = true; //added; note the scope
- state.calltime = new Date();
-
- if (!popped) {
- test.semaphore -= 1;
- popped = true;
- resumeProcessing();
- } else {
- test.pushFailure("Called the callback returned from `assert.async` more than once",
- sourceFromStacktrace(2));
- }
- };
-
- //assign our scope variable as a propery as well so it's externally accessable
- fn.state = state;
-
- return fn;
-
- /**
- orriginal
-
- return function done() {
- if ( !popped ) {
- test.semaphore -= 1;
- popped = true;
- resumeProcessing();
- } else {
- test.pushFailure( "Called the callback returned from `assert.async` more than once",
- sourceFromStacktrace( 2 ) );
- }
- };*/
- },
Then you can use it:
- var asyncTest1 = assert.async();
-
- /////// some code /////
-
- if (!asyncTest1.status.complete) {
- assert.ok(false, "async test1 did not complete before this point");
- }