End Loose Ends

April 2010 Entries

Debugging JavaScript Unit Tests

I am experimenting with jsUnit, a javascript unit testing framework.  I am really enjoying the ability to easily unit test my javascript code.  I particularly like the UI:

 

image

jsUnit uses a technique they refer to as cache-busting to ensure the System Under Test is reloaded between test runs.  Unfortunately, this makes it difficult for developers that want to debug a unit test (GASP! Yes, once in a while, the debugger is useful when unit testing).

The key is a simple javascript trick for setting a breakpoint in the actual code.  Wherever you want the code to break, just add a debugger statement and Voila! You will be able to debug why that pesky unit test is failing.  Keep in mind, you will need to have a javascript debugger running.  In this case, I opened FireBug, enabled the script debugger for the site and let the debugger statement do the rest.

image