Last active
December 21, 2022 13:50
-
-
Save jacobrask/5547388 to your computer and use it in GitHub Desktop.
JavaScript unit testing in the console
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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