QUnit: Almost just what I was looking for…

I started using QUnit a few months back and have really enjoyed it.  That said, I was irked by a few minor features of QUnit and finally got irritated enough to make some changes.

First off, what’s with adding “module: ” as a delimiter for the module name and the test?  It used to be “$”.  Well, I’ve been using it with a BDDish convention and this feature finally got under my skin.  I checked the current version of testrunner.js and it seems to be unchanged.

Simple change:

function test(name, callback) {
name = config.currentModule + " module: " + name;


function test(name, callback) {
name = config.currentModule + " : " + name;

Next, I was a little dismayed that we had ok() but no not_ok() and equals() but no not_equals(). There was also no assertion for is_null, not_null, is_undefined and not_undefined (strict for both).
To fix this, I first created the functions I wanted in testrunner.js (I re-used ok for most of these…may do something more intelligent with the messages in the future, ok just sends (no message) if none is specified):

function not_ok(a, msg) {
ok(!a, msg);
function not_equals(actual, expected, message) {
push(expected != actual, actual, expected, message);
function is_null(a, msg) {
ok(a === null, msg);
function not_null(a, msg) {
ok(a !== null, msg);
function is_undefined(a, msg) {
ok(a == undefined, msg);
function not_undefined(a, msg) {
ok(a !== undefined, msg);

** This was all done in the function test(name, callback) in testrunner.

Then, I had to add this to the section where the ‘window’ object is extended (see code follofing comment: // public API as global methods):

$.extend(window, {
test: test,
module: module,
expect: expect,
ok: ok,
not_ok: not_ok,
equals: equals,
not_equals: not_equals,
is_null: is_null,
is_undefined: is_undefined,
not_null: not_null,
not_undefined: not_undefined,


Now I got the assertion methods that I wanted in the first place! I hate mucking with the internals on the testrunner.js file, but the symbol “config” is hidden so I couldn’t define not_equals externally. It would require mucking with the internals in any case.

Well, hope that’s of interest to someone out there. It was simply too much fun not to share.


Oooh, and I’d be remiss if I didn’t include my tests, right?

        $(document).ready(function() {

            module(“when using assertion extensions”);

            test(“should support not_ok”, function() {
                not_ok(true == false, “contradiction, should be not_ok”);
                equals(true, false);

            test(“should support is_null”, function() {
                var testVal = null;
                is_null(testVal, “should be null”);

            test(“should support not_null”, function() {
                var testVal = “null”;
                not_null(testVal, “should not be null”);
                not_null(undefined, “undefined is strictly not null”);

            test(“should support is_undefined”, function() {
                function testFunc() {}
                is_undefined(undefined, “obviously undefined”);
                is_undefined(testFunc.method1, “also undefined”);

            test(“should support not_undefined”, function() {
                function testFunc() {}
                testFunc.method1 = function() {};
                not_undefined(null, “null is not undefined”);
                not_undefined(testFunc.method1, “also undefined”);

About Joshua Lockwood

I code stuff and people pay me for it.
This entry was posted in JavaScript, scripting, Unit Tests. Bookmark the permalink. Follow any comments here with the RSS feed for this post.

2 Responses to QUnit: Almost just what I was looking for…

  1. jlockwood says:

    Yeah, heard of screw unit…just haven’t played with it much. I looked at the link and they look pretty similar syntax wise. As far as the finer syntactic details…I guess I’m not too picky. In fact, I believe that if you can crank out JavaScript code without complaint then you’ve probably already developed a super-human strength to ignore syntax. I love JavaScript as a language, but the syntax itself is wonky (for instance, I hate the keyword ‘function’).

    Thanks for the link though…I’ll probably look at it again. AgileJoe has been pitching it to me for a while.

Leave a Reply

Your email address will not be published. Required fields are marked *


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>