.

Unit Testing Javascript |

It turns out I’ve amassed a heck of a lot of Javascript. It started out as simple effects. Then it progressed to some widgets. Now I ended up looking at Javascript as a valid tool in the arsenal.

That just creates one small problem. How the hell do you test Javascript? There are a number of libraries out there. But which one is the right one? I’m not sure I’m going to be able to answer that for you, but I’ll at least try to collect my notes so you can come to your own conclusion.

CrossCheck
JsUnit (Apparently there are two different libs names JsUnit so make sure you follow the link.)
Scriptaculous Test.Runner

CrossCheck

CrossCheck caught my eye almost immediately -
http://www.jsunit.net/talks/jsunit.ppt

Crosscheck is an open source testing framework for verifying your in-browser javascript. It helps you ensure that your code will run in many different browsers such as Internet Explorer and Firefox, but without needing installations of those browsers. The only thing you need is a Java Virtual Machine.

This sounded awesome - one of the biggest problems we currently have is that we have to test stuff under IE but no one wants to run a Windows box. (Yes I’m aware that I can run Parallels,VMWare, or Wine - but I haven’t gotten there yet - and it still will be a pain) They’re based out of Austin which is good to know - since that means I might meet them at some point.

My warning bells went off when I read their Cons page.

Element appearance is not accuarate
No CSS cascade

This is a problem since a lot of the Javascript I’ll be testing will be wdigets. How they are drawn is important. And judging by the number of stylesheets in our include directory I imagine No CSS cascade is also a problem. I wish there was an easy way to write test for CrossCheck that could be run in a browser under one of the other frameworks.

Test.Runner

One of the developers recommended this as a path to the future. It has support for for BDD. I’m not doing BDD. It’s on that list of things I would someday like to know more about.

I’m already using the Scriptactulous libraries for effects in Rails. So it seems like this might be a natural fit.

Then I read an overview from CrossCheck of Test.Runner. Full Article

Basically his main criticism of the test framework is that it automatically adds methods to the objects under test. Apparently there are arguments about how good an idea this is in general, I see it done a lot in both Javascript and Ruby and I’m not opposed to it - since I am often the beneficiary for the features it provides. That being said - it does seem very very very wrong for a test framework to do that.

Where does that leave us?

JsUnit

I started with a PowerPoint presentation. Then I dug in. It seems very complete. I mean it has all the assertion stuff you would expect. You can roll tests together to form suites. If I am understanding the docs correctly I can even setup a system where it will run all my tests on different machines with different browsers so I can know for sure that they really work.

Once I get more code up and organized in jsUnit I’ll post again with some lessons learned about how to organize your tests.

There were a couple of things that seem to be missing. The first is a code coverage tool. That would be seriously helpful - especially since I now find myself the proud owner of lots of code that doesn’t have a single test written on it. The second one is an easy way to mock objects. That comes in handy when you want a quick and easy way to make sure things work without having to go through the whole process of setting everything up.

Thanks to this blog I found JSMock which seems to be exactly what the doctor ordered.

Now I just have to sit down and write some tests.

Update
Wow - the jsmock guy ROCKS! He actually shipped a Rakefile with his lib. When you run it - it launches firefox and validates all his code. Not only is that great to verify it is working - but it means I can just look at his code to see how to setup the exact same thing for all of my code - I love it when someone else does all the work!

Update 2
More tools for the collection. Turns out there is a nice JavaDoc style documentation generator for Javascript. JsDoc Only bummer is it is in perl.

2 Responses to 'Unit Testing Javascript'
  1. Charles Lowell:

    It’s true. At this point, Crosscheck is really best for doing low-level gutsy-type stuff like testing your client-server interaction, where you can control at a very fine-grained level when and how your server replies to events (it’s simulated). It’s also good for at what time and in what order events will take place (you can’t cause default actions like focus shifting on a TAB press to occur inside a browser due to security reasons). But, like you said, it doesn’t repeat the rendering logic of the browser itself, just the memory model. If anyone out there is into pain and wants to reverse engineer browser rendering code, please get in touch!

    I’m intrigued by the idea of having another version of crosscheck that runs in browser, or even an adapter for running Crosscheck tests inside other in-browser frameworks. Obviously, all of the cross-browser functionality wouldn’t be available in that mode, but it might make things simpler by only requiring a single test framework to learn.

  2. Michael Mathews:

    As the original author of JSDoc I can verify that you are not the first person to complain about the tool’s perlishness. Some love Perl, some hate it, but it was the best choice at the time for parsing JS files, and it works, up to a point.

    But the good news is that we are about to release version 2 of the JSDoc tool, and guess what? No Perl. In fact it’s 100% JavaScript! It will run in Rhino or even completely inside a simple web browser. I don’t want to over hype the thing because it’s still alpha-ware and won’t be officially released for a few weeks yet, but watch the JSDoc site for announcements if you’re interested.

Leave a Reply

Moderation Active: Old stuff here... Therefore your comment on this post will be moderated (i.e. don't submit twice !)

    Stuff I want to read

    Shelfari: Book reviews on your book blog

    Stuff I've Read

    Shelfari: Book reviews on your book blog
    Categories
    Archives

    .