Thursday, July 1st, 2010

Keep your vows; Keeping wed to Node

Category: Node, Testing

<p>Vows can be a beautiful thing. Alexis Sellier of LESS fame, is becoming an open source star. This time around he brings us Vows an asynchronous-friendly behavior driven development framework for Node.js.

Write you BBD specs like this:

javascript
< view plain text >
  1. // division-by-zero-test.js
  2.  
  3. var vows = require('vows'),
  4.     assert = require('assert');
  5.  
  6. // Create a Test Suite
  7. vows.describe('Division by Zero').addBatch({
  8.     'when dividing a number by zero': {
  9.         topic: function () { return 42 / 0 },
  10.  
  11.         'we get Infinity': function (topic) {
  12.             assert.equal (topic, Infinity);
  13.         }
  14.     },
  15.     'but when dividing zero by zero': {
  16.         topic: function () { return 0 / 0 },
  17.  
  18.         'we get a value which': {
  19.             'is not a number': function (topic) {
  20.                 assert.isNaN (topic);
  21.             },
  22.             'is not equal to itself': function (topic) {
  23.                 assert.notEqual (topic, topic);
  24.             }
  25.         }
  26.     }
  27. }).run(); // Run it

and you get a very nice report card out of the other end:

With macros you end up with very nice DSL syntax such as:

javascript
< view plain text >
  1. {   'GET /': {
  2.         topic: api.get('/'),
  3.         'shoud respond with a 200 OK': assertStatus(200)
  4.     },
  5.     'POST /': {
  6.         topic: api.post('/'),
  7.         'shoud respond with a 405 Method not allowed': assertStatus(405)
  8.     },
  9.     'GET /resources (no api-key)': {
  10.         topic: api.get('/resources'),
  11.         'shoud respond with a 403 Forbidden': assertStatus(403)
  12.     },
  13.     'GET /resources?apikey=af816e859c249fe'
  14.         topic: api.get('/resources?apikey=af816e859c249fe'),
  15.         'shoud return a 200 OK': assertStatus(200),
  16.         'should return a list of resources': function (res) {
  17.             assert.isArray (res.body);
  18.         }
  19.     }
  20. }
  21.  
  22. // or even
  23. {
  24.   'GET  /':                   respondsWith(200),
  25.   'POST /':                   respondsWith(405),
  26.   'GET  /resources (no key)': respondsWith(403)
  27. }

The website itself goes into exquisite detail on the install process, sample usage, and more. The website also happens to be beautiful itself, and full of HTML5 markup to boot.

Related Content:

Posted by Dion Almaer at 6:14 am
1 Comment

+++--
3.3 rating from 3 votes

1 Comment »

Comments feed TrackBack URI

Sexy site and sexy-looking tool :) Javascript is an awesome language.

Comment by Skilldrick — July 6, 2010

Leave a comment

You must be logged in to post a comment.