Skip to content

Instantly share code, notes, and snippets.

@undecided

undecided/dojo_assert.js

Last active Aug 29, 2015
Embed
What would you like to do?
Wrote a simple Javascript / Node.js testing framework on the train so that I could dojo.
// TODO: Would love VERBOSE to be given via environment var
// TODO: Should probably have assertIdentity (===) too. Trivial to add.
// TODO: I would like passing tests to appear as full-stops on one line in quiet mode.
var VERBOSE = true;
function describe(desc, desc_fn) {
desc_fn(function(it_desc, it_fn) {
it_fn({
assertEqual: function(expected, actual) {
if(expected == actual) {
this.pass();
} else {
this.fail(expected, actual);
}
},
assertTrue: function(actual) {
if(actual) {
this.pass();
} else {
this.fail(actual, null, true);
}
},
assertCustom: function(custom_function, actual) {
if(custom_function(actual)) {
this.pass();
} else {
this.fail(custom_function, actual);
}
},
pass: function() {
if(VERBOSE) {
console.log("PASS: " + desc + " - " + it_desc);
} else {
console.log('.');
}
},
fail: function(expected, actual, skip_actual) {
console.log("\nFAIL: " + desc + " - " + it_desc);
console.log("Expected:", expected);
if(!skip_actual) {
console.log("Actual:", actual);
}
}
});
});
}
/*=========== TESTS ================= */
describe("Dojo Testing Framework", function(it) {
it("should test equality", function(lets) {
lets.assertEqual(true, true); // Pass
lets.assertEqual(false, true); // Fail
});
it("should test truth", function(lets) {
lets.assertTrue(true); // Pass
lets.assertTrue(false); // Fail
});
it("should test... uhh... other stuff", function(lets) {
lets.assertCustom(function(actual) { return true == actual }, 1); // Pass
lets.assertCustom(function(actual) { return true === actual }, 1); // Fail
})
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.