diff --git a/dist/testit.js b/dist/testit.js index 69e0c46..06127f2 100644 --- a/dist/testit.js +++ b/dist/testit.js @@ -1,6 +1,7 @@ -/*! Test.it v 0.9.0 | MIT | https://github.com/n2geoff/testit */ +/*! Test.it v1.0.0 | MIT | https://github.com/n2geoff/testit */ const test = { "log": console.log, + "version": "v1.0.0", "_tests": {}, "run": function run(errors, next) { if(typeof errors !== "boolean") { @@ -43,46 +44,14 @@ const test = { this._tests = tests; return this; }, - "expects": function expects(val) { - return { - "to": { - "be": { - "a": (type) => { return test.expects(val).to.be.an(type); }, - "an": (type) => { - - if(["array"].indexOf(type) !== -1) { - if(val.constructor.name.toLowerCase() !== "array") { - throw new Error(`expected ${typeof val} to be an ${type}`); - } - - return true; - } - - if(typeof val !== type) { - throw new Error(`expected ${typeof val} to be an ${type}`); - } - }, - "ok": () => { return test.expects(val).to.exist(); }, - "like": (comp) => { - if(val != comp) { - throw new Error(`expected ${val} == ${comp}`); - } - } - }, - "equal": (comp) => { - if(val !== comp) { - throw new Error(`expected ${val} === ${comp}`); - } - }, - "exist": () => { - if(!val) { - throw new Error(`expected ${val} to be truthy`); - } - }, - "pass": () => { return true; }, - "fail": (msg) => { throw new Error(msg); } + "assert": (expression, msg) => { + try { + if(!expression) { + throw new Error(msg || "Assertion Failed"); } - }; + } catch (e) { + throw new Error(msg); + } } }; diff --git a/dist/testit.min.js b/dist/testit.min.js index d329b2e..58afdaa 100644 --- a/dist/testit.min.js +++ b/dist/testit.min.js @@ -1 +1 @@ -const test={log:console.log,_tests:{},run:function(t,e){"boolean"!=typeof t&&(e=t,t=!0);let r=this._tests||[],o=[],n=[];return Object.keys(r).forEach(e=>{let s=r[e];try{s(),n.push(`\n+OK ${e}`)}catch(r){t?o.push(`\n-ERR ${e} \n --- \n ${r.stack} \n ---`):o.push(`\n-ERR ${e}`)}}),"function"==typeof e?e({pass:n,fail:o}):(test.log(...n,...o),test.log(`\n# tests ${o.length+n.length} pass ${n.length} fail ${o.length}`),!o.length)},it:function(t){return this._tests=t,this},expects:function(t){return{to:{be:{a:e=>test.expects(t).to.be.an(e),an:e=>{if(-1!==["array"].indexOf(e)){if("array"!==t.constructor.name.toLowerCase())throw new Error(`expected ${typeof t} to be an ${e}`);return!0}if(typeof t!==e)throw new Error(`expected ${typeof t} to be an ${e}`)},ok:()=>test.expects(t).to.exist(),like:e=>{if(t!=e)throw new Error(`expected ${t} == ${e}`)}},equal:e=>{if(t!==e)throw new Error(`expected ${t} === ${e}`)},exist:()=>{if(!t)throw new Error(`expected ${t} to be truthy`)},pass:()=>!0,fail:t=>{throw new Error(t)}}}}};export default test; \ No newline at end of file +const test={log:console.log,version:"v1.0.0",_tests:{},run:function(t,e){"boolean"!=typeof t&&(e=t,t=!0);let s=this._tests||[],n=[],o=[];return Object.keys(s).forEach(e=>{let r=s[e];try{r(),o.push(`\n+OK ${e}`)}catch(s){t?n.push(`\n-ERR ${e} \n --- \n ${s.stack} \n ---`):n.push(`\n-ERR ${e}`)}}),"function"==typeof e?e({pass:o,fail:n}):(test.log(...o,...n),test.log(`\n# tests ${n.length+o.length} pass ${o.length} fail ${n.length}`),!n.length)},it:function(t){return this._tests=t,this},assert:(t,e)=>{try{if(!t)throw new Error(e||"Assertion Failed")}catch(t){throw new Error(e)}}};export default test; \ No newline at end of file diff --git a/dist/testit.umd.js b/dist/testit.umd.js index dbb6a19..8b8350a 100644 --- a/dist/testit.umd.js +++ b/dist/testit.umd.js @@ -4,9 +4,10 @@ (global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.umd = factory()); }(this, (function () { 'use strict'; -/*! Test.it v 0.9.0 | MIT | https://github.com/n2geoff/testit */ + /*! Test.it v1.0.0 | MIT | https://github.com/n2geoff/testit */ const test = { "log": console.log, + "version": "v1.0.0", "_tests": {}, "run": function run(errors, next) { if(typeof errors !== "boolean") { @@ -49,46 +50,14 @@ this._tests = tests; return this; }, - "expects": function expects(val) { - return { - "to": { - "be": { - "a": (type) => { return test.expects(val).to.be.an(type); }, - "an": (type) => { - - if(["array"].indexOf(type) !== -1) { - if(val.constructor.name.toLowerCase() !== "array") { - throw new Error(`expected ${typeof val} to be an ${type}`); - } - - return true; - } - - if(typeof val !== type) { - throw new Error(`expected ${typeof val} to be an ${type}`); - } - }, - "ok": () => { return test.expects(val).to.exist(); }, - "like": (comp) => { - if(val != comp) { - throw new Error(`expected ${val} == ${comp}`); - } - } - }, - "equal": (comp) => { - if(val !== comp) { - throw new Error(`expected ${val} === ${comp}`); - } - }, - "exist": () => { - if(!val) { - throw new Error(`expected ${val} to be truthy`); - } - }, - "pass": () => { return true; }, - "fail": (msg) => { throw new Error(msg); } + "assert": (expression, msg) => { + try { + if(!expression) { + throw new Error(msg || "Assertion Failed"); } - }; + } catch (e) { + throw new Error(msg); + } } }; diff --git a/dist/testit.umd.min.js b/dist/testit.umd.min.js index 7fa3ade..8f7c0dc 100644 --- a/dist/testit.umd.min.js +++ b/dist/testit.umd.min.js @@ -1 +1 @@ -!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define("umd",t):(e="undefined"!=typeof globalThis?globalThis:e||self).umd=t()}(this,function(){"use strict";const e={log:console.log,_tests:{},run:function(t,o){"boolean"!=typeof t&&(o=t,t=!0);let n=this._tests||[],r=[],s=[];return Object.keys(n).forEach(e=>{let o=n[e];try{o(),s.push(`\n+OK ${e}`)}catch(o){t?r.push(`\n-ERR ${e} \n --- \n ${o.stack} \n ---`):r.push(`\n-ERR ${e}`)}}),"function"==typeof o?o({pass:s,fail:r}):(e.log(...s,...r),e.log(`\n# tests ${r.length+s.length} pass ${s.length} fail ${r.length}`),!r.length)},it:function(e){return this._tests=e,this},expects:function(t){return{to:{be:{a:o=>e.expects(t).to.be.an(o),an:e=>{if(-1!==["array"].indexOf(e)){if("array"!==t.constructor.name.toLowerCase())throw new Error(`expected ${typeof t} to be an ${e}`);return!0}if(typeof t!==e)throw new Error(`expected ${typeof t} to be an ${e}`)},ok:()=>e.expects(t).to.exist(),like:e=>{if(t!=e)throw new Error(`expected ${t} == ${e}`)}},equal:e=>{if(t!==e)throw new Error(`expected ${t} === ${e}`)},exist:()=>{if(!t)throw new Error(`expected ${t} to be truthy`)},pass:()=>!0,fail:e=>{throw new Error(e)}}}}};return e}); \ No newline at end of file +!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define("umd",e):(t="undefined"!=typeof globalThis?globalThis:t||self).umd=e()}(this,function(){"use strict";const t={log:console.log,version:"v1.0.0",_tests:{},run:function(e,n){"boolean"!=typeof e&&(n=e,e=!0);let o=this._tests||[],s=[],i=[];return Object.keys(o).forEach(t=>{let n=o[t];try{n(),i.push(`\n+OK ${t}`)}catch(n){e?s.push(`\n-ERR ${t} \n --- \n ${n.stack} \n ---`):s.push(`\n-ERR ${t}`)}}),"function"==typeof n?n({pass:i,fail:s}):(t.log(...i,...s),t.log(`\n# tests ${s.length+i.length} pass ${i.length} fail ${s.length}`),!s.length)},it:function(t){return this._tests=t,this},assert:(t,e)=>{try{if(!t)throw new Error(e||"Assertion Failed")}catch(t){throw new Error(e)}}};return t}); \ No newline at end of file diff --git a/package.json b/package.json index 8173a63..686a1b5 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "testit", - "version": "0.9.0", + "version": "1.0.0", "description": "minimalistic testing library", "main": "src/testit.js", "directories": { diff --git a/src/testit.js b/src/testit.js index 9aab1a9..5ac7784 100644 --- a/src/testit.js +++ b/src/testit.js @@ -1,6 +1,7 @@ -/*! Test.it v 0.9.0 | MIT | https://github.com/n2geoff/testit */ +/*! Test.it v1.0.0 | MIT | https://github.com/n2geoff/testit */ const test = { "log": console.log, // eslint-disable-line + "version": "v1.0.0", "_tests": {}, "run": function run(errors, next) { // TODO: rewrite to allow a show errors flag (optional)