How to test Backbone apps

Testing your Backbone code will let you write better apps, faster. This series will show you how. I’ll cover unit and functional testing, and techniques to test your app without a live backend/API.

Why should I care about testing my Backbone code?

Let me give you the typical pro-testing before and after:

Before

It's Friday at 18.46. Your app isn't working on IE again. You fix and deploy, and realise you've pushed a one character typo. Meeting friends in the pub? Nope.

After

Flash to a cheesy silver bullet comparison:

IE didn't have Array#indexOf but your tests alerted you before deploy. The release was great: no surprise, you already knew it worked from your happily blinking test suites. Desk beer!

Sounds nice! However, silver bullets belong in vampire stories. Every programming practice has costs along with benefits, testing is no exception. So we have an additional story to consider:

It's Friday at 18.46. Your Selenium suite takes 28 minutes to run, and your 96% code coverage unit test suite had to be rewritten due to product changes. You need to release to make users happy, but all your time was spent fixing tests.

Testing is not the goal - releasing a great Backbone app for your users is! I’ll be highlighting techniques to keep your tests from become fragile and costly to maintain.

Series overview

The articles in the series will be:

  1. Testing Backbone views
  2. Testing Backbone models
  3. Asynchronous tests
  4. Creating a mocked server for Backbone apps
  5. Advanced Backbone views - easy testing with view models
  6. Functional testing for Backbone

Experience

This series is drawn from the methods I've found most effective while developing a number of products with Backbone - an analytics dashboard for Skimlinks, numerous real time games for Picklive, a machine learning Twitter client and Chrome extensions.

What will I learn?

By the end of the series you'll be able to:

Schedule

I'll be posting the rest of the articles over the next few weeks, starting this week. Subscribe to the feed or follow me so you don’t miss them as they come out.

If you're in the UK, you might like the class I've run before and will run again in the future at General Assembly London, Building Web Apps with Backbone.js. It'll take you through Backbone.js from the basics, and work through a case study of converting a legacy jQuery app into a single-page Backbone app.

Thanks for reading, please say hi if you've any questions - @timruffles.

Enjoy this? Subscribe to my RSS feed or follow me on Twitter.