JavaScript Unit Tests: Jasmine vs Mocha

Someone recently asked me whether I prefer Jasmine or Mocha for unit testing JavaScript. My answer is:

Jasmine and Mocha are both great. I use both, depending on the project and team. There’s a great community around both, and you’ll find everything you need for either.

Jasmine

Jasmine is easier to get started – it’s all-in-one package will generally get you and a team up and testing much faster, and you’ll be in good hands with it.

Mocha

Mocha is significantly more flexible, but you have to piece it together yourself.

There is no spy framework built in to Mocha, so most people use sinon.js. There’s no assertion framework built in to Mocha either, so you’ll have to pick one. I like Chai for that, but there are many, many others available. You can also configure Mocha for BDD (jasmine style) or TDD (qunit style) easily. But you have to pick and choose how you want Mocha to work.

This flexibility is great because you can build the test environment that you really want. But it means you have more work to do, more individual pieces to maintain / keep up to date, etc.


Post Footer automatically generated by Add Post Footer Plugin for wordpress.

About Derick Bailey

Derick Bailey is an entrepreneur, problem solver (and creator? :P ), software developer, screecaster, writer, blogger, speaker and technology leader in central Texas (north of Austin). He runs SignalLeaf.com - the amazingly awesome podcast audio hosting service that everyone should be using, and WatchMeCode.net where he throws down the JavaScript gauntlets to get you up to speed. He has been a professional software developer since the late 90's, and has been writing code since the late 80's. Find me on twitter: @derickbailey, @mutedsolutions, @backbonejsclass Find me on the web: SignalLeaf, WatchMeCode, Kendo UI blog, MarionetteJS, My Github profile, On Google+.
This entry was posted in ChaiJS, Jasmine, Javascript, MochaJS, Testing, Unit Testing. Bookmark the permalink. Follow any comments here with the RSS feed for this post.
  • http://www.aidanfeldman.com Aidan Feldman

    The output format options are quite different…

  • http://twitter.com/yoerids Yoeri Deschrijver

    Im looking for a javascript unittest framework that can be integrated into MsBuild. Any suggestions?

    • WickyNilliams

      I’d love to know the answer to this also. I usually use grunt as my build system for JS projects, so i’m thinking you could probably install node on your build server and get it to run grunt (where grunt, amongst other things, then runs my tests headlessly in phantomJS).

      When i get time i am going to investgiate this, would be great to find out someone had done the work already though :)

      • http://twitter.com/kadukf Frantisek Kaduk

        you can use Chutzpah for it.

        • WickyNilliams

          The internet: The only place where you can ask an open question and get a helpful answer from a stranger 5 months later :)

          Just what I was looking for, thanks very much

  • rdamborsky

    Nice summary. Thanks

  • David

    What do you use for Integration/acceptance testing your Marionette apps? Do you have a recommendation for a pure JS solution that can be run in-browser? Could Jasmine not be extended to be used in this way as rspec is used with Capybara?
    Thanks