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 });
    $("div#view").html("");
    });
    it("renders go button", function () {
    App.state.set({ page: "events" });
    var $go = $("div#view a.go:visible");
    $go.size().should.equal(1);
    $go.attr("href").should.equal("/lobby");
    });
    });

    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 15,161+ engineers just like you already growing their careers with my emails, workshops, books, and courses.

    ⭐️⭐️⭐️⭐️✨
    4.5 stars average rating

    Have a burning question that you think I can answer? Hit me up on twitter and I'll do my best.

    Who am I and who do I help? I'm Swizec Teller and I turn coders into engineers with "Raw and honest from the heart!" writing. No bullshit. Real insights into the career and skills of a modern software engineer.

    Curious about Serverless and the modern backend? Check out Serverless Handbook, for frontend engineers 👉 ServerlessHandbook.dev

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

    Want to become a true senior engineer? Getting the title's easy. Just stick around. Being a true senior takes a new way of thinking. The Senior Mindset Series can help 👉 SeniorMindset.com. It's my most loved series of essays.

    Want to get my best emails on JavaScript, React, Serverless, Fullstack Web, or Indie Hacking? Check out swizec.com/collections

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

    Did someone amazing share this letter with you? Wonderful! You can sign up for my weekly letters for software engineers on their path to greatness, here: swizec.com/blog

    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 ❤️