Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jasmine vs. Mocha JavaScript testing for Rails 3.1+ [closed]

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.

like image 888
LupineDev Avatar asked Mar 14 '12 21:03

LupineDev


People also ask

What is the difference between mocha and Jasmine framework?

Jasmine was created around 2008. The documentation describes Jasmine as “batteries included,” meaning that it attempts to provide everything a developer needs in a test framework. Mocha is younger than Jasmine, created around 2011. Mocha is not a “complete” test framework, and doesn't attempt to be.

Which is better jasmine or jest?

The test finished much faster than Jest, like because there were no variables to handle and no additions to be made. Jasmine is a powerful Javascript testing framework, especially when paired with Angular. Its testing language is a bit more intricate than Jest, which is why it's also great for backend testing.

Is jest same as Jasmine?

Jest is a testing platform built on Jasmine, which originated from Facebook. It offers a selection of advanced features that makes testing just a little bit easier. Jasmine provides a clean and simple API for end-to-end JavaScript testing with Node. js or in the browser.


2 Answers

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.

like image 175
Peter Lyons Avatar answered Sep 29 '22 14:09

Peter Lyons


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.

like image 23
jejacks0n Avatar answered Sep 29 '22 12:09

jejacks0n