Last active
November 3, 2018 17:36
-
-
Save freedmand/35ad503f2a3a088a2792e91e6a904a65 to your computer and use it in GitHub Desktop.
Minimal unit testing framework (TypeScript)
This file contains hidden or 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 assert(arg1: any, arg2: any) { | |
if (arg1 != arg2) throw new Error(`Expected ${arg1} to equal ${arg2}`); | |
} | |
const PASS = ['32']; // green | |
const FAIL = ['31', '1']; // red, bold | |
function logStyle(codes: string | string[], text: string) { | |
if (Array.isArray(codes)) codes = codes.join(';'); | |
console.log(`\x1b[${codes}m${text}\x1b[0m`); | |
} | |
class Tester { | |
constructor() {} | |
test(name: string, fn: () => void) { | |
try { | |
fn(); | |
logStyle(PASS, `${name} PASSED`); | |
} catch (e) { | |
logStyle( | |
FAIL, | |
`${name} FAILED: | |
${e}` | |
); | |
} | |
} | |
} | |
/** | |
* Usage: | |
* const t = new Tester(); | |
* t.test('testName', () => { | |
* assert(1, 1); | |
* assert(1, 2); // will fail | |
* }); | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment