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");
});
});
Posted
Mar 11 2009, 07:20 PM
by
jlockwood