testit/src/testit.js

67 lines
2.2 KiB
JavaScript
Raw Normal View History

2018-04-02 09:21:16 +00:00
/* Test.it v 0.5.2 | MIT | https://github.com/n2geoff/testit */
;(function (root, factory) {
"use strict";
// support browser & commonjs
2018-04-02 09:21:16 +00:00
if (typeof module === "object" && module.exports) {
module.exports = factory(root.test);
} else {
root.test = factory(root.test);
}
2018-04-02 09:21:16 +00:00
}(this, function () {
"use strict";
2018-03-31 22:45:35 +00:00
const test = {
2018-04-02 09:21:16 +00:00
"_tests": {},
"run": function (next) {
let tests = this._tests;
// capture results
let failed = [];
let passed = [];
// loop through tests
2018-04-02 09:21:16 +00:00
Object.keys(tests).forEach(function (name) {
let test = tests[name];
// execute
try {
test();
passed.push(`\n+ ${name}`);
} catch (err) {
// TODO: include error stack option
// let back = err.stack.split('\n')[2];
// let re = /(?<=\()(.*?)(?=\))/g;
// let trace = re.exec(back)[0];
failed.push(`\n- ${name}`);
console.error(err);
// failed.push(`\n- ${name} - - ${trace}`);
}
2018-03-31 22:45:35 +00:00
});
// summary
2018-04-02 09:21:16 +00:00
if(typeof next === "function") {
return next({
pass: passed,
fail: failed
});
} else {
console.log(...passed, ...failed);
console.log(`\n# tests ${failed.length + passed.length} pass ${passed.length} fail ${failed.length}`);
return failed.length ? false : true;
}
},
2018-04-02 09:21:16 +00:00
"it": function (tests) {
this._tests = tests;
return this;
},
2018-04-02 09:21:16 +00:00
"assert": function (e, a) { if (e != a) throw Error(`expected ${e} == ${a}`); },
"equals": function (e, a) { if (e !== a) throw Error(`expected ${e} === ${a}`); },
"exists": function (v) { if (!v) throw Error(`exists value ${v}`); },
"pass": function () { return true; },
"fail": function (m) { throw Error(m); }
};
2018-03-31 22:45:35 +00:00
return test;
}));