testit/dist/testit.umd.js

67 lines
2.0 KiB
JavaScript
Raw Normal View History

2020-09-08 22:37:54 +00:00
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
typeof define === 'function' && define.amd ? define('umd', factory) :
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.umd = factory());
}(this, (function () { 'use strict';
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;
}
let tests = this._tests || [];
let failed = [];
let passed = [];
Object.keys(tests).forEach((name) => {
let test = tests[name];
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}`);
}
}
});
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;
},
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
}
};
return test;
})));