Erik A. Hanson's Weblog

Debugging a JsUnit Test With Firebug

Posted: November 16th, 2006    Tags: Howto, Javascript, JsUnit

Sometimes I find that I want to debug a JsUnit test because I’m not quite sure what’s going on in the browser.

FireBug has a nice debugger, but I couldn’t get it to work with the JsUnit test runner. I could set breakpoints in JsUnit itself, but any breakpoints in my test would be ignored.

And then it hit me: ditch the test runner. I opened my test page directly in Firefox, opened FireBug, clicked the Debugger tab, and chose my test from the “Scripts” menu. Here’s a simplified version:

function testUpdate() {
  var listPanel = new ListPanel();
  listPanel.update(["one", "two", "three"]);

  var children = $(”content”).childNodes;
  assertEquals(”one”, children[0]);
  assertEquals(”two”, children[1]);
  assertEquals(”three”, children[2]);
}

I put a breakpoint at the first assertEquals and switched to the Console tab and typed testUpdate(). I saw the ListPanel getting drawn in the browser (something you don’t see in the JsUnit test runner) and in the Debugger tab I could inspect all my variables.

But even better, I could go back to the Console tab and start evaluating Javascript.

>>> children.length
3

Good.

>>> children[0]
div

Bad.

>>> children[0].firstChild
[Text] “one”

Better.

>>> children[0].firstChild.nodeValue
“one”

Perfect.



4 Responses to “Debugging a JsUnit Test With Firebug”

  1. Jim Sproull Says:

    Hey, thanks for the tip! One of those ‘why didn’t I think of that’ moments ;)

  2. Erik A. Hanson » Blog Archive » A Ruby Script To Generate a JsUnit Suite Says:

    [...] Also, I wanted to be able to view any test page so I could debug the tests with Firebug (as explained here). [...]

  3. Alex Egg Says:

    Do you have any info on integration jsunit with rails. Mostly I’m intersted in my jsunit tests being included in the rails test environment.

    e.g.

    rake test
    run unit tests…
    run functional tests…
    run integration tests..
    run jsunit tests…

  4. Erik Says:

    seleniumrc_fu has some JsUnit integration:

    README | browse SVN | download page

Leave a Reply

About Me

I'm a software developer and consultant in San Francisco. I mostly develop web apps in Javascript, Java and Rails. There's a little more about me on my LinkedIn profile.

Recent Projects

wshlst.com: a group-oriented wish list website.

BoardsConsult​/​RotationTracker: a site to help medical residents prepare for the U.S. Medical Licensing Exam, and a related site to help medical schools evaluate and track the progress of the residents in their programs.

Jobs

I moderate a few completely free jobs mailing lists with RSS feeds and zero spam: XP jobs, Rails jobs and Mac development jobs. Also, my pals at Pivotal Labs have agile Rails and Java job openings.

Plug: Web Hosting

Looking for cheap shared web hosting and/or domain registration? I've been using DreamHost for years and I've been very happy with them. They offer tons of disk space and bandwidth, one-click installation for apps like WordPress and Gallery, unlimited domains, thousands of email accounts, private domain registration, and more. They are also a nice bunch of folks, not some big dumb company.