added way to supress error stack

This commit is contained in:
Geoff Doty 2018-12-02 04:30:08 -05:00
parent c4cf2ed933
commit c1d8f386eb
6 changed files with 36 additions and 11 deletions

View File

@ -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) **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** **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 in the Browser
- Works with CommonJS (aka NodeJS) - Works with CommonJS (aka NodeJS)
- Barely over a 100 lines - *Barely* over a 100 lines
- Single File - Single File
- No Dependicies - No Dependicies
- 2kb footprint (*before gzip*) - 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!** **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 ## Usage
@ -52,7 +53,7 @@ Error: just wanted to fail fast
# tests 2 pass 1 fail 1 # 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... 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. 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. A sample test runner is provided for both **HTML** and **NODE** in the `test/` directory; `run.html` and `run.js` respectfully.
## Methods ## Methods

14
dist/testit.js vendored
View File

@ -11,7 +11,11 @@
const test = { const test = {
"_tests": {}, "_tests": {},
"run": function run(next) { "run": function run(errors, next) {
if(typeof errors !== "boolean") {
next = errors;
errors = true;
}
let tests = this._tests; let tests = this._tests;
let failed = []; let failed = [];
@ -24,7 +28,13 @@
test(); test();
passed.push(`\n+OK ${name}`); passed.push(`\n+OK ${name}`);
} catch (err) { } 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}`);
}
} }
}); });

3
dist/testit.min.js vendored
View File

@ -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}); /*! 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});

View File

@ -12,7 +12,12 @@
const test = { const test = {
"_tests": {}, "_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; let tests = this._tests;
// capture results // capture results
@ -28,7 +33,13 @@
test(); test();
passed.push(`\n+OK ${name}`); passed.push(`\n+OK ${name}`);
} catch (err) { } 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}`);
}
} }
}); });

View File

@ -10,7 +10,7 @@
</head> </head>
<body> <body>
<script> <script>
test.run(function(r) { test.run(false, function(r) {
document.body.style.backgroundColor = ( document.body.style.backgroundColor = (
r.fail.length ? "#ff9999" : "#99ff99" r.fail.length ? "#ff9999" : "#99ff99"
); );