Skip to content

Instantly share code, notes, and snippets.

@chromy
Created June 27, 2013 22:04
Show Gist options
  • Save chromy/5880818 to your computer and use it in GitHub Desktop.
Save chromy/5880818 to your computer and use it in GitHub Desktop.
A tiny Javascript test framework.
function AssertionError(message) {
this.name = "AssertionError";
this.message = message || "Assertion Error";
}
AssertionError.prototype = new Error();
AssertionError.prototype.constructor = AssertionError;
function Tests() {
this.tests = [];
}
Tests.prototype.add = function(test_case) {
this.tests.push(test_case);
};
Tests.prototype.run = function() {
var total = 0;
var failed = 0;
var results = [];
this.tests.forEach(function(testcase) {
total++;
try {
testcase();
results.push('.');
} catch (e) {
failed++;
console.log(e);
if (e instanceof AssertionError) {
results.push('F');
} else {
results.push('E');
}
}
});
console.log(results.join(''));
console.log(failed + " of " + total + " failed (" + failed/total + "%)");
};
function assert(expr, message) {
if (!expr) {
throw new AssertionError(message);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment