Skip to content

Instantly share code, notes, and snippets.

@jacobrask
Last active December 21, 2022 13:50
Show Gist options
  • Save jacobrask/5547388 to your computer and use it in GitHub Desktop.
Save jacobrask/5547388 to your computer and use it in GitHub Desktop.
JavaScript unit testing in the console
function test (name, fn) {
console.group(name);
var args = [ console.assert.bind(console) ];
// Async
if (fn.length > 1) {
var timeout = setTimeout(function(){ console.error(name+' timed out'); }, 2000);
args.push(clearTimeout.bind(null, timeout));
}
fn.apply(null, args);
console.groupEnd();
}
test("My test", function (assert) {
assert(true, "Everything ok?");
});
test("Asynchronous test", function (assert, done) {
setTimeout(function () {
assert(true, "Everything ok?");
done();
}, 100);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment