Skip to content

Instantly share code, notes, and snippets.

@joewalnes
Created November 1, 2012 21:01
Show Gist options
  • Save joewalnes/3996513 to your computer and use it in GitHub Desktop.
Save joewalnes/3996513 to your computer and use it in GitHub Desktop.
TinyTest.js
<script src="THING_TO_TEST.js"></script>
<script src="tinytest.js"></script>
<script>
TinyTest.run('THING_TO_TEST.js', {
'some test name': function() {
this.assertEquals(1, 2);
},
...
});
</script>
// Very simple unit-test library, with zero deps. Results logged to console.
// -jwalnes
var TinyTest = {
run: function(suiteName, tests) {
var failures = 0;
for (var testName in tests) {
var testAction = tests[testName];
try {
testAction.apply(this);
console.log('Suite:', suiteName, 'Test:', testName, 'OK');
} catch (e) {
failures++;
console.error('Suite:', suiteName, 'Test:', testName, 'FAILED');
console.error(e.stack);
}
}
setTimeout(function() { // Give document a chance to complete
if (window.document && document.body) {
document.body.style.backgroundColor = (failures == 0 ? '#99ff99' : '#ff9999');
}
}, 0);
},
fail: function(msg) {
throw new Error('fail(): ' + msg);
},
assert: function(value, msg) {
if (!value) {
throw new Error('assert(): ' + msg);
}
},
assertEquals: function(expected, actual) {
if (expected != actual) {
throw new Error('assertEquals() "' + expected + '" != "' + actual + '"');
}
},
assertStrictEquals: function(expected, actual) {
if (expected !== actual) {
throw new Error('assertStrictEquals() "' + expected + '" !== "' + actual + '"');
}
},
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment