testit/src/testit.js

64 lines
1.7 KiB
JavaScript

/*! Test.it v1.1.2 | MIT | https://github.com/n2geoff/testit */
const test = {
"log": console.log, // eslint-disable-line
"version": "v1.1.2",
"_tests": {},
"run": function run(errors, next) {
// TODO: rewrite to allow a show errors flag (optional)
if(typeof errors !== "boolean") {
next = errors;
errors = true;
}
let tests = this._tests || [];
// capture results
let failed = [];
let passed = [];
// loop through tests
Object.keys(tests).forEach((name) => {
let test = tests[name];
// execute
try {
test();
passed.push(`\n+OK ${name}`);
} catch (err) {
if (errors) {
failed.push(`\n-ERR ${name} \n --- \n ${err.stack} \n ---`);
} else {
failed.push(`\n-ERR ${name}`);
}
}
});
// summary
if(typeof next === "function") {
return next({
pass: passed,
fail: failed
});
} else {
test.log(...passed, ...failed);
test.log(`\n# tests ${failed.length + passed.length} pass ${passed.length} fail ${failed.length}`);
return failed.length ? false : true;
}
},
"it": function it(tests) {
this._tests = tests;
return this;
},
"assert": (expression, msg) => {
try {
if(!expression) {
throw new Error(msg || "Assertion Failed");
}
} catch (e) {
throw new Error(msg);
}
}
};
export default test;