@asdgf/core
Advanced tools
Comparing version 0.0.2 to 0.0.3
@@ -1,1 +0,1 @@ | ||
const y=(a=Date.now())=>()=>(Date.now()-a).toFixed(2)+"ms";globalThis.QUEUE=[];function m(a){const t={tests:[],suiteOnly:!1,only:[],...a},o=(e,i)=>{t.tests.push({name:e,handler:i})};return o.only=(e,i)=>{t.only.push({name:e,handler:i})},o.skip=(e,i)=>{t.tests.push({name:e,handler:()=>({skipped:!0})})},{suite:t,it:o,before:e=>{t.before=e},beforeEach:e=>{t.beforeEach=e},after:e=>{t.after=e},afterEach:e=>{t.afterEach=e}}}function E(a,t,o={}){const{suite:n,it:r,before:l,beforeEach:u,after:e,afterEach:i}=m({name:a,...o});QUEUE.push(n),t({it:r,before:l,beforeEach:u,after:e,afterEach:i})}const b=(a,t)=>E(a,t);b.only=(a,t)=>E(a,t,{suiteOnly:!0}),b.skip=(a,t)=>E(a,t,{skip:!0});async function w(a,{renderer:t}={}){const{name:o,only:n,tests:r,before:l,after:u,beforeEach:e,afterEach:i,skip:h}=a,f=n.length?n:r,s={name:o,skipped:0,failed:0,total:0,tests:[]};if(h)return t?.suiteStart?.({skip:h,name:o,only:n,tests:r}),s.skipped=f.length,s.tests=f.map(({name:p})=>({name:p,passed:!1,duration:"",error:!1,skipped:!0})),s.total=f.length,t?.suiteEnd?.(s),s;try{t?.suiteStart?.({skip:h,name:o,only:n,tests:r}),await l?.();for(const p of f){const c={name:p.name,passed:!0,skipped:!1,duration:"",error:!1},k=y();try{await e?.();const{skipped:d}=await p.handler()||{};c.duration=k(),c.skipped=!!d,c.passed=!d,c.skipped&&s.skipped++,await i?.()}catch(d){s.failed++,c.passed=!1,c.duration=k(),await i?.(),c.error={expected:d?.expects,...d,message:d.message,stack:d.stack}}finally{s.total++,t?.renderTest?.(c),s.tests.push(c)}}}finally{return await u?.(),t?.suiteEnd?.(s),s}}async function U({renderer:a}={}){const t=y(),o=[],n=[];let r=0,l=0,u=0;const e=QUEUE.filter(f=>f.suiteOnly),h=e.length>0?e:QUEUE;for(const f of h){const s=await w(f,{renderer:a});r+=s.total,l+=s.skipped,u+=s.failed,s.tests.forEach(({error:p})=>{p&&n.push(p)}),o.push(s)}return{status:"FINISHED",total:r,skipped:l,failed:u,errors:n,passed:u<1,duration:t(),results:o}}export{b as describe,U as executeTests}; | ||
const b=(s=Date.now())=>()=>(Date.now()-s).toFixed(2)+"ms";globalThis.QUEUE=[];function m(s){const t={tests:[],suiteOnly:!1,only:[],...s},o=(e,i)=>{t.tests.push({name:e,handler:i})};return o.only=(e,i)=>{t.only.push({name:e,handler:i})},o.skip=(e,i)=>{t.tests.push({name:e,handler:()=>({skipped:!0})})},{suite:t,it:o,before:e=>{t.before=e},beforeEach:e=>{t.beforeEach=e},after:e=>{t.after=e},afterEach:e=>{t.afterEach=e}}}function k(s,t,o={}){const{suite:n,it:r,before:l,beforeEach:u,after:e,afterEach:i}=m({name:s,...o});QUEUE.push(n),t({it:r,before:l,beforeEach:u,after:e,afterEach:i})}const w=(s,t)=>k(s,t);w.only=(s,t)=>k(s,t,{suiteOnly:!0}),w.skip=(s,t)=>k(s,t,{skip:!0});async function E(s="",t){try{await t?.()}catch(o){throw console.error(`Error in hook "${s}":`),new Error(o)}}async function g(s,{renderer:t}={}){const{name:o,only:n,tests:r,before:l,after:u,beforeEach:e,afterEach:i,skip:d}=s,f=n.length?n:r,a={name:o,skipped:0,failed:0,total:0,tests:[]};if(d)return t?.suiteStart?.({skip:d,name:o,only:n,tests:r}),a.skipped=f.length,a.tests=f.map(({name:p})=>({name:p,passed:!1,duration:"",error:!1,skipped:!0})),a.total=f.length,t?.suiteEnd?.(a),a;t?.suiteStart?.({skip:d,name:o,only:n,tests:r}),await E("before",l);for(const p of f){const c={name:p.name,passed:!0,skipped:!1,duration:"",error:!1},y=b();await E("beforeEach",e);try{const{skipped:h}=await p.handler()||{};c.duration=y(),c.skipped=!!h,c.passed=!h,c.skipped&&a.skipped++}catch(h){a.failed++,c.passed=!1,c.duration=y(),c.error={expected:h?.expects,...h,message:h.message,stack:h.stack}}finally{a.total++,t?.renderTest?.(c),a.tests.push(c)}await E("afterEach",i)}return await E("after",u),t?.suiteEnd?.(a),a}async function U({renderer:s}={}){const t=b(),o=[],n=[];let r=0,l=0,u=0;const e=QUEUE.filter(f=>f.suiteOnly),d=e.length>0?e:QUEUE;for(const f of d){const a=await g(f,{renderer:s});r+=a.total,l+=a.skipped,u+=a.failed,a.tests.forEach(({error:p})=>{p&&n.push(p)}),o.push(a)}return{status:"FINISHED",total:r,skipped:l,failed:u,errors:n,passed:u<1,duration:t(),results:o}}export{w as describe,U as executeTests}; |
{ | ||
"name": "@asdgf/core", | ||
"version": "0.0.2", | ||
"version": "0.0.3", | ||
"description": "A stupid simple way to test", | ||
@@ -11,3 +11,3 @@ "main": "index.js", | ||
"build": "esbuild src/index.js --minify --format=esm --outfile=index.js", | ||
"prepublish": "npm run build" | ||
"prepublishOnly": "npm run build" | ||
}, | ||
@@ -14,0 +14,0 @@ "repository": { |
@@ -49,2 +49,11 @@ const timer = (now = Date.now()) => () => (Date.now() - now).toFixed(2) + 'ms'; | ||
async function withErrorHandling(type = '', handler) { | ||
try { | ||
await handler?.(); | ||
} catch (e) { | ||
console.error(`Error in hook "${type}":`); | ||
throw new Error(e); | ||
} | ||
} | ||
/** | ||
@@ -78,46 +87,40 @@ * Runs the given tests in a suite, and its lifecycle hooks like before, beforeEach, after, afterEach | ||
try { | ||
renderer?.suiteStart?.({skip, name, only, tests}); | ||
await before?.(); | ||
renderer?.suiteStart?.({skip, name, only, tests}); | ||
await withErrorHandling('before', before); | ||
for (const test of testsToRun) { | ||
const testResult = { | ||
name: test.name, | ||
passed: true, | ||
skipped: false, | ||
duration: '', | ||
error: false | ||
} | ||
for (const test of testsToRun) { | ||
const testResult = { | ||
name: test.name, | ||
passed: true, | ||
skipped: false, | ||
duration: '', | ||
error: false | ||
} | ||
const time = timer(); | ||
try { | ||
await beforeEach?.(); | ||
const { skipped } = await test.handler() || {}; | ||
testResult.duration = time(); | ||
testResult.skipped = !!skipped; | ||
testResult.passed = !skipped; | ||
const time = timer(); | ||
await withErrorHandling('beforeEach', beforeEach); | ||
try { | ||
const { skipped } = await test.handler() || {}; | ||
testResult.duration = time(); | ||
testResult.skipped = !!skipped; | ||
testResult.passed = !skipped; | ||
if(testResult.skipped) testSuiteResult.skipped++; | ||
await afterEach?.(); | ||
} catch (err) { | ||
testSuiteResult.failed++; | ||
testResult.passed = false; | ||
testResult.duration = time(); | ||
await afterEach?.(); | ||
testResult.error = {expected: err?.expects, ...err, message: err.message, stack: err.stack}; | ||
} finally { | ||
testSuiteResult.total++; | ||
renderer?.renderTest?.(testResult); | ||
testSuiteResult.tests.push(testResult); | ||
} | ||
} | ||
} finally { | ||
await after?.(); | ||
renderer?.suiteEnd?.(testSuiteResult); | ||
return testSuiteResult; | ||
if(testResult.skipped) testSuiteResult.skipped++; | ||
} catch (err) { | ||
testSuiteResult.failed++; | ||
testResult.passed = false; | ||
testResult.duration = time(); | ||
testResult.error = {expected: err?.expects, ...err, message: err.message, stack: err.stack}; | ||
} finally { | ||
testSuiteResult.total++; | ||
renderer?.renderTest?.(testResult); | ||
testSuiteResult.tests.push(testResult); | ||
} | ||
await withErrorHandling('afterEach', afterEach); | ||
} | ||
await withErrorHandling('after', after); | ||
renderer?.suiteEnd?.(testSuiteResult); | ||
return testSuiteResult; | ||
} | ||
@@ -124,0 +127,0 @@ |
13701
245