Jasmine vs. Mocha JavaScript testing for Rails 3.1+-ThrowExceptions

Exception or error:

I have experience with Jasmine and do like it quite a bit. Does anyone have experience with both Jasmine and Mocha, specifically for Rails? I am wondering if it’s worth switching to.

How to solve:

I have done testing in both Jasmine and Mocha. First, switching is relatively easy. The basic describe and it BDD pattern is identical. You will need to change how you do your assertions and switch to a different interface for asynchronous tests. Overall they are comparable.

Mocha’s asynchronous interface is much simpler and more consistent. Tests and setup can be either synchronous or asynchronous, which is great. This, plus the fact that TJ Holowaychuck is an epic code poet are good reasons to try Mocha.

I do think the Jasmine matchers are easier to read and more elegant, especially when paired with the jasmine-jquery plugin. Mocha is usually paired with a separate library for assertions, often chai.js if you are doing in-browser testing or should.js for node-only testing. I am happy with chai’s assert.equal() interface, but the Jasmine style expect($("#central_errors").html()).toContain("must provide a name"); seems more elegant to me. I am not a fan of the chai.js expect(42).to.be.above(41) style interface with dot-delimited sentences because it doesn’t work well aurally.

Ultimately, this is a personal preference question and I highly encourage you to just spend a day or so writing Mocha tests instead of Jasmine and see how it feels. Totally a worthwhile investment, even if you decide to stick with Jasmine you will be doing so from a place of first-hand knowledge and have an awareness of other ways to solve some of the problems Jasmine solves. I tried it and I’m sticking with Mocha partly because betting on TJ is a good bet, but Jasmine is also a mature, solid, and widely adopted library.


Check out the Teabag project on github. It specifically allows you to use the asset pipeline along with Mocha, Jasmine, or QUnit.


Mocha’s a pretty great library, but I typically use Jasmine — for me it’s primarily about knowledge and experience with Jasmine, but Mocha has some really cool features — like letting you know when you’re bleeding things into the global scope.

Figured it was worth mentioning the project because it specifically allows you to play around with both (in different suites) so you can decide for yourself.

Leave a Reply

Your email address will not be published. Required fields are marked *