Are You Testing Your Javascript Yet?

What’s the difference between your application’s javascript code and its java/ruby/python/whatever code?

Think about it for a second. Done? Well, the answer is: none!

In today’s web applications your javascript code is no different from the rest of your code base but yet we treat it with disregard - and we’re all to blame.

We often see developers investing time writing all sorts of automated tests around their code base but fail to do the same for their javascript - and yet they get surprised when their client can’t click on a button on their web app that depends on that ajax call that has been manually tested once when the page was first released.

Ask yourself this question if you’re not sure if you should be testing your javascript: If it breaks, can my users still benefit from my app? - I think the answer for most web apps is obvious.

As for tools, I can only speak of two, JsTestDriver and screw-unit, the latter being the one we’re using in my current project at ThoughtWorks.

What I liked about screw-unit is that it’s very easy to get it up and running. Being a Behavior-Driven Testing Framework it’s not surprising it has a syntax similar to Rspec -which I prefer over JsTestDriver’s similarity to JUnit’s.

This is how a test written with screw-unit could look like:

Not too bad for a javascript test suite huh?

So the next time you write a javascript line of code, how about writing a test for it? Consider using screw-unit as your tool but if you have been writing tests using a different framework and you’re happy with it, share your experience.
I’m always keen to learn better ways of doing what I do.

Cheers!

Comments