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

Learned something new? Want to become a better engineer? ๐Ÿ’Œ

Join 9,400+ people just like you already improving their skills.

Here's how it works ๐Ÿ‘‡

Leave your email and I'll send you an Interactive Modern JavaScript Cheatsheet ๐Ÿ“– right away. After that you'll get a thoughtfully written email every week aboutย React, JavaScript,ย  andย lessons learned in my 20 years of writing code for companies ranging from tiny startups to Fortune5 behemoths.



Man, I love your way of writing these newsletters. Often very relatable and funny perspectives about the mundane struggles of a dev. Lightens up my day. ~ Kostas

PS: You should also follow me on twitter ๐Ÿ‘‰ here.
It's where I go to shoot the shit about programming.