node.jsbddvows

In Vows.js how do you revert back to the original topic after going through an asynchronous callback?


Say I have the following sequence:

vows.describe('Example').addBatch({
   'An example' : {
      topic: new Example(),
      'with an async method' : function(example) {
         example.asyncMethod(this.callback);
      },
      'will do some magic' : function(err, result) {
         assert.equal(result.magical, true);
      },
      'and then we will be back on track' : function(example) {
         assert.isTrue(example.backOnTrack); 
      }
   }
}).run();

Is the test "and then we will be back on track" possible to hit with the topic (Example) from before?

EDIT

vows.describe('Example').addBatch({
   'An example' : {
      topic: function(example){ // <- where example is a parent topic
         example.asyncMethod(this.callback);
      },    
      'callback after an async method will do some magic' : function(err, result) {
         assert.equal(result.magical, true);
      },
      'and then we will be back on track' : function(example) {
         assert.isTrue(example.backOnTrack); 
      }
   }
}).run();

Solution

  • A topic returns a topic that will be send to all your vows.

    In that first vow "with an async method" this.callback is undefined because a callback is only defined in a topic. In the second vow the arguments are example not err, result

    If you want to do any async testing you need to set a function for your topic and use this.callback or return an EventEmitter from the topic.

    Edit:

    The issue you have is returning multiple topics from vows. This is not possible.

    The easiest solution is to return a new topic that is an array of two topics. This feels hackish.

    The better solution is to make sure that you test your topics in isolation. Basically your testing that "side-effects" have successfully occurred. Side effects should never occur