Mocha - A great test framework for Node.js 2

Posted by Bryan on December 05, 2011

I’ve been looking for a test framework for node.js for a while now that had all the features I was looking for.  Well, the search is over.  Mocha is hands-down the best I’ve used so far.  I like nodeunit, and have even contributed to it a bit, but the things I’ve been contributing are included in mocha.  It’s also quite a bit faster than nodeunit in my very crude benchmarks.

The key features for me:

  • Before all, after all, before each, and after each hooks.
  • Nice reporters (nested, spec style is my favorite — which is what I added to nodeunit).
  • The source code is clean and well-organized.  Nodeunit’s is good too, but I find it hard to follow sometimes.
  • Tests never hang (not sure what I’m doing wrong, but a lot of tests hang for me nodeunit).
  • Async and synchronous test support. I.e., you don’t need to pass in a callback function to individual tests or test hooks unless the test or hook has an async function in it.

It also has support for both “TDD” and “BDD” styles.  The Elders at MOG prefer the TDD syntax, so it’s nice having the ability to use that while reaping the benefits of BDD-like features.  It’s effectively the same exact thing as using the BDD syntax — it’s just using different words (’describe’ vs ’suite’, ‘it’ vs ‘test’).

A couple of drawbacks (or future features hopefully):

  • No support for recursively running all tests in a directory and its subdirectories.  This is solvable with a quick Makefile line though.
  • Can’t intertwine BDD and TDD styles (not without some ugly hacks anyway).  I.e., it would be cool if one guy on the team strongly preferred to use ‘describe’ instead of ’suite’, that Mocha would detect it somehow and let them run side-by-side with the TDD-style tests.

That’s a short list of minor complaints, folks.  I highly recommend trying it.

P.s. I’ve only barely used Jasmine, but it seems like there was some limitation (at the time anyway) that made it difficult to use with nodejs.  Could be totally wrong though.  A lot of people like Vows as well, but I had a hard time getting into that syntax, and it sure as hell wouldn’t fly with The Elders.

Trackbacks

Use this link to trackback from your own site.

Comments

Leave a response

  1. Rob Levin Sat, 28 Jan 2012 06:38:06 PST

    OMG, I know who you’re referring to when you say the Elders .. hehe. Funny Bryan, I ended up on your blog on a google search for mocha vs jasmine .. nice SEO! Anyway, I’ve been using jasmine for a while and it’s ok but I’m thinking of trying out mocha which seems to have a somewhat interchangeable syntax so my old tests should still work.

    I don’t really have a reason to switch from jasmine to mocha but I’m keen to give it a whirl. I ended up using expresso yesterday since traversal, an pretty neat npm, has a readme that suggests using it. But it ain’t got no before/beforeEach, etc. as far as I can see so forget it.

    Anyway, I’m going to add you to my reader so keep up the writing Bryan and don’t mess with them Elders!

  2. Bryan Sat, 28 Jan 2012 08:19:11 PST

    Hey Rob, yeah, I didn’t get very far with expresso for the same reasons. Expresso does have code coverage, though, which would be nice. It will be in mocha one of these days though.

    Also check out buster.js (http://busterjs.org/). Looks like it’s very good.

Comments