Los Techies : Blogs about software and anything tech!

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) {
if(config.currentModule)
name = config.currentModule + " module: " + name;

became:


function test(name, callback) {
if(config.currentModule)
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");
            });
           
        });

Kick It on DotNetKicks.com
Posted Mar 11 2009, 07:20 PM by jlockwood

Comments

DotNetShoutout wrote QUnit: Almost just what I was looking for... - Joshua Lockwood
on 03-12-2009 5:09 PM

Thank you for submitting this cool story - Trackback from DotNetShoutout

len smith wrote re: QUnit: Almost just what I was looking for...
on 03-12-2009 7:07 PM

I used to use qunit, but I'm a recent screw-unit convert.

github.com/.../mock_spec.js

jlockwood wrote re: QUnit: Almost just what I was looking for...
on 03-12-2009 9:28 PM

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.

Reflective Perspective - Chris Alcock » The Morning Brew #306 wrote Reflective Perspective - Chris Alcock » The Morning Brew #306
on 03-13-2009 3:13 AM

Pingback from  Reflective Perspective - Chris Alcock  » The Morning Brew #306

Add a Comment

(required)  
(optional)
(required)  
Remember Me?

Enter the numbers above:
Copyright Los Techies 2008, 2009. All rights reserved.
Powered by Community Server (Commercial Edition), by Telligent Systems