UNPKG

manten

Version:

満点 - Lightweight testing library for Node.js

3 lines (2 loc) 5.97 kB
import{expect as se}from"expect";let u=!0;const d=typeof self<"u"?self:typeof window<"u"?window:typeof global<"u"?global:{};let h=0;if(d.process&&d.process.env&&d.process.stdout){const{FORCE_COLOR:t,NODE_DISABLE_COLORS:e,NO_COLOR:n,TERM:r,COLORTERM:s}=d.process.env;e||n||t==="0"?u=!1:t==="1"||t==="2"||t==="3"?u=!0:r==="dumb"?u=!1:"CI"in d.process.env&&["TRAVIS","CIRCLECI","APPVEYOR","GITLAB_CI","GITHUB_ACTIONS","BUILDKITE","DRONE"].some(c=>c in d.process.env)?u=!0:u=process.stdout.isTTY,u&&(process.platform==="win32"||s&&(s==="truecolor"||s==="24bit")?h=3:r&&(r.endsWith("-256color")||r.endsWith("256"))?h=2:h=1)}let $={enabled:u,supportLevel:h};function y(t,e,n=1){const r=`\x1B[${t}m`,s=`\x1B[${e}m`,c=new RegExp(`\\x1b\\[${e}m`,"g");return o=>$.enabled&&$.supportLevel>=n?r+(""+o).replace(c,r)+s:""+o}const T=y(2,22),D=y(31,39),x=y(32,39),E=y(33,39);function _(t){if(typeof t!="number")throw new TypeError("Expected a number");const e=t>0?Math.floor:Math.ceil;return{days:e(t/864e5),hours:e(t/36e5)%24,minutes:e(t/6e4)%60,seconds:e(t/1e3)%60,milliseconds:e(t)%1e3,microseconds:e(t*1e3)%1e3,nanoseconds:e(t*1e6)%1e3}}const j=(t,e)=>e===1?t:`${t}s`,M=1e-7;function p(t,e={}){if(!Number.isFinite(t))throw new TypeError("Expected a finite number");e.colonNotation&&(e.compact=!1,e.formatSubMilliseconds=!1,e.separateMilliseconds=!1,e.verbose=!1),e.compact&&(e.secondsDecimalDigits=0,e.millisecondsDecimalDigits=0);const n=[],r=(o,i)=>{const l=Math.floor(o*10**i+M);return(Math.round(l)/10**i).toFixed(i)},s=(o,i,l,a)=>{if((n.length===0||!e.colonNotation)&&o===0&&!(e.colonNotation&&l==="m"))return;a=(a||o||"0").toString();let w,g;if(e.colonNotation){w=n.length>0?":":"",g="";const O=a.includes(".")?a.split(".")[0].length:a.length,P=n.length>0?2:1;a="0".repeat(Math.max(0,P-O))+a}else w="",g=e.verbose?" "+j(i,o):l;n.push(w+a+g)},c=_(t);if(s(Math.trunc(c.days/365),"year","y"),s(c.days%365,"day","d"),s(c.hours,"hour","h"),s(c.minutes,"minute","m"),e.separateMilliseconds||e.formatSubMilliseconds||!e.colonNotation&&t<1e3)if(s(c.seconds,"second","s"),e.formatSubMilliseconds)s(c.milliseconds,"millisecond","ms"),s(c.microseconds,"microsecond","\xB5s"),s(c.nanoseconds,"nanosecond","ns");else{const o=c.milliseconds+c.microseconds/1e3+c.nanoseconds/1e6,i=typeof e.millisecondsDecimalDigits=="number"?e.millisecondsDecimalDigits:0,l=o>=1?Math.round(o):Math.ceil(o),a=i?o.toFixed(i):l;s(Number.parseFloat(a),"millisecond","ms",a)}else{const o=t/1e3%60,i=typeof e.secondsDecimalDigits=="number"?e.secondsDecimalDigits:1,l=r(o,i),a=e.keepDecimalsOnWholeSeconds?l:l.replace(/\.0+$/,"");s(Number.parseFloat(a),"second","s",a)}if(n.length===0)return"0"+(e.verbose?" milliseconds":"ms");if(e.compact)return n[0];if(typeof e.unitCount=="number"){const o=e.colonNotation?"":" ";return n.slice(0,Math.max(e.unitCount,1)).join(o)}return e.colonNotation?n.join(""):n.join(" ")}const f=` `,{log:F,error:m}=console,V=x("\u2714"),B=D("\u2716"),v=E("\u2022"),I=({startTime:t,timeout:e,endTime:n})=>{const r=(n||Date.now())-t;let s=p(r);return e&&(s+=` / ${p(e)}`),r<50?"":` ${T(`(${s})`)}`},G=t=>{const{title:e,error:n,attempt:r,retry:s}=t;let c=`${e+I(t)}`;s>1&&(c+=T(` (${r}/${s})`)),n?m(`${B} ${c}`):F(`${V} ${c}`)},U=t=>{if(t.length===0)return;const e=[];let n=0,r=0,s,c;for(const i of t)i.startTime&&(!s||s>i.startTime)&&(s=i.startTime),i.endTime===void 0?e.push(i):((!c||c<i.endTime)&&(c=i.endTime),i.error?r+=1:n+=1);let o="";if(e.length>0){for(const i of e)o+=`${f}${v} ${i.title+I(i)}`;o+=f}o+=`${f}${T(p(c-s))}`,o+=f+(n>0?x:T)(`${n.toLocaleString()} passed`),r>0&&(o+=f+D(`${r.toLocaleString()} failed`)),e.length>0&&(o+=f+E(`${e.length.toLocaleString()} pending`)),o+=f,F(o)},W=()=>{let t,e;const n=new Promise((r,s)=>{t=r,e=s});return Object.assign(n,{resolve:t,reject:e})},Y=t=>{const e=W(),n=setTimeout(()=>e.reject(new Error(`Timeout: ${t}ms`)),t);return Object.assign(e,{timeoutId:n})},z=(t,e)=>{if(!t||!("then"in t)||e===void 0||e===0)return t;const n=Y(e);return Promise.race([t,n]).finally(()=>{clearTimeout(n.timeoutId)})},R=t=>{const e=[];return{addHook:s=>{e.push(s)},runHooks:async(...s)=>{for(const c of e)try{await c(...s)}catch(o){await t(o)}}}},J=async(t,e)=>{for(let n=1;n<=e;n+=1)try{await t(n);return}catch(r){if(n===e)throw r}},K=async t=>{const{testFunction:e,timeout:n}=t,r=R(o=>{m("[onTestFail]",t.title),m(o)}),s=async o=>(o&&typeof o=="object"&&"matcherResult"in o&&o.constructor.name==="JestAssertionError"&&delete o.matcherResult,m(o),await r.runHooks(o),o),c=R(async o=>{const i=await s(o);t.error||(t.error=i)});try{await J(async o=>{t.attempt=o,t.startTime=Date.now();try{await z(e({onTestFail:r.addHook,onTestFinish:c.addHook}),n)}catch(i){throw await s(i),i}finally{await c.runHooks(),t.endTime=Date.now()}},t.retry)}catch(o){t.error=o,process.exitCode=1}finally{G(t)}},N=[];process.on("exit",()=>{U(N)});const L=(t,e)=>async(n,r,s)=>{t&&(n=`${t} ${n}`);const c={title:n,testFunction:r,retry:1};s!==void 0&&(typeof s=="number"?c.timeout=s:(c.timeout=s?.timeout,s?.retry&&(c.retry=s?.retry))),N.push(c);const o=K(c);e&&e.pendingTests.push(o),await o},Z=t=>("default"in t&&(t=t.default),"default"in t&&(t=t.default),t),k=(t,e)=>(n,...r)=>{const s=b(t);return s.run(async()=>Z(await n).apply(s,r),e)},q=async t=>{for(;t.length>0;){const e=t.splice(0);await Promise.all(e)}},b=t=>{const e={pendingTests:[],callbacks:{onFinish:[]},run:async(n,r)=>{try{const s=(async()=>{await n(e.api),await q(e.pendingTests)})();r&&r.pendingTests.push(s),await s}catch(s){m(s),process.exitCode=1}finally{for(const s of e.callbacks.onFinish)try{await s()}catch(c){m(c),process.exitCode=1}}}};return e.api={test:t?L(`${t} \u203A`,e):S,describe:t?C(`${t} \u203A`,e):A,runTestSuite:t?k(t,e):H,onFinish:n=>{e.callbacks.onFinish.push(n)}},e},C=(t,e)=>async(n,r)=>{t&&(n=`${t} ${n}`),await b(n).run(r,e)},S=L(),A=C(),H=k(),Q=b(),X=t=>async function(...e){await t((this||Q).api,...e)};export{A as describe,se as expect,H as runTestSuite,S as test,X as testSuite};