Last active
April 25, 2016 17:42
-
-
Save placek/1fdd3176a813f91f0600616845a0d30b to your computer and use it in GitHub Desktop.
simple unit testsiute for javascript
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
(function() { | |
// styles for console | |
var greenStyle = 'font-weight: bold; color: green;'; | |
var redStyle = 'font-weight: bold; color: red;'; | |
var normalStyle = 'font-weight: none;'; | |
// customizable assertions | |
var _assertions = { | |
truth: function(obj) { | |
if(obj != true) { | |
throw new Error('argument is not true'); | |
} | |
}, | |
equality: function(a, b) { | |
if(a != b) { | |
throw new Error('expected ' + a + ' to be equal ' + b); | |
} | |
} | |
}; | |
// jsUnit component | |
this.jsUnit = { | |
test: function(description, testUnit) { | |
try { | |
testUnit(_assertions); | |
console.log('%cjsUnit: "' + description + '"', greenStyle); | |
} catch(exception) { | |
console.log('%cjsUnit: "' + description + '" - ' + exception.message + "\n%c" + exception.stack, redStyle, normalStyle); | |
} | |
} | |
}; | |
})(); | |
// example: | |
// jsUnit.test('truth test that fails', function(assert) { | |
// assert.truth(false); | |
// }); | |
// jsUnit.test('truth test that passes', function(assert) { | |
// assert.truth(true); | |
// }); | |
// jsUnit.test('equality test that fails', function(assert) { | |
// assert.equality(2, 4); | |
// }); | |
// jsUnit.test('equality test that passes', function(assert) { | |
// assert.equality(3, '3'); | |
// }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment