Thursday, November 7, 2013

mocha done callback stuff

In mocha testing, you may use the "done" callback to test asynchronous code. This may come with some pains as Derick Bailey explains at his blog, but I was able to refactor the last of the two tests I show off here using "done" like so:

describe("when refresh is called twice yet some time apart", function() {
   var firstPromise;
   var secondPromise;
 
   before(function(done) {
      widget = new LogViewer();
      widget.postCreate();
      firstPromise = widget.refresh();
      firstPromise.then(function(){
         secondPromise = widget.refresh();
         done();
      })
   });
 
   it("should procure two separate promises", function() {
      assert.isFalse(firstPromise === secondPromise);
   });
});

 
 

This ensures that the test is really running in Jenkins/Grunt builds and not just making a green checkmark in the HTML test viewer that happens by way of a promise returned outside of anything your CI process might see.

 
 

Addendum 12/7/2018: The done trick is legit in Jasmine tests too.

No comments:

Post a Comment