UNPKG

saintest

Version:
7 lines (6 loc) 6.71 kB
/*! * saintest v0.2.0 | MIT License * Copyright (c) 2025-present NOuSantx */ const e="",t=e=>`[38;5;${e}m`,o=35,r=203,n=245;function i(n,i){return`\n ${t(o)}+ Expected: ${JSON.stringify(i)}${e}\n ${t(r)}- Received: ${JSON.stringify(n)}${e}`}function s(e){return{toBe(t){if(e!==t)throw new Error(`Expected ${JSON.stringify(e)} to be ${JSON.stringify(t)}${i(e,t)}`)},toEqual(t){if(JSON.stringify(e)!==JSON.stringify(t))throw new Error(`Expected deep equality${i(e,t)}`)},toThrow(t){let o=!1,r=null;try{e()}catch(e){o=!0,r=e}if(!o)throw new Error("Expected function to throw an error");if(t&&r.message!==t)throw new Error(`Expected error message "${t}" but got "${r.message}"`)},toBeGreaterThan(t){if(!(e>t))throw new Error(`Expected ${e} to be greater than ${t}`)},toBeLessThan(t){if(!(e<t))throw new Error(`Expected ${e} to be less than ${t}`)},toContain(t){if(!e.includes(t))throw new Error(`Expected ${JSON.stringify(e)} to contain ${JSON.stringify(t)}`)},toHaveLength(t){if(e.length!==t)throw new Error(`Expected length of ${e.length} to be ${t}`)},toBeInstanceOf(t){if(!(e instanceof t))throw new Error(`Expected ${e} to be instance of ${t.name}`)},toBeTruthy(){if(!e)throw new Error(`Expected ${e} to be truthy`)},toBeFalsy(){if(e)throw new Error(`Expected ${e} to be falsy`)},toBeNull(){if(null!==e)throw new Error(`Expected ${e} to be null`)},toBeUndefined(){if(void 0!==e)throw new Error(`Expected ${e} to be undefined`)},toBeDefined(){if(void 0===e)throw new Error("Expected value to be defined")},toBeNaN(){if(!Number.isNaN(e))throw new Error(`Expected ${e} to be NaN`)},toMatch(t){if(!t.test(e))throw new Error(`Expected ${e} to match ${t}`)},not:{toBe(t){if(e===t)throw new Error(`Expected ${JSON.stringify(e)} not to be ${JSON.stringify(t)}`)},toEqual(t){if(JSON.stringify(e)===JSON.stringify(t))throw new Error(`Expected values not to be deeply equal${i(e,t)}`)},toBeInstanceOf(t){if(e instanceof t)throw new Error(`Expected ${e} not to be instance of ${t.name}`)},toMatch(t){if(t.test(e))throw new Error(`Expected ${e} not to match ${t}`)},toContain(t){if(e.includes(t))throw new Error(`Expected ${JSON.stringify(e)} not to contain ${JSON.stringify(t)}`)},toBeTruthy(){if(e)throw new Error(`Expected ${e} not to be truthy`)},toBeFalsy(){if(!e)throw new Error(`Expected ${e} not to be falsy`)},toBeNull(){if(null===e)throw new Error("Expected value not to be null")},toBeUndefined(){if(void 0===e)throw new Error("Expected value not to be undefined")},toBeDefined(){if(void 0!==e)throw new Error("Expected value to be undefined")},toBeNaN(){if(Number.isNaN(e))throw new Error("Expected value not to be NaN")},toHaveLength(t){if(e.length===t)throw new Error(`Expected length not to be ${t}`)},toBeGreaterThan(t){if(e>t)throw new Error(`Expected ${e} not to be greater than ${t}`)},toBeLessThan(t){if(e<t)throw new Error(`Expected ${e} not to be less than ${t}`)},toHaveProperty(t,o){const r=t.split(".");let n=e;try{for(const e of r)n=n[e];if(void 0===o)throw new Error(`Expected object not to have property "${t}"`);if(n===o)throw new Error(`Expected property "${t}" not to have value ${JSON.stringify(o)}`)}catch(e){return}},toThrow(t){try{e()}catch(e){if(!t)throw new Error("Expected function not to throw an error");if(e.message===t)throw new Error(`Expected function not to throw error "${t}"`)}},toBeCloseTo(t,o=2){const r=Math.pow(10,o);if(Math.round(e*r)===Math.round(t*r))throw new Error(`Expected ${e} not to be close to ${t} with precision of ${o} decimal points`)}},toHaveProperty(t,o){const r=t.split(".");let n=e;for(const e of r){if(!(e in n))throw new Error(`Expected object to have property "${t}"`);n=n[e]}if(void 0!==o&&n!==o)throw new Error(`Expected property "${t}" to have value ${JSON.stringify(o)}, got ${JSON.stringify(n)}`)},toBeCloseTo(t,o=2){const r=Math.pow(10,o);if(Math.round(e*r)!==Math.round(t*r))throw new Error(`Expected ${e} to be close to ${t} with precision of ${o} decimal points`)}}}const a=[],l={name:"Standalone Tests",tests:[],beforeEach:null,afterEach:null,beforeAll:null,afterAll:null};let c=null,f=0,$=0,h=0,w=0;function E(e,t){const o={name:e,tests:[],beforeEach:null,afterEach:null,beforeAll:null,afterAll:null};a.push(o);const r=c;c=o,t(),c=r}function u(e){c?c.beforeEach=e:l.beforeEach=e}function d(e){c?c.afterEach=e:l.afterEach=e}function p(e){c?c.beforeAll=e:l.beforeAll=e}function g(e){c?c.afterAll=e:l.afterAll=e}function b(e,t){const o={name:e,fn:t,skip:!1,only:!1,timeout:100};return c?c.tests.push(o):l.tests.push(o),{skip:()=>{o.skip=!0,h++},only:()=>{o.only=!0},timeout:e=>{o.timeout=e}}}function y(e,t){return b(e,t)}async function x(i,s){if(i.skip)return console.log(` ${t(214)}${e} ${t(n)}${i.name} (skipped)${e}`),{status:"skipped"};try{s.beforeEach&&await s.beforeEach();const r=Promise.race([Promise.resolve(i.fn()),new Promise(((e,t)=>setTimeout((()=>t(new Error(`Test timed out after ${i.timeout}ms`))),i.timeout)))]);return await r,s.afterEach&&await s.afterEach(),console.log(` ${t(o)}${e} ${t(7)}${i.name}${e}`),{status:"passed"}}catch(o){return console.log(` ${t(r)}${e} ${t(n)}${i.name}${e}`),console.log(`${t(n)} ${o.message}${e}`),{status:"failed"}}}async function m(i){if(0===i.tests.length)return;let s=0,a=0,l=0;console.log(`\n${t(o)}${i.name}${t(n)} [${i.tests.length} tests]${e}\n`);try{i.beforeAll&&await i.beforeAll();const c=i.tests.filter((e=>e.only)),h=c.length>0?c:i.tests;for(const e of h)switch((await x(e,i)).status){case"passed":s++,f++;break;case"failed":a++,$++;break;case"skipped":l++}i.afterAll&&await i.afterAll();const w=s+a+l,E=(s/(w-l)*100).toFixed(2);console.log("\n Suite Summary:"),console.log(` ${t(o)}${E}%${e} ${t(n)}passing${e}`),console.log(` ${t(o)}${s} passed${e} · ${t(r)}${a} failed${e} · ${t(214)}${l} skipped${e} · ${t(n)}${w} total${e}`)}catch(o){console.error(`${t(r)}Suite Error: ${o.message}${e}`)}}async function N(){w=performance.now(),l.tests.length>0&&await m(l);for(const e of a)await m(e);const i=((performance.now()-w)/1e3).toFixed(3),s=f+$+h,c=(f/(s-h)*100).toFixed(2);(a.length>0||l.tests.length>0)&&(console.log("\nFinal Test Results:"),console.log(`${t(o)}${c}%${e} ${t(n)}of all tests passing${e}`),console.log(`${t(o)}${f} passed${e} · ${t(r)}${$} failed${e} · ${t(214)}${h} skipped${e} · ${t(n)}${s} total${e}`),console.log(`${t(n)}Total Time: ${i}s${e}\n`))}var v={expect:s,it:b,test:y,describe:E,beforeEach:u,afterEach:d,beforeAll:p,afterAll:g,run:N,testSuites:a,defaultSuite:l,currentSuite:c};export{g as afterAll,d as afterEach,p as beforeAll,u as beforeEach,c as currentSuite,v as default,l as defaultSuite,E as describe,s as expect,b as it,N as run,y as test,a as testSuites}; //# sourceMappingURL=index.esm.min.js.map