testit/dist/testit.js

59 lines
1.5 KiB
JavaScript
Raw Permalink Normal View History

2020-09-20 20:02:45 +00:00
/*! Test.it v1.0.0 | MIT | https://github.com/n2geoff/testit */
2020-09-08 22:37:54 +00:00
const test = {
"log": console.log,
2020-09-20 20:02:45 +00:00
"version": "v1.0.0",
2020-09-08 22:37:54 +00:00
"_tests": {},
"run": function run(errors, next) {
if(typeof errors !== "boolean") {
next = errors;
errors = true;
}
2020-09-08 22:37:54 +00:00
let tests = this._tests || [];
let failed = [];
let passed = [];
2020-09-08 22:37:54 +00:00
Object.keys(tests).forEach((name) => {
let test = tests[name];
2020-09-08 22:37:54 +00:00
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}`);
}
}
2020-09-08 22:37:54 +00:00
});
2020-09-08 22:37:54 +00:00
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}`);
2020-09-08 22:37:54 +00:00
return failed.length ? false : true;
}
},
"it": function it(tests) {
this._tests = tests;
return this;
},
2020-09-20 20:02:45 +00:00
"assert": (expression, msg) => {
try {
if(!expression) {
throw new Error(msg || "Assertion Failed");
2020-09-08 22:37:54 +00:00
}
2020-09-20 20:02:45 +00:00
} catch (e) {
throw new Error(msg);
}
2020-09-08 22:37:54 +00:00
}
};
2020-09-08 22:37:54 +00:00
export default test;