Skip to content

Instantly share code, notes, and snippets.

@kristoferjoseph
Last active July 27, 2016 18:55
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save kristoferjoseph/9fa00cdd4bfec01bdcaa150f42c5dc95 to your computer and use it in GitHub Desktop.
Tiny test lib and runner.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>tiny tests</title>
<style>
body {
font-family: sans-serif;
padding: 3rem;
}
.test-pass {
margin-left: 2rem;
color: #2ECC40;
}
.test-fail {
margin-left: 2rem;
color: #FF4136;
}
</style>
</head>
<body>
<div id="output"></div>
<script src="./test.bundle.js"></script>
</body>
</html>
var assert = require('assert')
var display
if (typeof window !== 'undefined') {
display = document.getElementById('output')
}
function print(out) {
if (display) {
display.innerHTML += out
}
}
function fail(msg) {
var out = ' ✘ ' + msg
console.error(out)
print('<h4 class="test-fail">'+out+'</h4>')
}
function pass(msg) {
var out = ' ✔︎ ' + msg
console.info(out)
print('<h4 class="test-pass">'+out+'</h4>')
}
function test(label, func) {
beforeEach()
var f = false
label = label || ''
console.info(label)
print('<h3 class="test-label">'+label+'</h3>')
try {
msg = func()
}
catch(e) {
f = e.message
fail(f)
}
finally {
if (!f) {
pass('passed')
}
}
afterEach()
}
function beforeEach() {
}
function afterEach() {
}
@kristoferjoseph
Copy link
Author

kristoferjoseph commented Jul 27, 2016

Found myself without internet and wrote this tiny test lib. Was really fun to figure out and works for most modules. Works in node and the browser.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment