@asdgf/core
Advanced tools
Comparing version 0.0.3 to 0.0.4
@@ -1,1 +0,3 @@ | ||
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}; | ||
const y=(n=Date.now())=>()=>(Date.now()-n).toFixed(2)+"ms";globalThis.QUEUE=[];function k(n,s,i={}){const t={name:n,tests:[],suiteOnly:!1,only:[],...i},r=(e,o)=>{t.tests.push({name:e,handler:o})};r.only=(e,o)=>{t.only.push({name:e,handler:o})},r.skip=(e,o)=>{t.tests.push({name:e,handler:()=>({skipped:!0})})};const f=e=>{t.before=e},u=e=>{t.beforeEach=e},h=e=>{t.after=e},d=e=>{t.afterEach=e};QUEUE.push(t),s({it:r,before:f,beforeEach:u,after:h,afterEach:d})}const m=(n,s)=>{k(n,s)};m.only=(n,s)=>{k(n,s,{suiteOnly:!0})},m.skip=(n,s)=>{k(n,s,{skip:!0})};async function E(n="",s){try{await s?.()}catch(i){const t=new Error(i);throw t.message=`in hook "${n}" | ||
${i.message}`,t}}async function b(n,{renderer:s}={}){const{name:i,only:t,tests:r,before:f,after:u,beforeEach:h,afterEach:d,skip:e}=n,o=t.length?t:r,a={name:i,skipped:0,failed:0,total:0,tests:[]};if(e)return s?.suiteStart?.({skip:e,name:i,only:t,tests:r}),a.skipped=o.length,a.tests=o.map(({name:l})=>({name:l,passed:!1,duration:"",error:!1,skipped:!0})),a.total=o.length,s?.suiteEnd?.(a),a;s?.suiteStart?.({skip:e,name:i,only:t,tests:r}),await E("before",f);for(const l of o){const c={name:l.name,passed:!0,skipped:!1,duration:"",error:!1};await E("beforeEach",h);const w=y();try{const{skipped:p}=await l.handler()||{};c.skipped=!!p,c.passed=!p,c.skipped&&a.skipped++}catch(p){a.failed++,c.passed=!1,c.error={expected:p?.expects,...p,message:p.message,stack:p.stack}}finally{c.duration=w(),a.total++,s?.renderTest?.(c),a.tests.push(c)}await E("afterEach",d)}return await E("after",u),s?.suiteEnd?.(a),a}async function g({renderer:n}={}){const s=y(),i=[],t=[];let r=0,f=0,u=0;const h=QUEUE.filter(o=>o.suiteOnly),e=h.length>0?h:QUEUE;for(const o of e){const a=await b(o,{renderer:n});r+=a.total,f+=a.skipped,u+=a.failed,a.tests.forEach(({error:l})=>{l&&t.push(l)}),i.push(a)}return{status:"FINISHED",total:r,skipped:f,failed:u,errors:t,passed:u<1,duration:s(),results:i}}export{m as describe,g as executeTests}; |
{ | ||
"name": "@asdgf/core", | ||
"version": "0.0.3", | ||
"version": "0.0.4", | ||
"description": "A stupid simple way to test", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -13,12 +13,15 @@ const timer = (now = Date.now()) => () => (Date.now() - now).toFixed(2) + 'ms'; | ||
* @typedef {import('../types').Handler} Handler | ||
* @typedef {import('../types').Setup} Setup | ||
*/ | ||
/** | ||
* Creates the suite, and related functions for a suite | ||
* @param {Partial<Suite>} options | ||
* @return {Setup} setup | ||
* @param {string} name | ||
* @param {Handler} handler | ||
* @param {{ | ||
* suiteOnly?: boolean, | ||
* skip?: boolean | ||
* }} options | ||
*/ | ||
function setup(options) { | ||
const suite = { tests:[], suiteOnly: false, only: [], ...options }; | ||
function createSuite(name, handler, options = {}) { | ||
/** @type {Suite} */ | ||
const suite = { name, tests:[], suiteOnly: false, only: [], ...options }; | ||
@@ -36,7 +39,2 @@ const it = (name, handler) => { | ||
return { suite, it, before, beforeEach, after, afterEach }; | ||
} | ||
function createDescribe(name, handler, options = {}) { | ||
const {suite, it, before, beforeEach, after, afterEach } = setup({name, ...options}); | ||
QUEUE.push(suite); | ||
@@ -47,5 +45,5 @@ handler({it, before, beforeEach, after, afterEach}); | ||
/** @type {Describe} describe */ | ||
export const describe = (name, handler) => createDescribe(name, handler); | ||
describe.only = (name, handler) => createDescribe(name, handler, {suiteOnly: true}) | ||
describe.skip = (name, handler) => createDescribe(name, handler, {skip: true}) | ||
export const describe = (name, handler) => { createSuite(name, handler) }; | ||
describe.only = (name, handler) => { createSuite(name, handler, {suiteOnly: true}) }; | ||
describe.skip = (name, handler) => { createSuite(name, handler, {skip: true}) }; | ||
@@ -56,4 +54,5 @@ async function withErrorHandling(type = '', handler) { | ||
} catch (e) { | ||
console.error(`Error in hook "${type}":`); | ||
throw new Error(e); | ||
const err = new Error(e); | ||
err.message = `in hook "${type}"\n\n${e.message}`; | ||
throw err; | ||
} | ||
@@ -102,8 +101,7 @@ } | ||
const time = timer(); | ||
await withErrorHandling('beforeEach', beforeEach); | ||
const time = timer(); | ||
try { | ||
const { skipped } = await test.handler() || {}; | ||
testResult.duration = time(); | ||
testResult.skipped = !!skipped; | ||
@@ -116,6 +114,7 @@ testResult.passed = !skipped; | ||
testResult.passed = false; | ||
testResult.duration = time(); | ||
testResult.error = {expected: err?.expects, ...err, message: err.message, stack: err.stack}; | ||
} finally { | ||
testResult.duration = time(); | ||
testSuiteResult.total++; | ||
@@ -122,0 +121,0 @@ renderer?.renderTest?.(testResult); |
@@ -7,11 +7,2 @@ type Callback = () => void | Promise<void> | ||
export interface Setup { | ||
suite: Suite, | ||
it: It, | ||
before: Hook, | ||
beforeEach: Hook, | ||
after: Hook, | ||
afterEach: Hook, | ||
} | ||
export interface It { | ||
@@ -18,0 +9,0 @@ (name: string, handler: TestFn): void | Promise<void>, |
13269
237