Patching JsTestDriver

I hacked a little on JsTestDriver today, fixing a few bugs in some assertions, and possibly more interestingly - improved assertion error messages for certain types of objects.

tl;dr download the patch (to apply to local build of JsTestDriver), or a readily patched Asserts.js file.

Bug fixes

I fixed the following bugs in the patch:

What this means is that assertEquals is now safer to use, and you can now use it with DOM nodes, which was impossible before. I'm a little worried that the method will still produce false positives in some cases where the objects compared have no enumerable properties, but I'll look further into this.

Error messages

So to the "itch scratching" - I've been slightly annoyed for a while about how JsTestDriver reports certain types of objects when assertions fail. For instance, if you were to do the following:

"test compare DOM nodes": function () {
    var span = document.createElement("span");
    span.id = "hey";
    span.className = "note text";
    var ol = document.createElement("ol");
    ol.class = "items";

    assertSame(span, ol);
}

JsTestDriver would produce a message along the lines of "expected {} but was {}" - or even worse - "Converting circular structure to JSON" - which is not really helpful at all. Additionally, as I explained above, had you used assertEquals in place of assertSame you'd run the risk of the test actually passing. Oops!

With my patch the above test will rather fail with the following message: 'expected <span id="hey" class="note text">...</span> but was <ol class="items">...</ol>', which is a bit more verbose, but also vastly more helpful.

Functions

While I was at it I also made a minor improvement to the string representation of functions. If you're comparing functions, and the function has a name, rather than "expeced [function] but was [function]" you will see "expected function aFunc() but was function getSomething", which, again, helps locate failures faster.

That's really all there is to it. If you build JsTestDriver from source, you can apply the following patch to trunk. If you don't you can download a readily patched Asserts.js file and simply add it as the first file to be loaded in jsTestDriver.conf.

Published 23. September 2010 in javascript, test driven development og open source.

Possibly related