@@ -31,24 +31,38 @@ const objectStore = require('../objectStore')
3131
3232const config = objectStore . get ( 'config' )
3333
34- const generateSlackBlocks = ( progress ) => {
34+ const millisecondsToTime = ( milliseconds ) => {
35+ const seconds = Math . floor ( milliseconds / 1000 )
36+ const minutes = Math . floor ( seconds / 60 )
37+ const hours = Math . floor ( minutes / 60 )
38+ return `${ String ( hours ) . padStart ( 2 , '0' ) } :${ String ( minutes % 60 ) . padStart ( 2 , '0' ) } :${ String ( seconds % 60 ) . padStart ( 2 , '0' ) } `
39+ }
40+
41+ const generateSlackBlocks = ( progress , reportURL ) => {
3542 const slackBlocks = [ ]
3643 let totalAssertionsCount = 0
3744 let totalPassedAssertionsCount = 0
3845 let totalRequestsCount = 0
46+ const failedTestCases = [ ]
3947 progress . test_cases . forEach ( testCase => {
4048 // console.log(fStr.yellow(testCase.name))
4149 totalRequestsCount += testCase . requests . length
42- // let testCaseAssertionsCount = 0
43- // let testCasePassedAssertionsCount = 0
50+ let testCaseAssertionsCount = 0
51+ let testCasePassedAssertionsCount = 0
4452 testCase . requests . forEach ( req => {
4553 const passedAssertionsCount = req . request . tests && req . request . tests . passedAssertionsCount ? req . request . tests . passedAssertionsCount : 0
4654 const assertionsCount = req . request . tests && req . request . tests . assertions && req . request . tests . assertions . length ? req . request . tests . assertions . length : 0
4755 totalAssertionsCount += assertionsCount
4856 totalPassedAssertionsCount += passedAssertionsCount
49- // testCaseAssertionsCount += assertionsCount
50- // testCasePassedAssertionsCount += passedAssertionsCount
57+ testCaseAssertionsCount += assertionsCount
58+ testCasePassedAssertionsCount += passedAssertionsCount
5159 } )
60+ if ( testCaseAssertionsCount !== testCasePassedAssertionsCount ) {
61+ failedTestCases . push ( {
62+ name : testCase . name ,
63+ failedAssertions : testCaseAssertionsCount - testCasePassedAssertionsCount
64+ } )
65+ }
5266 // const passed = testCasePassedAssertionsCount === testCaseAssertionsCount
5367 // // TODO: make sure this list should not be more than 40 because we can add only max 50 blocks in a slack message
5468 // if(!passed) {
@@ -61,6 +75,40 @@ const generateSlackBlocks = (progress) => {
6175 // })
6276 // }
6377 } )
78+
79+ // totalAssertionsCount = totalPassedAssertionsCount
80+ // failedTestCases.length = 0
81+
82+ if ( config . briefSummaryPrefix ) {
83+ const top5FailedTestCases = failedTestCases . sort ( ( a , b ) => b . failedAssertions - a . failedAssertions ) . slice ( 0 , 5 )
84+ return [ {
85+ type : 'context' ,
86+ elements : [ {
87+ type : 'mrkdwn' ,
88+ text : [
89+ `${ totalAssertionsCount === totalPassedAssertionsCount ? '🟢' : '🔴' } ` ,
90+ reportURL ? `<${ reportURL } |${ config . briefSummaryPrefix } >` : `${ config . briefSummaryPrefix } ` ,
91+ `failed: \`${ totalAssertionsCount - totalPassedAssertionsCount } /${ totalAssertionsCount } ` ,
92+ `(${ ( 100 * ( ( totalAssertionsCount - totalPassedAssertionsCount ) / totalAssertionsCount ) ) . toFixed ( 2 ) } %)\`,` ,
93+ `requests: \`${ totalRequestsCount } \`,` ,
94+ `tests: \`${ progress . test_cases . length } \`,` ,
95+ `duration: \`${ millisecondsToTime ( progress . runtimeInformation . runDurationMs ) } \`` ,
96+ top5FailedTestCases . length > 0 && '\nTop 5 failed test cases:\n' ,
97+ top5FailedTestCases . length > 0 && top5FailedTestCases . map ( tc => `• ${ tc . name } : \`${ tc . failedAssertions } \`` ) . join ( '\n' )
98+ ] . filter ( Boolean ) . join ( ' ' )
99+ } ]
100+ } ]
101+ }
102+
103+ slackBlocks . push ( {
104+ type : 'header' ,
105+ text : {
106+ type : 'plain_text' ,
107+ text : 'Testing Toolkit Report' ,
108+ emoji : true
109+ }
110+ } )
111+
64112 let summaryText = ''
65113
66114 summaryText += '>Total assertions: *' + totalAssertionsCount + '*\n'
@@ -107,40 +155,32 @@ const generateSlackBlocks = (progress) => {
107155 } ,
108156 ...additionalParams
109157 } )
158+ if ( reportURL ) {
159+ slackBlocks . push ( {
160+ type : 'section' ,
161+ text : {
162+ type : 'mrkdwn' ,
163+ text : '<' + reportURL + '|View Report>'
164+ }
165+ } )
166+ }
167+ slackBlocks . push ( {
168+ type : 'divider'
169+ } )
110170 return slackBlocks
111171}
112172
113- const sendSlackNotification = async ( progress , reportURL = null ) => {
173+ const sendSlackNotification = async ( progress , reportURL = 'http://localhost/' ) => {
114174 if ( config . slackWebhookUrl ) {
115175 const url = config . slackWebhookUrl
116176 const webhook = new IncomingWebhook ( url )
117- let slackBlocks = [ ]
118- slackBlocks . push ( {
119- type : 'header' ,
120- text : {
121- type : 'plain_text' ,
122- text : 'Testing Toolkit Report' ,
123- emoji : true
124- }
125- } )
126- slackBlocks = slackBlocks . concat ( generateSlackBlocks ( progress ) )
127- if ( reportURL ) {
128- slackBlocks . push ( {
129- type : 'section' ,
130- text : {
131- type : 'mrkdwn' ,
132- text : '<' + reportURL + '|View Report>'
133- }
134- } )
135- }
136- slackBlocks . push ( {
137- type : 'divider'
138- } )
177+ const blocks = generateSlackBlocks ( progress , reportURL )
178+
139179 try {
140180 // console.log(JSON.stringify(slackBlocks, null, 2))
141181 await webhook . send ( {
142182 text : 'Test Report' ,
143- blocks : slackBlocks
183+ blocks
144184 } )
145185 console . log ( 'Slack notification sent.' )
146186 } catch ( err ) {
0 commit comments