Last active
August 29, 2015 13:56
-
-
Save undecided/9206975 to your computer and use it in GitHub Desktop.
Wrote a simple Javascript / Node.js testing framework on the train so that I could dojo.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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