saintest
Version:
Lightweight testing framework
7 lines (6 loc) • 7.09 kB
JavaScript
/*!
* saintest v0.2.0 | MIT License
* Copyright (c) 2025-present NOuSantx
*/
;Object.defineProperty(exports,"__esModule",{value:!0});const e="[0m",t=e=>`[38;5;${e}m`,o=35,r=203,n=245;function s(n,s){return`\n ${t(o)}+ Expected: ${JSON.stringify(s)}${e}\n ${t(r)}- Received: ${JSON.stringify(n)}${e}`}function i(e){return{toBe(t){if(e!==t)throw new Error(`Expected ${JSON.stringify(e)} to be ${JSON.stringify(t)}${s(e,t)}`)},toEqual(t){if(JSON.stringify(e)!==JSON.stringify(t))throw new Error(`Expected deep equality${s(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${s(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=[],c={name:"Standalone Tests",tests:[],beforeEach:null,afterEach:null,beforeAll:null,afterAll:null};exports.currentSuite=null;let l=0,f=0,u=0,$=0;function p(e,t){const o={name:e,tests:[],beforeEach:null,afterEach:null,beforeAll:null,afterAll:null};a.push(o);const r=exports.currentSuite;exports.currentSuite=o,t(),exports.currentSuite=r}function h(e){exports.currentSuite?exports.currentSuite.beforeEach=e:c.beforeEach=e}function w(e){exports.currentSuite?exports.currentSuite.afterEach=e:c.afterEach=e}function E(e){exports.currentSuite?exports.currentSuite.beforeAll=e:c.beforeAll=e}function d(e){exports.currentSuite?exports.currentSuite.afterAll=e:c.afterAll=e}function x(e,t){const o={name:e,fn:t,skip:!1,only:!1,timeout:100};return exports.currentSuite?exports.currentSuite.tests.push(o):c.tests.push(o),{skip:()=>{o.skip=!0,u++},only:()=>{o.only=!0},timeout:e=>{o.timeout=e}}}function g(e,t){return x(e,t)}async function b(s,i){if(s.skip)return console.log(` ${t(214)}○${e} ${t(n)}${s.name} [3m(skipped)${e}`),{status:"skipped"};try{i.beforeEach&&await i.beforeEach();const r=Promise.race([Promise.resolve(s.fn()),new Promise(((e,t)=>setTimeout((()=>t(new Error(`Test timed out after ${s.timeout}ms`))),s.timeout)))]);return await r,i.afterEach&&await i.afterEach(),console.log(` ${t(o)}✓${e} ${t(7)}${s.name}${e}`),{status:"passed"}}catch(o){return console.log(` ${t(r)}✗${e} ${t(n)}${s.name}${e}`),console.log(`${t(n)} ${o.message}${e}`),{status:"failed"}}}async function y(s){if(0===s.tests.length)return;let i=0,a=0,c=0;console.log(`\n${t(o)}${s.name}${t(n)} [${s.tests.length} tests]${e}\n`);try{s.beforeAll&&await s.beforeAll();const u=s.tests.filter((e=>e.only)),$=u.length>0?u:s.tests;for(const e of $)switch((await b(e,s)).status){case"passed":i++,l++;break;case"failed":a++,f++;break;case"skipped":c++}s.afterAll&&await s.afterAll();const p=i+a+c,h=(i/(p-c)*100).toFixed(2);console.log("\n Suite Summary:"),console.log(` ${t(o)}${h}%${e} ${t(n)}passing${e}`),console.log(` ${t(o)}${i} passed${e} · ${t(r)}${a} failed${e} · ${t(214)}${c} skipped${e} · ${t(n)}${p} total${e}`)}catch(o){console.error(`${t(r)}Suite Error: ${o.message}${e}`)}}async function S(){$=performance.now(),c.tests.length>0&&await y(c);for(const e of a)await y(e);const s=((performance.now()-$)/1e3).toFixed(3),i=l+f+u,p=(l/(i-u)*100).toFixed(2);(a.length>0||c.tests.length>0)&&(console.log("\nFinal Test Results:"),console.log(`${t(o)}${p}%${e} ${t(n)}of all tests passing${e}`),console.log(`${t(o)}${l} passed${e} · ${t(r)}${f} failed${e} · ${t(214)}${u} skipped${e} · ${t(n)}${i} total${e}`),console.log(`${t(n)}Total Time: ${s}s${e}\n`))}var m={expect:i,it:x,test:g,describe:p,beforeEach:h,afterEach:w,beforeAll:E,afterAll:d,run:S,testSuites:a,defaultSuite:c,currentSuite:exports.currentSuite};exports.afterAll=d,exports.afterEach=w,exports.beforeAll=E,exports.beforeEach=h,exports.default=m,exports.defaultSuite=c,exports.describe=p,exports.expect=i,exports.it=x,exports.run=S,exports.test=g,exports.testSuites=a;
//# sourceMappingURL=index.min.cjs.map