No-Frills In-Browser Unit Testing

or, Testing Framework? What Testing Framework?

by Alex Chaffee
Burlington Code Academy

The Best Tests

"The best tests are the ones you actually write." - me

The Best Test Framework

function assert(value) {
  if (!value) {

All you need is loveassert.

Even better

function assert(value, why) {
  if (value) {
    console.log("Success: " + why);
  } else {
    console.error(new Error("Failure: " + why).stack);

console keeps calm and carries on

console.assert(pageNumber >= 0 && pageNumber < numPages,
               'page number should be in range');

Test As You Go - Step 1

Start with the null case.

assert(fancyText('') === '');

function fancyText(s) {
    return '';

"Run Test Suite" command: F5 (Refresh)

Test As You Go - Step 2

Write one more test case...

assert(fancyText('') === '');
assert(fancyText('apple') === 'Apple');

...and enough code to make it pass

function fancyText(s) {
    return s[0].toUpperCase() + 

Test As You Go - Step 3

There is no step 3!

Just keep writing tests, and code that satisfies them.

assert(fancyText('pad thai') === 'Pad Thai');
assert(fancyText('state of the union') === 'State of the Union');

Self-Testing Web Sites



tap dancing shoes

image from

Tapped Out

fancy screenshot with tap

Tapped Source: fancy.js

t.equal is tap's assert :

var test = require('tape');
if (window.tapExtension) {
  test = window.tapExtension(test);

function fancyText(s) {
  return s[0].toUpperCase() + 

test('fancy formatter', function(t) {
  t.equal(fancyText(""), "", 
    "should not do anything to an empty string");
  t.equal(fancyText("bob"), "Bob", 
    "should capitalize a single word");

Tapped Source: fancy-bundle.js


npm install --save-dev browserify tape
browserify -o fancy-bundle.js --standalone fancyText fancy.js

Tapped Source: index.html

<button id="format" onclick="doFormat()">
>> Format >>
<script src="fancy-bundle.js"></script>
function doFormat() {
  var inputWidget = document.getElementById("input");
  var outputWidget = document.getElementById("output");
  var output = fancyText(inputWidget.value);
  outputWidget.value = output;



 Previous Lesson Next Lesson