Async Improvement to check if it's completed from external code

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:

  1. // Increment this Test's semaphore counter, then return a single-use function that
  2. // decrements that counter a maximum of once.
  3. async: function() {
  4. var test = this.test,
  5. popped = false;

  6. test.semaphore += 1;
  7. test.usedAsync = true;
  8. pauseProcessing();
  9.         
  10.        //the point of this change is to check if an async has been triggered from
  11.                //within a unit test; if we find it isn't we can raise an assert failure
  12.        //create our state object within scope; note this needs a wrapper arround the
  13.        //complete; i tried to do without it but couldn't figure it out
  14.                //this might be a better solution anyways
  15. var state = {
  16.     complete: false,
  17.                     calltime: null,
  18. };

  19.                 //the orriginal QUnit function; modified
  20. fn = function () {
  21.     state.complete = true; //added; note the scope
  22.     state.calltime = new Date();

  23.     if (!popped) {
  24.         test.semaphore -= 1;
  25.         popped = true;
  26.         resumeProcessing();
  27.     } else {
  28.         test.pushFailure("Called the callback returned from `assert.async` more than once",
  29.                     sourceFromStacktrace(2));
  30.     }
  31. };

  32.                 //assign our scope variable as a propery as well so it's externally accessable
  33. fn.state = state;

  34. return fn;

  35.     /**
  36.         orriginal

  37. return function done() {
  38. if ( !popped ) {
  39. test.semaphore -= 1;
  40. popped = true;
  41. resumeProcessing();
  42. } else {
  43. test.pushFailure( "Called the callback returned from `assert.async` more than once",
  44. sourceFromStacktrace( 2 ) );
  45. }
  46. };*/
  47. },

Then you can use it:

  1. var asyncTest1 = assert.async();

  2. /////// some code /////

  3. if (!asyncTest1.status.complete) {
  4.      assert.ok(false, "async test1 did not complete before this point");
  5. }