Javascript: Unit Testing
//test suite
function assert (value, desc) {
if (value) {
console.debug("PASS: " + desc);
}
else {
console.error("FAILED: " + desc);
}
}
window.onload = function () {
assert(true, "The test suite is running.");
assert(false, "Fail!");
};
// test group
(function () {
this.assert = function assert(value, desc) {
if (value) {
console.debug("PASSED: " + desc);
}
else {
console.error("FAILED: " + desc);
}
};
this.test = function test(name, fn) {
console.warn("TESTGROUP: " + name);
fn();
};
})();
window.onload = function () {
test("A test.", function () {
assert(true, "First assertion completed");
assert(true, "Second assertion completed");
assert(true, "Third assertion 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");
});
};
// Async test group
(function () {
var queue = [],
paused = false;
this.test = function (name, fn) {
queue.push(function () {
console.warn("TESTGROUP: " + name);
fn();
});
runTest();
};
this.pause = function () {
paused = true;
};
this.resume = function () {
paused = false;
setTimeout(runTest, 1);
};
function runTest() {
if (!paused && queue.length) {
queue.shift()();
if (!paused) {
resume();
}
}
}
this.assert = function assert(value, desc) {
if (value) {
console.debug("PASSED: " + desc);
}
else {
console.error("FAILED: " + desc);
}
};
})();
window.onload = function () {
test("Async Text #1", function () {
pause();
setTimeout(function () {
assert(true, "First test completed");
assert(false, "Test failed!");
resume();
}, 1000);
});
test("Async Test #2", function () {
pause();
setTimeout(function () {
assert(true, "Second test completed");
resume();
}, 1000);
});
test("Third Async Test", function () {
pause();
setTimeout(function () {
assert(true, "First test");
assert(false, "Second tet");
assert(true, "Third test");
}, 3000);
});
};
// Test suite
function assert (value, desc) {
var li = document.createElement("li");
li.className = value ? "pass" : "fail";
li.appendChild(document.createTextNode(desc));
document.getElementById("results").appendChild(li);
}
window.onload = function () {
assert(true, "The test suite is running.");
assert(false, "Fail!");
};
// Test group
(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";
}
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 assertion completed");
assert(true, "Second assertion completed");
assert(true, "Third assertion 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");
});
};
// Async test group
(function () {
var queue = [], paused = false, results;
this.test = function (name, fn) {
queue.push(function () {
results = document.getElementById("results");
results = assert(true, name).appendChild(
document.createElement("ul")
);
fn();
});
runTest();
};
this.pause = function () {
paused = true;
};
this.resume = function () {
paused = false;
setTimeout(runTest, 1);
};
function runTest() {
if (!paused && queue.length) {
queue.shift()();
if (!paused) {
resume();
}
}
}
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";
}
return li;
};
})();
window.onload = function () {
test("Async Text #1", function () {
pause();
setTimeout(function () {
assert(true, "First test completed");
resume();
}, 1000);
});
test("Async Test #2", function () {
pause();
setTimeout(function () {
assert(true, "Second test completed");
resume();
}, 1000);
});
};