Search code examples
javascriptnode.jsunit-testingvows

Vows JS Testing for Undefined


I'm trying to use vows js to create unit tests. I am having trouble when the "topic" is `undefined'. Please see the example below:

var vows = require('vows'),
  assert = require('assert');

function giveMeUndefined(){
  return undefined;
}

vows.describe('Test vow').addBatch({
  'When the topic is undefined': {
    topic: function() {
      return giveMeUndefined();
    },
    'should return the default value of undefined.': function(topic) {
      assert.isUndefined(topic);
    }
  }
}).export(module);

This isn't the code exactly, but it's the gist of it. When I run the test I get "callback not fired." Stepping through the code of vows, I can see that it branches off when topic is undefined.

Ultimately I want to know how I can write the unit test to do this. Someone else on my team wrote what I consider a hack and did the assertion in the topic and returned true or false if topic === undefined.


Solution

  • From Vows docs :

    » A topic is either a value or a function which can execute asynchronous code.

    In your example topic is assigned to a function, so vows is expecting asynchronous code.

    Simply rewrite your topic as follow :

    var vows = require('vows'),
      assert = require('assert');
    
    function giveMeUndefined(){
      return undefined;
    }
    
    vows.describe('Test vow').addBatch({
      'When the topic is undefined': {
        topic: giveMeUndefined(),
        'should return the default value of undefined.': function(topic) {
          assert.isUndefined(topic);
        }
      }
    }).export(module);