Jasmine vs. Mocha JavaScript testing for Rails 3.1+

13,369

Solution 1

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.

Solution 2

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

https://github.com/modeset/teaspoon

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.

Share:
13,369
LupineDev
Author by

LupineDev

Web Developer and Math Geek

Updated on June 14, 2022

Comments

  • LupineDev
    LupineDev about 2 years

    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.

  • LupineDev
    LupineDev over 12 years
    Thanks Peter! Just the kind of observations I was looking for :)
  • Jo Liss
    Jo Liss about 12 years
    Hey Peter, great writeup! Just to add, with Chai, you can get a Jasmine-style ("bdd") interface too: chaijs.com/code/expect.html In fact you can mix-and-match it with the assert ("tdd") interface. Analogous to jasmine-jquery, there's also a chai-jquery plugin, though it's pretty new.
  • Peter Lyons
    Peter Lyons about 12 years
    Yup both chai and mocha have multiple interfaces. As I mention above, I dislike the chai bdd/expect interface because I don't find all those dots helpful.
  • Scott Silvi
    Scott Silvi about 11 years
    +1 for aurally. And a great writeup. And for the protip to try them both and see how they work phalangeally.
  • Anton Rudeshko
    Anton Rudeshko about 10 years
    Is there any changes in your answer because of Jasmine 2.0?
  • vulcan raven
    vulcan raven about 9 years
    With v2+, Jasmine provides mocha-like async interface. Other than that, Jasmine has everything mocha has to offer and much more. (Disclaimer: I have read the usage docs of both and compared and I have used Mocha more than Jasmine in my node.js projects, if you are strict mocha person, you need to give Jasmine a try!)