George V. Reilly

Review: Backbone.js Testing

Backbone.js Testing
Title: Backbone.js Testing
Author: Ryan Roemer
Rating: ★ ★ ★ ★ ½
Publisher: Packt
Copyright: 2013
Pages: 168
Keywords: pro­gram­ming, testing, javascript, backbone, mocha, chai, sinon
Reading period: October 2013

Backbone.js Testing is a short, dense in­tro­duc­tion to testing JavaScript ap­pli­ca­tions with three testing libraries, Mocha, Chai, and Sinon.JS. Although the author uses a sample ap­pli­ca­tion 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 test­ing—no­tably, the DOM, 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 col­lec­tion­s; dealing with asyn­chrony; provides useful testing heuristics, including isolating components to reduce de­pen­den­cies; 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-dri­ven_de­vel­op­ment or Behavior-dri­ven_de­vel­op­ment, which provide the in­tel­lec­tu­al foun­da­tions of much of the book. Nor does he have much to say about testa­bil­i­ty 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 ar­chi­tec­ture of Backbone); other people’s 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 ex­plo­ration.

I learned a lot from this book and I have no hesitation in rec­om­mend­ing it.

Disclosure: Thanks to Ryan Roemer and Packt for a review copy of this book.

blog comments powered by Disqus
Adapting Penelope, part II » « Turn off Windows Defender on your builds