Last active
January 15, 2021 19:45
-
-
Save mattmccray/4a3f171b122d9da05b38e79efdcdb143 to your computer and use it in GitHub Desktop.
MUTT - Micro Unit Testing Tool (for browsers)
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
// MUTT - Micro Unit Testing Tool (for browsers) | |
let _testDepth = 0; | |
export function test(name, suite) { | |
const title = `🐶 ${name}`; | |
const ctx = { | |
count: 0, | |
failed: 0 | |
}; | |
_testDepth++; | |
if (_testDepth > 1) console.group(title); | |
else console.groupCollapsed(title); | |
try { | |
suite({ | |
equal(actual, expected, message) { | |
assert(ctx, actual == expected, message); | |
}, | |
notEqual(actual, expected, message) { | |
assert(ctx, actual != expected, message); | |
}, | |
fail(message) { | |
assert(ctx, false, message); | |
}, | |
ok(actual, message) { | |
assert(ctx, actual, message); | |
}, | |
notOk(actual, message) { | |
assert(ctx, !actual, message); | |
}, | |
is(actual, expected, message) { | |
assert(ctx, actual === expected, message); | |
}, | |
isNot(actual, expected, message) { | |
assert(ctx, actual !== expected, message); | |
}, | |
throws(block, message) { | |
assertException(ctx, block, message, true); | |
}, | |
doesNotThrow(block, message) { | |
assertException(ctx, block, message, false); | |
} | |
}); | |
} catch (err) { | |
console.error("Error running tests:", err); | |
} | |
if (ctx.count > 0) { | |
if (ctx.failed === 0) | |
console.log(`✅ ${ctx.count} of ${ctx.count} succeeded.`); | |
else console.log(`💥 ${ctx.failed} of ${ctx.count} failed.`); | |
} | |
console.groupEnd(title); | |
if (ctx.failed > 0) console.log(`🚨 ${name}: ${ctx.failed} Failure(s)`); | |
_testDepth--; | |
} | |
function assertException(ctx, block, message, expectsThrow) { | |
let threw = false; | |
try { | |
block(); | |
} catch (err) { | |
threw = true; | |
} | |
assert(ctx, threw === expectsThrow, message); | |
} | |
function assert(ctx, assertion, message) { | |
ctx.count++; | |
console.assert(assertion, message); | |
if (!assertion) ctx.failed++; | |
else console.info(message); | |
} | |
export default test; | |
// Quick self-test | |
test("MUTT", (t) => { | |
test("Setup", () => { | |
const methods = Object.keys(t).sort().join(","); | |
t.equal( | |
methods, | |
"doesNotThrow,equal,fail,is,isNot,notEqual,notOk,ok,throws", | |
"Provides all assertion methods." | |
); | |
}); | |
test("Assertions", () => { | |
t.ok(true, "ok(true)"); | |
t.notOk(false, "notOk(false)"); | |
t.equal(1, 1, "equal(1,1)"); | |
t.equal(1, "1", "equal(1,'1')"); | |
t.notEqual(1, 2, "notEqual(1,2)"); | |
t.is(1, 1, "is(1,1)"); | |
t.isNot(1, 2, "isNot(1,2)"); | |
t.isNot(1, "1", "isNot(1,'1')"); | |
t.doesNotThrow(() => { | |
// Nothing to see here | |
}, "doesNotThrow(() => {})"); | |
t.throws(() => { | |
throw new Error("There was an error."); | |
}, "throws(() => {})"); | |
// t.fail("Force a failure."); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment