Swizec Teller - a geek with a hatswizec.com

Senior Mindset Book

Get promoted, earn a bigger salary, work for top companies

Senior Engineer Mindset cover
Learn more

    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.

    Published on December 27th, 2012 in Uncategorized

    Did you enjoy this article?

    Continue reading about Testing Backbone apps with Mocha

    Semantically similar articles hand-picked by GPT-4

    Senior Mindset Book

    Get promoted, earn a bigger salary, work for top companies

    Learn more

    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.

    Want to become a true senior engineer? Take ownership, have autonomy, and be a force multiplier on your team. The Senior Engineer Mindset ebook can help 👉 swizec.com/senior-mindset. These are the shifts in mindset that unlocked my career.

    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 get my best emails on JavaScript, React, Serverless, Fullstack Web, or Indie Hacking? Check out swizec.com/collections

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