@@ -22,8 +22,10 @@ const defaultConfig = {
2222 passed : { status_id : 1 } ,
2323 failed : { status_id : 5 } ,
2424 } ,
25+ runId : undefined ,
2526 closeTestRun : true ,
26- version : '1' // this is the build version - OPTIONAL
27+ version : '1' , // this is the build version - OPTIONAL,
28+ resultProcessor : undefined
2729} ;
2830
2931let helper ;
@@ -38,8 +40,13 @@ module.exports = (config) => {
3840 config = deepMerge ( defaultConfig , config ) ;
3941 output . showDebugLog ( config . debugLog ) ;
4042
41- if ( config . host === '' || config . user === '' || config . password === '' ) throw new Error ( 'Please provide proper Testrail host or credentials' ) ;
43+ if ( ! config . host ) throw new Error ( 'Please provide proper Testrail host' ) ;
44+ if ( ! config . user ) throw new Error ( 'Please provide proper Testrail user' ) ;
45+ if ( ! config . password ) throw new Error ( 'Please provide proper Testrail password' ) ;
4246 if ( ! config . projectId ) throw new Error ( 'Please provide project id in config file' ) ;
47+ if ( config . resultProcessor && typeof config . resultProcessor !== 'function' ) {
48+ throw new Error ( 'Result processor (`resultProcessor` config option) has to be function' ) ;
49+ }
4350
4451 const testrail = new TestRail ( config ) ;
4552
@@ -202,7 +209,7 @@ module.exports = (config) => {
202209 let config_ids = [ ] ;
203210
204211 mergedTests . forEach ( test => {
205- for ( let [ key , value ] of Object . entries ( test ) ) {
212+ for ( const [ key , value ] of Object . entries ( test ) ) {
206213 if ( key === 'case_id' ) {
207214 ids . push ( value ) ;
208215 }
@@ -334,20 +341,40 @@ module.exports = (config) => {
334341
335342 const allResults = passedTests . concat ( failedTests . concat ( skippedTest ) ) ;
336343
337- // Before POST-ing the results, filter the array for any non-existing tags in TR test bucket assigned to this test run
338- // This is to avoid any failure to POST results due to labels in the results array not part of the test run
339- let validResults = [ ] ;
340- testrail . getCases ( config . projectId , config . suiteId ) . then ( res => {
341- if ( res . length ) {
342- validResults = allResults . filter ( result => res . find ( tag => tag . id == result . case_id ) ) ;
343- const missingLabels = allResults . filter ( result => ! validResults . find ( vResult => vResult . case_id == result . case_id ) ) ;
344+ testrail . getCases ( config . projectId , config . suiteId ) . then ( testCases => {
345+ if ( testCases . length ) {
346+ // Before POST-ing the results, filter the array for any non-existing tags in TR test bucket assigned to this test run
347+ // This is to avoid any failure to POST results due to labels in the results array not part of the test run
348+ const { validResults, missingLabels } = allResults . reduce (
349+ ( acc , testResult ) => {
350+ const testCase = testCases . find ( it => it . id == testResult . case_id ) ;
351+ // If there is `resultProcessor` callback in config, then we need to process test result
352+ const processedResult = config . resultProcessor
353+ ? config . resultProcessor ( testResult , { testCase, allResults, allTestCases : testCases } )
354+ : testResult ;
355+
356+ if ( processedResult ) {
357+ if ( testCase ) {
358+ acc . validResults . push ( processedResult ) ;
359+ } else {
360+ acc . missingLabels . push ( processedResult ) ;
361+ }
362+ }
363+ return acc ;
364+ } ,
365+ { validResults : [ ] , missingLabels : [ ] }
366+ ) ;
367+
344368 if ( missingLabels . length ) {
345369 output . error ( `Error: some labels are missing from the test run and the results were not send through: ${ JSON . stringify ( missingLabels . map ( l => l . case_id ) ) } ` ) ;
346370 }
371+
372+ return { validResults } ;
347373 }
348- } ) . then ( ( ) => {
374+ return { validResults : [ ] } ;
375+ } ) . then ( ( { validResults } ) => {
349376 if ( validResults . length ) {
350- testrail . addResultsForCases ( runId , { results : validResults } ) . then ( res => {
377+ testrail . addResultsForCases ( runId , { results : validResults } ) . then ( res => {
351378 output . log ( `The run ${ runId } is updated with ${ JSON . stringify ( res ) } ` ) ;
352379
353380 for ( const test of failedTests ) {
0 commit comments