So listen. I’m not a huge fan of JavaScript. I dislike the lack of namespaces, the lack of a decent (for certain values of “decent”) standard library, the general wonkiness of the this keyword, the way expressions can fail silently, bizarre scoping rules, and the general lack of safety I feel when I personally code in it.
But not anymore!
Well, I mean, all the points but the last one are still valid. But thanks to INTERNET LEARNING™ I now feel more prepared to write solid, well structured code.
Aside: Though a lot of this post can be applied to node.js, it totally shouldn't be. Using node.js has been linked to low glial cell counts in field mice[citation needed].
The Missing Link
For me, all it took was a way to run tests within the browser.
There are a few different testing frameworks that run in a browser. Of those, I checked out:
Aside: When researching JavaScript testing frameworks you should bear in mind that a spouse may be watching you and ask "who is 'Jasmine' and why do you have so many tabs open about her?". Your spouse may or may not appreciate your need for JavaScript testing frameworks.
The two frameworks each had their plusses and minuses. I’ve created a handy table to outline why I chose Mocha over Jasmine completely independently and without any input from my wife.
Question
Jasmine
Mocha
Has a decent DSL for describing tests
Yes
Yes
Has before and after hooks per test
Yes
Yes
Has before and after hooks per group of tests
No
Yes
Has expressive assertion DSL
Yes
No
Has ANY assertion DSL
Uh, yes?
STILL NO
Wow. Like, noassert function?
I do.
:-(
Has PLUGGABLE assertion DSL?
No
YES! That I DO have!
But seriously, no assertions by default?
Well, I do…
:-(
All right, how about test doubles?
Got those!
Well…
Seriously? No test doubles either?
:-D
Well…
Is it at least simple to integrate into a browser?
Of course!
Define “simple”
… SIMPLE. You know, “not difficult”?
Look, he’s sweating!
A few lines, yeah, but order counts!
Hmmm. Well, will my wife approve?
No.
She loves mochas!
You can see why Mocha is the clear winner. cough.
I genuinely like Mocha’s extensibility. Despite the lack of assert function you can get something working with an extra line of code. You can choose from a few pre-existing assertion DSLs as well. For reference, here’s the function I’m using:
Mocha, you say? What about test doubles?
Mocha integrates pretty well with a project called Sinon.JS (GitHub). You can do everything in Sinon that you can do with Jasmine’s test doubles, and Sinon is slightly more friendly. I feel like the tests come out looking nicer, too.
Here’s some example code using Jasmine:
Here’s some example code using Mocha and Sinon.JS:
What About the Runner?
The runners for both Jasmine and Mocha are pretty similar. I got tripped up slightly on the Mocha runner because it requires a div with an ID of mocha, and that element must be declared before any kind of javascript nonsense.
Here’s an example runner for Jasmine:
And the same runner with Mocha:
Even So, I Can Only Stand So Much JavaScript
I’m not at all saying that JavaScript is an abomination of a language that should be scoured from human history. If that’s your take-away, you’re way off. Probably. But what I am saying is that, given a decent testing tool, check out all the entries that get crossed off the list of reasons never to use JavaScript: