Swizec Teller - a geek with a hatswizec.com

    Testing Backbone apps with Mocha

    A test bed

    A while ago I decided it was finally time to bite the bullet and learn frontend testing. Things were getting out of hand with a project I'm working on (melt.ee) and I could no longer keep a good development pace while ensuring a good level of quality.

    You know the drill, it's just a three page app. Then corner cases in UX start cropping up and suddenly it's a 7 page app whose state machine covers a conference room whiteboard. Suddenly your approach produces three bugs for every one you fix and the thought of adding features gives you nightmares.

    Time for a refactor!

    And a brilliant opportunity to introduce some tests.

    Mocha frontend testing

    Looking far and wide for a nice tool to use I finally settled on Mocha because it's the same set of tools I use for testing node.js servers. Selenium still feels kind of weird to me and although Casper.js looks awesome I had a hard time figuring out how to begin using it, plus it didn't feel like the perfect fit.

    Mocha is a pure javascript test runner that simply lets you run some javascript code. That's it. Because I was testing a Backbone.js app it was the best fit - all I really have to do is run some of my production code and check for side-effects.

    I'm running my tests in the browser because for the life of me I couldn't get mocha-phantomjs to run the tests even though they seem to work fine. No output at all.

    Most of my tests merely check that views are rendering correctly so they look something like this:

    describe("Events", function () {
    beforeEach(function () {
    App.state.clear({ silent: true });
    it("renders go button", function () {
    App.state.set({ page: "events" });
    var $go = $("div#view a.go:visible");

    As you can see, my testing harness has a _div#view _where things render, then I tell the App to go into a certain state and check the correct view has rendered.

    This is where things get tricky and I have yet to fully figure them out.

    I want to test the actions the App takes. Does clicking a button perform the right actions? Does it react correctly to changes in the url? Stuff like that.

    Problem is, triggering those actions would make the browser navigate away from the test bed and that's not what I want. There's already enough issues with the App reacting to some events by pushing url changes to the browser so I can't just blindly refresh the test page.

    Another interesting source of problems is that in some states the App must perform a redirect - sometimes you want to run the tests, but are greeted with a "Oh hey, let me just redirect that to X!"

    I hear Casper.js is a better fit for making full navigation tests, so that's the next thing I'll be looking into.

    Despite the failings of my approach, frontend testing has already been a great help with this project. No longer must I spend five minutes clicking around to see changes in a view that's 5 steps down the funnel, I have a reasonable first indicator of problems when dependencies bleed over ...

    ... hell, it's even caught a backend bug that I didn't anticipate in the other test suite. Scoar!

    Most of all, it makes development more pleasurable. Seeing those views fly by when the tests run brings a smile to my face in pure nerdy joy.

    That's important.

    Enhanced by Zemanta

    Did you enjoy this article?

    Published on December 27th, 2012 in Uncategorized

    Learned something new?
    Want to become an expert?

    Here's how it works 👇

    Leave your email and I'll send you thoughtfully written emails every week about React, JavaScript, and your career. Lessons learned over 20 years in the industry working with companies ranging from tiny startups to Fortune5 behemoths.

    Join Swizec's Newsletter

    And get thoughtful letters 💌 on mindsets, tactics, and technical skills for your career. Real lessons from building production software. No bullshit.

    "Man, love your simple writing! Yours is the only newsletter I open and only blog that I give a fuck to read & scroll till the end. And wow always take away lessons with me. Inspiring! And very relatable. 👌"

    ~ Ashish Kumar

    Join over 14,000 engineers just like you already improving their careers with my letters, workshops, courses, and talks. ✌️

    Have a burning question that you think I can answer? I don't have all of the answers, but I have some! Hit me up on twitter or book a 30min ama for in-depth help.

    Ready to Stop copy pasting D3 examples and create data visualizations of your own?  Learn how to build scalable dataviz components your whole team can understand with React for Data Visualization

    Curious about Serverless and the modern backend? Check out Serverless Handbook, modern backend for the frontend engineer.

    Ready to learn how it all fits together and build a modern webapp from scratch? Learn how to launch a webapp and make your first 💰 on the side with ServerlessReact.Dev

    Want to brush up on your modern JavaScript syntax? Check out my interactive cheatsheet: es6cheatsheet.com

    By the way, just in case no one has told you it yet today: I love and appreciate you for who you are ❤️

    Created by Swizec with ❤️