Skip to content

Instantly share code, notes, and snippets.

@kevinmeredith
Last active January 1, 2016 11:09
Show Gist options
  • Select an option

  • Save kevinmeredith/8136425 to your computer and use it in GitHub Desktop.

Select an option

Save kevinmeredith/8136425 to your computer and use it in GitHub Desktop.
"Secrets of the JavaScript Ninja" home-made assert
<html>
<head>
<title>Test Suite</title>
<script>
(function() {
var results;
this.assert = function assert(value, desc) {
var li = document.createElement("li");
li.className = value ? "pass" : "fail";
li.appendChild(document.createTextNode(desc));
results.appendChild(li);
if(!value) {
li.parentNode.parentNode.className = "fail"; // appear to need parentNode.parentNode for
}
return li;
};
this.test = function test(name,fn) {
results = document.getElementById("results");
results = assert(true, name).appendChild(
document.createElement("ul"));
fn();
};
})();
window.onload = function() {
test("A test.", function() {
assert(true, "first assumption completed");
assert(true, "second assumption completed");
assert(true, "third assumption completed");
});
test("Another test.", function() {
assert(true, "First test completed");
assert("false", "second test failed");
assert(true, "third assertion completed");
});
test("A third test.", function() {
assert(null, "fail");
assert(5, "pass");
});
};
</script>
<style>
#results li.pass { color: green; }
#results li.fail { color: red; }
</style>
</head>
<body>
<ul id="results"></ul>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment