@@ -38,6 +38,10 @@ const millisecondsToTime = (milliseconds) => {
3838 return `${ String ( hours ) . padStart ( 2 , '0' ) } :${ String ( minutes % 60 ) . padStart ( 2 , '0' ) } :${ String ( seconds % 60 ) . padStart ( 2 , '0' ) } `
3939}
4040
41+ /**
42+ * @param {FinalReport } progress
43+ * @param {string } reportURL - URL of the report
44+ */
4145const generateSlackBlocks = ( progress , reportURL ) => {
4246 const slackBlocks = [ ]
4347 let totalAssertionsCount = 0
@@ -88,10 +92,10 @@ const generateSlackBlocks = (progress, reportURL) => {
8892 text : [
8993 `${ totalAssertionsCount === totalPassedAssertionsCount ? '🟢' : '🔴' } ` ,
9094 reportURL ? `<${ reportURL } |${ config . briefSummaryPrefix } >` : `${ config . briefSummaryPrefix } ` ,
95+ `tests: \`${ progress . test_cases . length } \`,` ,
96+ `requests: \`${ totalRequestsCount } \`,` ,
9197 `failed: \`${ totalAssertionsCount - totalPassedAssertionsCount } /${ totalAssertionsCount } ` ,
9298 `(${ ( 100 * ( ( totalAssertionsCount - totalPassedAssertionsCount ) / totalAssertionsCount ) ) . toFixed ( 2 ) } %)\`,` ,
93- `requests: \`${ totalRequestsCount } \`,` ,
94- `tests: \`${ progress . test_cases . length } \`,` ,
9599 `duration: \`${ millisecondsToTime ( progress . runtimeInformation . runDurationMs ) } \`` ,
96100 top5FailedTestCases . length > 0 && '\nTop 5 failed test cases:\n' ,
97101 top5FailedTestCases . length > 0 && top5FailedTestCases . map ( tc => `• ${ tc . name } : \`${ tc . failedAssertions } \`` ) . join ( '\n' )
@@ -170,25 +174,52 @@ const generateSlackBlocks = (progress, reportURL) => {
170174 return slackBlocks
171175}
172176
177+ /**
178+ * @param {FinalReport } progress
179+ * @param {string } reportURL - URL of the report
180+ */
173181const sendSlackNotification = async ( progress , reportURL = 'http://localhost/' ) => {
174- if ( config . slackWebhookUrl ) {
175- const url = config . slackWebhookUrl
176- const webhook = new IncomingWebhook ( url )
177- const blocks = generateSlackBlocks ( progress , reportURL )
178-
179- try {
180- // console.log(JSON.stringify(slackBlocks, null, 2))
181- await webhook . send ( {
182- text : 'Test Report' ,
183- blocks
184- } )
185- console . log ( 'Slack notification sent.' )
186- } catch ( err ) {
187- console . log ( 'ERROR: Sending slack notification failed. ' , err . message )
188- }
182+ const { slackWebhookUrl, slackWebhookUrlForFailed } = config
183+
184+ if ( ! slackWebhookUrl && ! needToNotifyFailed ( slackWebhookUrlForFailed , progress ) ) {
185+ console . log ( 'No Slack webhook URLs configured.' )
186+ return
187+ }
188+ const blocks = generateSlackBlocks ( progress , reportURL )
189+
190+ if ( slackWebhookUrl ) {
191+ await sendWebhook ( slackWebhookUrl , 'Test Report' , blocks )
192+ }
193+
194+ if ( needToNotifyFailed ( slackWebhookUrlForFailed , progress ) ) {
195+ await sendWebhook ( slackWebhookUrlForFailed , 'Failed Tests Report' , blocks )
196+ }
197+ }
198+
199+ const sendWebhook = async ( url , text , blocks ) => {
200+ const webhook = new IncomingWebhook ( url )
201+ try {
202+ await webhook . send ( { text, blocks } )
203+ console . log ( 'Slack notification sent.' )
204+ } catch ( err ) {
205+ console . log ( 'ERROR: Sending Slack notification failed. ' , err . message )
189206 }
190207}
191208
209+ /**
210+ * Determines if a notification for failed tests needs to be sent.
211+ * @param {string | undefined } webhookUrl
212+ * @param {FinalReport } progress
213+ * @returns {boolean }
214+ */
215+ const needToNotifyFailed = ( webhookUrl , progress ) => {
216+ return webhookUrl && ( ! progress ?. runtimeInformation ?. totalAssertions
217+ ? true
218+ : progress . runtimeInformation . totalPassedAssertions !== progress . runtimeInformation . totalAssertions )
219+ }
220+
192221module . exports = {
193- sendSlackNotification
222+ sendSlackNotification,
223+ sendWebhook,
224+ needToNotifyFailed
194225}
0 commit comments