Skip to content

Instantly share code, notes, and snippets.

@placek
Last active April 25, 2016 17:42
Show Gist options
  • Save placek/1fdd3176a813f91f0600616845a0d30b to your computer and use it in GitHub Desktop.
Save placek/1fdd3176a813f91f0600616845a0d30b to your computer and use it in GitHub Desktop.
simple unit testsiute for javascript
(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