Mini Test Suite
Super basic test suite
by Aubrey Taylor
HTML
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.0.0-beta1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script>
JavaScript
//////////////////////////////
// Throw open that console! //
//////////////////////////////
suite = {};
function assert(condition, message) {
if (!condition) {
throw message || "Assertion failed";
}
}
function test(testName, callback) {
id = testName.split(" ").join("_");
suite[id] = {
testName: testName,
callback: callback,
isAsync: testName.toLowerCase().indexOf("async") > -1,
}
}
function runTest(test) {
var callback = test.callback;
try {
callback();
}
catch(error) {
console.log("Test Failed! ", test.testName + ", ", error);
}
}
function runTestAsync(test) {
var callback = test.callback;
var dfd = new $.Deferred();
callback(dfd);
dfd.done(function(done){
try {
done();
}
catch(error) {
console.log("Test Failed! ", test.testName + ", ", error);
}
})
}
function runTestSuite() {
var count = 0;
for(testName in suite) {
var test = suite[testName];
var isAsync = test.isAsync;
if(isAsync){
runTestAsync(test);
}
else {
runTest(test);
}
count++;
}
console.log(count + " tests passed!")
}
test("test is true", function(){
assert(true, "false is not True");
});
test("async test is true", function(dfd){
done = function() {
assert(true, "false is not true");
}
setTimeout(function(){
dfd.resolve(done);
});
})
runTestSuite()