UNPKG

@currents/jest

Version:

Currents reporter for Jest

6 lines 10.2 kB
"use strict";var wt=Object.create;var w=Object.defineProperty;var It=Object.getOwnPropertyDescriptor;var kt=Object.getOwnPropertyNames;var Dt=Object.getPrototypeOf,bt=Object.prototype.hasOwnProperty;var Rt=(t,e)=>{for(var s in e)w(t,s,{get:e[s],enumerable:!0})},H=(t,e,s,r)=>{if(e&&typeof e=="object"||typeof e=="function")for(let n of kt(e))!bt.call(t,n)&&n!==s&&w(t,n,{get:()=>e[n],enumerable:!(r=It(e,n))||r.enumerable});return t};var u=(t,e,s)=>(s=t!=null?wt(Dt(t)):{},H(e||!t||!t.__esModule?w(s,"default",{value:t,enumerable:!0}):s,t)),jt=t=>H(w({},"__esModule",{value:!0}),t);var Pt={};Rt(Pt,{default:()=>C});module.exports=jt(Pt);var es=require("source-map-support/register");var yt=require("path");var N=require("jest-cli"),K=require("yargs/helpers"),_=u(require("yargs/yargs"));function B(){let t=(0,_.default)((0,K.hideBin)(process.argv)).options(N.yargsOptions).parse();return{...Object.keys(t).filter(s=>s in N.yargsOptions).reduce((s,r)=>(s[r]=t[r],s),{}),_:t._,$0:t.$0}}var U=u(require("debug")),p=(0,U.default)("currents-jest");var g=class{constructor(){this.promise=new Promise((e,s)=>{this.resolve=e,this.reject=s})}};var A=u(require("fs-extra")),E=require("path"),Y=require("uuid");var Lt=[];function q(t){Lt.push(t)}var f=u(require("chalk")),F=u(require("util"));var G=[];var V=(...t)=>{let e=F.default.format(...t);q(e),console.log(e)},I=V;var $=(...t)=>{let e=F.default.format(...t);return G.push(e),p("ERROR: ",e),V(f.default.bgRed.white(" ERROR "),e)};var ee=f.default.cyan,se=f.default.blueBright,re=f.default.red,oe=f.default.yellow,ne=f.default.green,ie=f.default.gray,ae=f.default.white,ce=f.default.black,pe=f.default.magenta,le=f.default.dim,ue=f.default.bold;function Nt(){let t=new Date().toISOString().replace(/[:.]/g,"-"),e=(0,Y.v4)();return`${t}-${e}`}async function z(t,e){let s=Nt(),r=(0,E.join)(t,e,s);return k(r)}async function k(t){try{return await A.default.ensureDir(t),p("Folder created",t),t}catch(e){throw $(`Failed to create folder at ${t}:`,e),e}}async function J(t,e,s){let r=(0,E.join)(t,e);try{return await A.default.writeFile(r,s,"utf8"),p("File created",r),r}catch(n){throw $(`Error writing file at ${r}:`,n),n}}var Q=require("@babel/code-frame"),X=u(require("chalk")),O=u(require("fs-extra")),h=u(require("path")),Z=u(require("stack-utils")),tt=u(require("url"));function Ft(t,e){return{column:t.column,file:et(t.file,e),line:t.line}}function $t(t){return t.includes(`${h.default.sep}node_modules${h.default.sep}`)}function et(t,e){return h.default.relative(t,e)||h.default.basename(e)}function At(t){let e=t.split(` `),s=e.findIndex(o=>o.startsWith(" at "));s===-1&&(s=e.length);let r=e.slice(0,s).join(` `),n=e.slice(s),i;for(let o of n){let{frame:a,fileName:m}=Jt(o);if(!(!a||!m)&&!$t(m)){i={file:m,column:a.column||0,line:a.line||0};break}}return{message:r,stackLines:n,location:i}}var Et=new Z.default;function Jt(t){let e=Et.parseLine(t);if(!e)return{frame:null,fileName:null};let s=null;return e.file&&(s=e.file.startsWith("file://")?tt.default.fileURLToPath(e.file):h.default.resolve(process.cwd(),e.file)),{frame:e,fileName:s}}function st(t,e,s,r){let n=e.stack,i=[],o;if(n){let a=At(n);if(i.push(a.message),o=a.location,o){try{!e.snippet&&(!r||O.default.realpathSync(`${t}/${r}`)!==o.file)&&(i.push(""),i.push(X.default.gray(" at ")+`${et(t,o.file)}:${o.line}`))}catch(m){p(`No file found: ${JSON.stringify(m)}`)}if(i.push(""),e.snippet)i.push(e.snippet);else try{let m=O.default.readFileSync(o.file,"utf8"),v=(0,Q.codeFrameColumns)(m,{start:o},{highlightCode:s});i.push(v)}catch{}}i.push(""),i.push(a.stackLines.join(` `))}else e.message?i.push(e.message):e.value&&i.push(e.value);return{location:o,message:i.join(` `)}}function rt(t,e){return{message:t.message,stack:t.stack,value:t.value,snippet:t.snippet,location:t.location?Ft(t.location,e):void 0}}var ot=u(require("crypto"));function nt(t){return ot.createHash("sha256").update(t).digest("base64").slice(0,8).replace(/\+/g,"-").replace(/\//g,"_").replace(/=+$/,"")}var D=u(require("path"));function it(t,e){return Ot(D.default.relative(e,t))}function Ot(t){return t.split(D.default.sep).join(D.default.posix.sep)}var at=require("lodash"),ct=u(require("crypto"));function pt(t){return t.context.config.displayName?.name??t.context.config.id}function y(t){return[...t.ancestorTitles,t.title]}function x(t){return it(t.path,t.context.config.rootDir)}function b(t,e){let s=y(e),r=x(t),n=s.join(" ")+r;return ct.default.createHash("sha256").update(n).digest("hex").substring(0,16)}function M(t){switch(t){case"passed":return"passed";case"failed":return"failed";case"pending":case"todo":return"pending";default:throw new Error("Invalid Jest test case status")}}function lt(t){switch(t){case"passed":return"passed";case"failed":return"failed";case"pending":case"todo":return"skipped";default:throw new Error("Invalid Jest test case status")}}function ut(t){switch(t){case"pending":case"todo":return"skipped";default:return"passed"}}function ft(t){return t.length===0?"failed":((0,at.maxBy)(t,s=>s.invocations??0)??t[t.length-1]).status}function mt(t){return(t?.invocations??1)-1}function gt(t){let e=t.map(s=>s.status);return t.length>1&&e.includes("failed")&&e.includes("passed")}var dt=u(require("path")),ht=u(require("fs"));function xt(){return Mt("jest")}function Mt(t){try{let e=require.resolve(dt.default.join(t,"package.json")),s=ht.default.readFileSync(e,"utf8");return JSON.parse(s).version}catch(e){return p('Failed to obtain the package version "%s": %o',t,e),null}}var St=require("lodash");function Tt(t){let e=B();return{framework:"jest",frameworkVersion:xt(),cliArgs:{options:(0,St.omit)(e,"_","$0"),args:e._},frameworkConfig:t}}var C=class{constructor(e,s){this.globalConfig=e;this.options=s;this.reportDir="";this.instancesDir="";this.specInfo={};this.projectBySpecMap={};this.specsCount=0;this.processedSpecsCount=0;this.reportDirDeferred=new g;this.specInfoDeferred={};this.testCaseDeferred={};this.resultsDeferred={};this.rootDir=this.globalConfig.rootDir}async onRunStart(e,s){p("Run started"),this.specsCount=e.numTotalTestSuites,this.reportDir=this.options?.reportDir?await k(this.options.reportDir):await z(this.rootDir,".currents"),I("[currents]: Run started"),I("[currents]: Report directory is set to - %s",this.reportDir),this.instancesDir=await k((0,yt.join)(this.reportDir,"instances"));let r=Tt(this.globalConfig);p("Report config:",r),await J(this.reportDir,"config.json",JSON.stringify(r)),this.reportDirDeferred.resolve()}async onTestFileStart(e){let s=x(e),r=pt(e);this.projectBySpecMap[s]=r,await this.reportDirDeferred.promise;let n=R(r,s);this.specInfo[n]={projectId:r,specName:s,testCaseList:{},specResult:null},this.specInfoDeferred[n]=new g,this.specInfoDeferred[n].resolve(),p("Spec execution started [%s]: %o",s,this.specInfo[n])}async onTestCaseStart(e,s){let r=x(e),n=this.projectBySpecMap[r],i=b(e,s),o=R(n,r);this.specInfo[o]||(this.specInfo[o]={projectId:n,specName:r,testCaseList:{},specResult:null},this.specInfoDeferred[o]=new g,this.specInfoDeferred[o].resolve());let a=j(n,r,i);this.specInfo[o].testCaseList[a]?this.specInfo[o].testCaseList[a].timestamps.push(s.startedAt??new Date().getTime()):(this.specInfo[o].testCaseList[a]={id:i,timestamps:[s.startedAt??new Date().getTime()],title:y(s),result:[],config:e.context.config},this.testCaseDeferred[a]=new g,this.testCaseDeferred[a].resolve()),p("Test case execution started [%s]: %o",i,this.specInfo[o].testCaseList[a])}async onTestCaseResult(e,s){let r=x(e),n=this.projectBySpecMap[r],i=b(e,s),o=R(n,r);await this.specInfoDeferred[o].promise;let a=j(n,r,i);this.testCaseDeferred[a]||(this.testCaseDeferred[a]=new g,this.testCaseDeferred[a].resolve()),await this.testCaseDeferred[a].promise,this.specInfo[o].testCaseList[a]||(this.specInfo[o].testCaseList[a]={id:i,timestamps:[],title:y(s),result:[],config:e.context.config,location:s.location},p("Test case execution was skipped [%s]: %o",i,this.specInfo[o].testCaseList[a])),this.specInfo[o].testCaseList[a].result.push(s),this.resultsDeferred[a]=new g,this.resultsDeferred[a].resolve(),p("Test case execution completed [%s]: %o",i,this.specInfo[o].testCaseList[a])}async onTestFileResult(e,s){let r=x(e),n=this.projectBySpecMap[r],i=R(n,r);p("Spec execution completed [%s], jest test result: %o",r,s),s.testResults.forEach(async c=>{let S=b(e,c),d=j(n,r,S);this.specInfo[i].testCaseList[d]||(this.specInfo[i].testCaseList[d]={id:S,timestamps:[],title:y(c),result:[c],config:e.context.config,location:c.location},this.resultsDeferred[d]=new g,this.resultsDeferred[d].resolve(),p("Spec execution completed [%s][%s], adding skipped tests: %o",r,S,this.specInfo[i].testCaseList[d]))});let o=new Date(s.perfStats.start).toISOString(),a=new Date(s.perfStats.end).toISOString(),m=s.perfStats.end-s.perfStats.start,v=await Promise.all(Object.values(this.specInfo[i].testCaseList).map(async c=>{let S=j(n,r,c.id);await this.resultsDeferred[S].promise;let d=ft(c.result);return{_t:c.timestamps[0]??s.perfStats.start,testId:c.id,title:c.title,state:M(d),isFlaky:gt(c.result),expectedStatus:ut(d),timeout:0,location:{column:c.location?.column??1,file:r,line:c.location?.line??1},retries:c.result.length,attempts:c.result.map((T,L)=>{let W=(T.failureMessages??[]).map(vt=>rt(st(c.config.rootDir,new Error(vt),!1,r),c.config.rootDir));return{_s:M(T.status),attempt:mt(T),startTime:c.timestamps.length&&c.timestamps[L]?new Date(c.timestamps[L]).toISOString():o,steps:[],duration:c.result[L].duration??0,status:lt(T.status),stdout:[],stderr:T.failureMessages??[],errors:W,error:W[0]}})}})),Ct=v.filter(c=>c.isFlaky).length,P={groupId:this.specInfo[i].projectId,spec:this.specInfo[i].specName,startTime:o,results:{stats:{suites:1,tests:s.testResults.length,passes:s.numPassingTests,pending:0,skipped:s.numPendingTests+s.numTodoTests,failures:s.numFailingTests,flaky:Ct,wallClockStartedAt:o,wallClockEndedAt:a,wallClockDuration:m},tests:v}};p("Spec execution completed [%s], result payload: %o",r,P),await J(this.instancesDir,`${nt(this.specInfo[i].specName)}.json`,JSON.stringify(P)),this.processedSpecsCount+=1}async onRunComplete(e,s){I("[currents]: Run completed")}};function R(t,e){return`${t}:${e}`}function j(t,e,s){return`${t}:${e}:${s}`} //# sourceMappingURL=index.js.map