Simple test harness

Simple JS test harness

by spoike

HTML

<ul id="testResults">
</ul>

CSS

body: { margin: 0; padding: 0; }
ul {
    margin: 0; padding: 0;
    list-style: none;    
    font-family: Arial, sans-serif;
}
ul li {
    margin: 0; padding: 10px 15px;
}
.pass {
    background-color: darkgreen;
    color: white;
    font-weight: bold;
}
.fail {
    background-color: darkred;
    color: white;
    font-weight: bold;
}

JavaScript

// Assertion function that adds to the test results list
function assert(b, str) {
  var results = document.getElementById('testResults');
  var result = document.createElement('li');
  result.setAttribute('class', (b ? 'pass' : 'fail'));
  result.innerHTML = str;
  results.appendChild(result);
}

assert(true, 'Truthy values passes');
assert(false, 'Falsy values fail');