Last updated

Vows and CoffeeScript

CoffeeScript is a really nice way to write JavaScript code. Combined with NodeJS you are empowered by a very fast platform to develop server-side applications. Of course, you want to test these apps as well. Vows is really great way to do this. Unfortunately it’s not straight forward (yet) to set up NodeJS + CoffeeScript + Vows.

~ First off, make sure you have CoffeeScript and Vows installed. Here I install them globally so you can use the coffee and vows command line utilities.

1npm install -g coffee-script
2npm install -g vows

Next up, in your product directory, create a directory named test. Here we’ll create (classic) example: division-test.coffee

 1vows = require 'vows'
 2assert = require 'assert'
 3
 4vows
 5  .describe('Division by zero')
 6  .addBatch
 7    'when dividing a number by zero':
 8      topic: -> 42/ 0
 9
10      'we get Infinity': (topic) ->
11        assert.equal topic, Infinity
12
13    'but when dividing zero by zero':
14      topic: -> 0 / 0
15
16      'we get a value which':
17        'is not a number': (topic) ->
18          assert.isNaN topic
19
20        'is not equal to itself': (topic) ->
21          assert.notEqual topic, topic
22
23  .export(module)

I’m not going to explain the intimate details of Vows here, suffice it to say that you calculate a value and store it in topic. Then you perform a set of expectations.

The magic part is the last line, .export(module). In all other examples you’ll see the last command is .run(). This run() command runs your vows immediately.

When using coffee-script, you don’t want to directly run you vows, but gather them all together, convert them to JavaScript and then have vows run them.

With the test/division-test.coffee saved, try running vows from your console. Here’s the output from vows --spec:

 1vows --spec
 2♢ Division by zero
 3
 4  when dividing a number by zero
 5    ✓ we get Infinity
 6  but when dividing zero by zero we get a value which
 7    ✓ is not a number
 8    ✓ is not equal to itself
 9
10✓ OK » 3 honored (0.002s)

Another great tip is vows -w. This will keep vows running and monitor your test files for changes. When a file changes, it will re-run your vows for you.

Happy testing!

Yes, I know there are issues with Coderay parsing UTF-8 characters.