Review: Backbone.js Testing
- by george_v_reilly
Title: Backbone.js Testing
Author: Ryan Roemer
Rating: $stars(4.5)
Publisher: Packt
Copyright: 2013
ISBN: 178216524X
Pages: 168
Keywords: programming, testing, javascript, backbone, mocha, chai, sinon
Reading period: October 2013
Backbone.js Testing is a short, dense introduction
to testing JavaScript applications with three testing libraries,
Mocha, Chai, and Sinon.JS.
Although the author uses a sample application
of a personal note manager written with Backbone.js
throughout the book, much of the material
would apply to any JavaScript client or server framework.
Mocha is a test framework that can be executed in the browser or by Node.js,
which runs your tests.
Chai is a framework-agnostic TDD/BDD assertion library.
Sinon.JS provides standalone test spies, stubs and mocks for JavaScript.
They complement each other and the author does a good job of explaining
when and how to use each.
I've written a lot of tests in Python (unittest and mock, primarily)
and C# (NUnit), but my experience with JavaScript unit testing
was both limited and years out of date.
The JavaScript ecosystem continues to evolve rapidly,
with new browser frameworks and Node packages springing up everywhere.
JavaScript has some particular challenges in testing—notably, asynchrony and callbacks.
Mocha, Chai, and Sinon meet those challenges, though they can't take away all the pain.
The author describes how to test Backbone models, views, and collections;
dealing with asynchrony;
provides useful testing heuristics, including isolating components to reduce dependencies;
when to use stubs and mocks and fake servers;
and test automation with PhantomJS.
He does not, however, teach you Backbone.js itself; for that, you'll need another book.
There are a few areas which I thought were dealt with too lightly.
There's no real discussion of Test-driven_development or
Behavior-driven_development,
which provide the intellectual foundations of much of the book.
Nor does he have much to say about testability and how to make
legacy code more testable.
The sample Notes app has plenty of testing seams
(much of this falls naturally out of the architecture of Backbone);
other apps are not so lucky.
The chapter on automation is extremely terse—it could be expanded into a very large book!—but it does provide useful indicators to many areas for exploration.
I learned a lot from this book and I have no hesitation in recommending it.
Disclosure: Thanks to Ryan Roemer and Packt for a review copy of this book.