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.
