Jasmine vs. Mocha JavaScript for Rails 3.1+

I have experience with Jasmine and I like it quite a bit. Does anyone have experience with Jasmine and Mocha, especially for Rails? I wonder if it's worth switching to it.

+73
javascript ruby-on-rails jasmine mocha
Mar 14 2018-12-12T00:
source share
2 answers

I tested both jasmine and mocha. Firstly, switching is relatively simple. The basic describe and it BDD template is identical. You will need to change how you make your statements and switch to a different interface for asynchronous tests. In general, they are comparable.

Mocha's asynchronous interface is much simpler and more consistent. Tests and settings can be either synchronous or asynchronous, which is great. This, plus the fact that TJ Holowaychuck is a poet of the epic, is a good reason to try Mocha.

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

Ultimately, it's a matter of personal preference, and I strongly recommend that you just spend a day or so writing Mocha tests instead of Jasmine and see how it feels. Totally profitable investments, even if you decide to stick to Jasmine, you will do it right away and learn about other ways to solve some of the problems that Jasmine solves. I tried this, and I stick to Mocha partly because betting on TJ is a good bet, but Jasmine is also a mature, solid and widely accepted library.

+119
Mar 19 '12 at 5:50
source share

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

https://github.com/modeset/teaspoon

Mocha is a fairly large library, but I usually use Jasmine - for me it is primarily about knowledge and experience with Jasmine, but Mocha has some really interesting features - for example, letting you know when you bleed into the global realm.

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

+4
Dec 27 '12 at 8:41
source share



All Articles