Skip to content

Instantly share code, notes, and snippets.

@lsongdev
Last active August 31, 2018 04:21
Show Gist options
  • Save lsongdev/5f1fff69c75ff079d93af872bc4edb87 to your computer and use it in GitHub Desktop.
Save lsongdev/5f1fff69c75ff079d93af872bc4edb87 to your computer and use it in GitHub Desktop.
/**
* super tiny testing framework
*
* @author Liu song <[email protected]>
* @github https://github.com/song940
*/
function color(str, c){
return "\x1b[" + c + "m" + str + "\x1b[0m";
};
const describe = (title, suite) => {
console.log(' - ', color(title, 36));
suite();
console.log();
};
let n = 0, e = 0;
const it = async (title, test) => {
try {
n++;
await test();
console.log(color(` ✔ ${title}`, 32));
} catch (err) {
e++;
console.error(color(` ✘ ${title}`, 31));
console.log();
console.error(color(` ${err.name}: ${err.message}`, 31));
console.error(color(` expected: ${err.expected}`, 32));
console.error(color(` actual: ${err.actual}`, 31));
console.log();
}
};
process.on('exit', () => {
console.log();
console.log(color(` All test case done (${n-e}/${n})`, e ? 31 : 32));
console.log();
if(e) process.exit(1);
});
module.exports = {
describe,
it
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment