Book Review: Backbone.js Testing by Ryan Roemer
The book itself talks a bit about the generalities of testing: how to test, what to test, and what makes a good test. It introduces a simple app which is centered around taking notes in markdown and rendering them as HTML. It is similar to the TodoMVC app, but is a bit simpler and more focused.
The middle chapters review the usage and APIs of 3 testing tools: Chai, an assertion library, Mocha, a test runner, and Sinon.JS, a library for test doubles. This is the testing stack that we use at Sprint.ly, so I was familiar with the topics covered. Each of these chapters was a solid overview of each. They contained all of the major features that I use day-to-day, while also introducing a few new ones that I wasn't aware of.
The last chapter is short, but covers setting up PhantomJS, a headless web browser, to run your browser-based tests in an automated fashion. It also discusses the value of continuous integration and automated testing.
Things I liked
The best thing about this book is its focus. Clocking in at ~140 pages, I was able to read it in 3 sessions over the course of a lazy Saturday. Ryan does a great job out outlining the bite-sized chunks for each chapter, explains them well, and doesn't linger unnecessarily long.
I think the example chosen for this book was also really good. The app was small enough that it fits logically into this small book. It didn't take too much effort to explain well and it was a knowable problem set. I think beyond this, the app seems like it accomplishes something that someone could actually use, which is a nice side effect.
I enjoyed the clear explanation of tests doubles that he gave as well. One of the things I really like about vocabulary lessons is that it offers dimensionality and specificity to conversations about concepts that would be otherwise a little murky to talk about. Until I read his explanation, the difference between test spies, mocks or stubs was a little fuzzy. Now the difference is crystal clear.
Things I didn't like
The first thing I didn't like about the book was that it wasn't clear
which things were being tested because it helped illustrate the
underlying concept the author was trying to communicate or it was
being tested because the author thought it was a great idea. One of
the major examples of this was testing Backbone model constructors and
the behavior of the
set method. This should all have existing tests
in Backbone's own testing, so I don't feel it's worth testing that in
your own code.
In a similar vein, there's a section later in the book where the
author is attempting to outline edge-case issues with Sinon's
library and how it can't mock some callbacks. You can get around this
issue by wrapping them in an anonymous function, but it wasn't clear
if that was a preferred solution or not.
Is it worth it?
At the end of the book, you'll understand how to test Backbone.js apps, which is exactly what the book aims to do. The downsides to this book are minor and I'll be recommending everyone at work is able to read a copy. If you find yourself writing Backbone apps, this will get you up and running with a solid testing infrastructure for what amounts to a day worth of reading. This is well worth the investment!