If you already have CoffeeScript installed (you do, don't you!? if not get it now), giving Mocha a try couldn't be easier. The clean, elegant look of nested callback functions in CoffeeScript, combined with Holowaychuk's awesome Should assertions, make CoffeeScript + Mocha the best-looking implementation of BDD that I have ever seen. Now, good looks aren't everything, but if they make you want to write a lot more tests, they can't be bad. (A word of caution: While Mocha works beautifully in the browser, Should does not, so for browser tests you'll need to use an alternate assertion library, such as Expect. Update: Chai provides an implementation of should assertions that works in the browser.)
Enough talk, let's look at a simple Mocha test in the BDD style.
Here is how to run the example:
describe 'Array', -> describe '#indexOf()', -> it 'should return -1 when not present', -> [1,2,3].indexOf(4).should.equal -1
- Install Mocha (globally for convenience):
$ sudo npm install -g mocha should
- Copy the code above to
$ mocha array-test.coffee --require should
Coming soon on CoffeeScript Love: How to integrate Mocha browser tests with the Rails (>= 3.1) Asset Pipeline. Update: Read Testing CoffeeScript in Rails.