Vows and CoffeeScript
10 Jun, 2011

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.

npm install -g coffee-script
npm install -g vows

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

vows = require 'vows'
assert = require 'assert'

vows
.describe('Division by zero')
'when dividing a number by zero':
topic: -> 42/ 0

'we get Infinity': (topic) ->
assert.equal topic, Infinity

'but when dividing zero by zero':
topic: -> 0 / 0

'we get a value which':
'is not a number': (topic) ->
assert.isNaN topic

'is not equal to itself': (topic) ->
assert.notEqual topic, topic

.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:

vows --spec
♢ Division by zero

when dividing a number by zero
✓ we get Infinity
but when dividing zero by zero we get a value which
✓ is not a number
✓ is not equal to itself

✓ 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.