Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Vows JS Testing for Undefined

Vows JS Testing for Undefined

Problem

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.

Problem courtesy of: arb

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);
Solution courtesy of: jcreignou

Discussion

View additional discussion.



This post first appeared on Node.js Recipes, please read the originial post: here

Share the post

Vows JS Testing for Undefined

×

Subscribe to Node.js Recipes

Get updates delivered right to your inbox!

Thank you for your subscription

×