devroom.io/content/posts/2011-06-10-vows-and-coffeescript.md

91 lines
2.5 KiB
Markdown
Raw Normal View History

2017-03-20 15:35:19 +00:00
+++
date = "2011-06-10"
title = "Vows and CoffeeScript"
tags = ["javascript", "bdd", "nodejs", "coffee-script", "vows", "v8"]
slug = "vows-and-coffeescript"
+++
2015-03-26 11:28:08 +00:00
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][1] is really
great way to do this. Unfortunately it's not straight forward (yet) to
set up NodeJS + CoffeeScript + Vows.
[1]: http://vowsjs.org
~
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.
2017-03-20 15:35:19 +00:00
``` shell
npm install -g coffee-script
npm install -g vows
```
2015-03-26 11:28:08 +00:00
Next up, in your product directory, create a directory named `test`.
Here we'll create (classic) example: `division-test.coffee`
2017-03-20 15:35:19 +00:00
``` coffee
vows = require 'vows'
assert = require 'assert'
2015-03-26 11:28:08 +00:00
2017-03-20 15:35:19 +00:00
vows
.describe('Division by zero')
.addBatch
'when dividing a number by zero':
topic: -> 42/ 0
2015-03-26 11:28:08 +00:00
2017-03-20 15:35:19 +00:00
'we get Infinity': (topic) ->
assert.equal topic, Infinity
2015-03-26 11:28:08 +00:00
2017-03-20 15:35:19 +00:00
'but when dividing zero by zero':
topic: -> 0 / 0
2015-03-26 11:28:08 +00:00
2017-03-20 15:35:19 +00:00
'we get a value which':
'is not a number': (topic) ->
assert.isNaN topic
2015-03-26 11:28:08 +00:00
2017-03-20 15:35:19 +00:00
'is not equal to itself': (topic) ->
assert.notEqual topic, topic
2015-03-26 11:28:08 +00:00
2017-03-20 15:35:19 +00:00
.export(module)
```
2015-03-26 11:28:08 +00:00
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`:
2017-03-20 15:35:19 +00:00
``` shell
vows --spec
♢ Division by zero
2015-03-26 11:28:08 +00:00
2017-03-20 15:35:19 +00:00
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
2015-03-26 11:28:08 +00:00
2017-03-20 15:35:19 +00:00
✓ OK » 3 honored (0.002s)
```
2015-03-26 11:28:08 +00:00
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._
2017-03-20 15:35:19 +00:00