If you dread the business of testing JavaScript and Backbone.js web applications, this book is your guardian angel. In easy stages and lots of examples it takes you through the whole testing situation, introducing you to best practice and current techniques. Overview In Detail Frontend web applications are soaring in popularity and the Backbone.js library is leading this charge with a modular, lightweight approach for organizing JavaScript web applications. At the same time, testing client-side JavaScript and Backbone.js programs remains a difficult and tedious undertaking. Backbone.js Testing brings sensible practices and current techniques to the challenges of Backbone.js test development. The book introduces fundamental testing concepts, comprehensive test infrastructure design, and practical exercises to easily and systematically test modern JavaScript web applications. The book progresses from Mocha test suites and Chai assertions to advanced test mocks and stubs with Sinon.JS. The requisite libraries and utilities are introduced with in-depth examples and best practices for integration with your applications. The book guides you through the test planning and implementation processes for your application models, views, routers, and other Backbone.js components. Backbone.js Testing gives you the tools, examples, and assistance to test your Backbone.js web applications thoroughly, quickly, and with confidence. What you will learn from this book Approach This book is packed with step-by-step tutorials and instructions in recipe format to help you create test infrastructures and gradually advance your Backbone.js application development and testing skills. Who this book is written for If you are a JavaScript developer looking to create and implement test support for your Backbone.js applications, then this book is ideal for you.
Don't be fooled by the title! Even if it says "Backbone.js testing", I recommend this book to any JavaScript developer that wants to get serious in the testing world. Backbone is just an excuse to teach all these techniques to the reader but they can't be applied to any JS library (if you are using any!).
I've been struggling myself to find good documentation about different strategies and libraries to better test my JavaScript projects and Ryan's book is one of the best and most concise resources I found so far. What I liked the most is not only that it goes directly to the point, but it is also filled by numerous references to other books and articles if the reader wants to dive deeper into the subject.
After reading this book you should expect to have increased several levels your testing skills and knowledge. You will learn how to make Mocha, Chai and Sinon.js dance together and how you can save time and increase the confidence in your releases with tools like Selenium and PhantomJS. However, even if the language is clear, someone that has not “suffered enough” regression, integration or any kind of bugs, will probably not enjoy this book as much as it should be enjoyed.
I would recommend this book to any developer that has felt the pain of (not) testing in JavaScript and wants to improve his skills (or just acquire them!).
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, 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 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 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 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.
What stands out in this book is the easy to understand writing and examples. I would say this is probably the best book for 101 level people even if you have never done any Javascript testing in any framework.
I like the fact that the author covered all three libraries: Mocha, Chai and Sinon. This is most relevant when you just started learning Backbone and still undecided on which one to go for or all of three. You will see examples for each and walked through a real test with the Notes application in the book.
One thing I like to see more is the talk about headless browser automation testing using Phantom.js. The book didn't mention Casper.js which is a very nice utility to use with Phantom.js, saving you a lot of time on manipulating the Phantom.js browser object and get data.