diff --git a/README.md b/README.md index 5f5494a..9ac63d0 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ **Test.it** is a small testing library for people that want to live in code, not in tests. No over engineering here. Inspired by the simplicity of libraries like [Tape](https://github.com/substack/tape),but the implementation ideas of things like [Expect](https://github.com/Automattic/expect.js) and [TinyTest](https://github.com/joewalnes/jstinytest) -This is probally not a *cure-all* testing solution, if you want something more robust checkout [Jasmine](), [Tape]() or [Mocha]() -- this is to... +This is probally not a *cure-all* testing solution, if you want something more robust checkout [Jasmine](https://jasmine.github.io/), [Tape](https://github.com/substack/tape) or [Mocha](https://mochajs.org/) -- this is to... **Test small things, with small things** @@ -12,7 +12,7 @@ This is probally not a *cure-all* testing solution, if you want something more r - Works in the Browser - Works with CommonJS (aka NodeJS) -- Barely over a 100 lines +- *Barely* over a 100 lines - Single File - No Dependicies - 2kb footprint (*before gzip*) @@ -21,7 +21,8 @@ This is probally not a *cure-all* testing solution, if you want something more r **No Bloat Here!** -- [Download Now Available](https://raw.githubusercontent.com/n2geoff/testit/master/src/testit.min.js) +- [Download Here](https://raw.githubusercontent.com/n2geoff/testit/master/src/testit.js) +- [Or Minified Version Here](https://raw.githubusercontent.com/n2geoff/testit/master/src/testit.min.js) ## Usage @@ -52,7 +53,7 @@ Error: just wanted to fail fast # tests 2 pass 1 fail 1 ``` -A `+OK` will proceed test lines that *pass* and a `-ERR` for those that *fail*, the error stack is included after the failing test wrapped in `---` +A `+OK` will proceed test lines that *pass* and a `-ERR` for those that *fail*, An error stack is included by default after the failing test wrapped in `---`. You can suppress outputing the error stack by passing `false` as an argument to `run()`, ie `run(false)`. You can, however, write your own custom test runner... @@ -89,6 +90,8 @@ If using the optional `next` param will return results as JSON From this object you can easily find the number of tests ran `pass.length`, number of failed tests `fail.length` or the total test count by adding the two. Simple. +> REMEMBER: you can bypass error output too + A sample test runner is provided for both **HTML** and **NODE** in the `test/` directory; `run.html` and `run.js` respectfully. ## Methods diff --git a/dist/testit.js b/dist/testit.js index bbd3a87..27ff03b 100644 --- a/dist/testit.js +++ b/dist/testit.js @@ -11,7 +11,11 @@ const test = { "_tests": {}, - "run": function run(next) { + "run": function run(errors, next) { + if(typeof errors !== "boolean") { + next = errors; + errors = true; + } let tests = this._tests; let failed = []; @@ -24,7 +28,13 @@ test(); passed.push(`\n+OK ${name}`); } catch (err) { - failed.push(`\n-ERR ${name} \n --- \n ${err.stack} \n ---`); + if (errors) { + console.log('ERRORS: YES'); + failed.push(`\n-ERR ${name} \n --- \n ${err.stack} \n ---`); + } else { + console.log('ERRORS: NO'); + failed.push(`\n-ERR ${name}`); + } } }); diff --git a/dist/testit.min.js b/dist/testit.min.js index 1735f54..d38a194 100644 --- a/dist/testit.min.js +++ b/dist/testit.min.js @@ -1 +1,2 @@ -!function(t,e){"use strict";"object"==typeof module&&module.exports?module.exports=e(t.test):t.test=e(t.test)}(this,function(){"use strict";const t={_tests:{},run:function(t){let e=this._tests,r=[],o=[];return Object.keys(e).forEach(t=>{let n=e[t];try{n(),o.push(`\n+OK ${t}`)}catch(e){r.push(`\n-ERR ${t} \n --- \n ${e.stack} \n ---`)}}),"function"==typeof t?t({pass:o,fail:r}):(console.log(...o,...r),console.log(`\n# tests ${r.length+o.length} pass ${o.length} fail ${r.length}`),!r.length)},it:function(t){return this._tests=t,this},expects:function(e){return{to:{be:{a:r=>t.expects(e).to.be.an(r),an:t=>{if(-1!==["array"].indexOf(t)){if("array"!==e.constructor.name.toLowerCase())throw new Error(`expected ${typeof e} to be an ${t}`);return!0}if(typeof e!==t)throw new Error(`expected ${typeof e} to be an ${t}`)},like:t=>{if(e!=t)throw new Error(`expected ${e} == ${t}`)}},equal:t=>{if(e!==t)throw new Error(`expected ${e} === ${t}`)},exist:()=>{if(!e)throw new Error(`expected ${e} to be truthy`)},pass:()=>!0,fail:t=>{throw new Error(t)}}}}};return t}); \ No newline at end of file +/*! Test.it v 0.8.0 | MIT | https://github.com/n2geoff/testit */ +!function(t,e){"use strict";"object"==typeof module&&module.exports?module.exports=e(t.test):t.test=e(t.test)}(this,function(){"use strict";const t={_tests:{},run:function(t,e){"boolean"!=typeof t&&(e=t,t=!0);let o=this._tests,n=[],r=[];return Object.keys(o).forEach(e=>{let s=o[e];try{s(),r.push(`\n+OK ${e}`)}catch(o){t?(console.log("ERRORS: YES"),n.push(`\n-ERR ${e} \n --- \n ${o.stack} \n ---`)):(console.log("ERRORS: NO"),n.push(`\n-ERR ${e}`))}}),"function"==typeof e?e({pass:r,fail:n}):(console.log(...r,...n),console.log(`\n# tests ${n.length+r.length} pass ${r.length} fail ${n.length}`),!n.length)},it:function(t){return this._tests=t,this},expects:function(e){return{to:{be:{a:o=>t.expects(e).to.be.an(o),an:t=>{if(-1!==["array"].indexOf(t)){if("array"!==e.constructor.name.toLowerCase())throw new Error(`expected ${typeof e} to be an ${t}`);return!0}if(typeof e!==t)throw new Error(`expected ${typeof e} to be an ${t}`)},like:t=>{if(e!=t)throw new Error(`expected ${e} == ${t}`)}},equal:t=>{if(e!==t)throw new Error(`expected ${e} === ${t}`)},exist:()=>{if(!e)throw new Error(`expected ${e} to be truthy`)},pass:()=>!0,fail:t=>{throw new Error(t)}}}}};return t}); \ No newline at end of file diff --git a/src/testit.js b/src/testit.js index fa87ba6..1722638 100644 --- a/src/testit.js +++ b/src/testit.js @@ -12,7 +12,12 @@ const test = { "_tests": {}, - "run": function run(next) { + "run": function run(errors, next) { + // rewrite to allow a show errors flag (optional) + if(typeof errors !== "boolean") { + next = errors; + errors = true; + } let tests = this._tests; // capture results @@ -28,7 +33,13 @@ test(); passed.push(`\n+OK ${name}`); } catch (err) { - failed.push(`\n-ERR ${name} \n --- \n ${err.stack} \n ---`); + if (errors) { + console.log('ERRORS: YES'); + failed.push(`\n-ERR ${name} \n --- \n ${err.stack} \n ---`); + } else { + console.log('ERRORS: NO'); + failed.push(`\n-ERR ${name}`); + } } }); diff --git a/test/run.html b/test/run.html index 3bbb66e..50d4b35 100644 --- a/test/run.html +++ b/test/run.html @@ -10,7 +10,7 @@