Skip to content

Instantly share code, notes, and snippets.

@freedmand
Last active November 3, 2018 17:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save freedmand/35ad503f2a3a088a2792e91e6a904a65 to your computer and use it in GitHub Desktop.
Save freedmand/35ad503f2a3a088a2792e91e6a904a65 to your computer and use it in GitHub Desktop.
Minimal unit testing framework (TypeScript)
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