Testing With Meteor, CoffeeScript and Mocha

Despite no mention in the official Meteor docs, adding unit tests is fairly easy. For some reason, they don’t mention this but any files in the tests/ folder will be ignored by the Meteor server.

What you’ll need:

Optional:
  • Growl (for notifications from mocha watch)
Getting everything running:
1
2
3
meteor create mocha
cd mocha
meteor add coffeescript

Setting up Mocha to watch our coffee files and send Growl notifications:

1
mocha tests/*.coffee -w -G --compilers coffee:coffee-script

Now to create a sample test:

test.coffee
1
2
3
4
5
describe "Array", ->
  describe "#indexOf()", ->
    it "should return -1 when the value is not present", ->
      assert.equal -1, [1, 2, 3].indexOf(1)
      assert.equal -1, [1, 2, 3].indexOf(0)

Mocha should notice the file change and run the tests showing you the passing test.

Source code here.

Some notes:

  • The growl notification didn’t work for me, but I assume that’s because I’m using an old version.
  • When using Mocha watch, sometimes a failing test result would be outputted many times
  • Jasmine should also work fine

Comments