Unit testing JavaScript with JsUnit & Ant

Testing your JavaScript can be really easy - and that’s how it should be.  Before you start a JavaScript project, you should set up a system that allows you to run your tests very easily. Once you remove barriers to running your test suite (like having to click 5 times or hunt and peck for a file in a directory), you’ll find yourself running your tests more often.  Eclipse is a great IDE for developing with JavaScript for me.  I don’t particularly like Aptana, so I use the JSEclipse plugin from Adobe for syntax highlighting and checking and run builds with the integrated Ant view.   I’ve found a sweet spot when running unit tests on the JavaScript code I’m developing for InternetBroadcasting - currently I’m using an Ant task to run the JsUnit tests. The Ant task does the following things:

  1. Iterates over a list of browsers and creates a list of those that are present on the current machine (using a Groovy task)
  2. Runs the unit test suite in all of the browsers found
  3. Runs JsDoc Toolkit to generate javadoc-style documentation from code comments 
  4. Concatenates all the JavaScript files into one file, and puts it in a new folder (”dist”)
  5. Minifies the JavaScript using YUI Compressor

I love the fact that my tests run on Firefox, IE7 & 6, Safari 3, and Opera 9 on my local machine, and all I have to do is sit and watch.  During the deployment, the release manager runs the tests on a unixy machine which has a Firefox binary.   It’s handy to have one build file that works on any machine, regardless of the operating system or installed software (something that Java dorks are used to doing). At any rate, because I’ve automated the test runner on several browsers, and because I have a one-click way to run the tests,  I’m finding cross-browser bugs waaaay before I would have before I started using this technique.  

No comments yet. Be the first.

Leave a reply