2022-07-06 17:17:51 +00:00
/ * *
* Script to generate the test results summary to be posted as a GitHub Job Summary and as a PR comment .
* /
const {
API _SUMMARY _PATH ,
E2E _PW _SUMMARY _PATH ,
SHA ,
PR _NUMBER ,
2022-10-05 17:44:20 +00:00
E2E _GRAND _TOTAL ,
2022-07-06 17:17:51 +00:00
} = process . env ;
/ * *
* Convert the given ` duration ` from milliseconds to a more user - friendly string .
* For example , if ` duration = 323000 ` , this function would return ` 5m 23s ` .
*
* @ param { Number } duration Duration in millisecods , as read from either the ` summary.json ` file in the Allure report , or from the ` test-results.json ` file from the Jest - Puppeteer report .
* @ returns String in "5m 23s" format .
* /
const getFormattedDuration = ( duration ) => {
const durationMinutes = Math . floor ( duration / 1000 / 60 ) ;
const durationSeconds = Math . floor ( ( duration / 1000 ) % 60 ) ;
return ` ${ durationMinutes } m ${ durationSeconds } s ` ;
} ;
/ * *
* Extract the test report statistics ( the number of tests that passed , failed , skipped , etc . ) from Allure report ' s ` summary.json ` file .
*
* @ param { string } summaryJSONPath Path to the Allure report ' s ` summary.json ` file .
2022-10-05 17:44:20 +00:00
* @ returns An object containing relevant statistics from the Allure report .
2022-07-06 17:17:51 +00:00
* /
2022-10-05 17:44:20 +00:00
const getAllureSummaryStats = ( summaryJSONPath ) => {
2022-07-06 17:17:51 +00:00
const summary = require ( summaryJSONPath ) ;
2022-10-05 17:44:20 +00:00
const { statistic , time } = summary ;
const { passed , failed , skipped , broken , unknown , total } = statistic ;
const { duration } = time ;
return {
passed ,
failed ,
skipped ,
broken ,
unknown ,
total ,
duration ,
} ;
} ;
/ * *
* Construct the array to be used for the API table row .
*
* @ returns Array of API test result stats .
* /
const createAPITableRow = ( ) => {
const { passed , failed , skipped , broken , unknown , total , duration } =
getAllureSummaryStats ( API _SUMMARY _PATH ) ;
2022-07-06 17:17:51 +00:00
const durationFormatted = getFormattedDuration ( duration ) ;
return [
2022-10-05 17:44:20 +00:00
'API Tests' ,
2022-07-06 17:17:51 +00:00
passed . toString ( ) ,
failed . toString ( ) ,
broken . toString ( ) ,
skipped . toString ( ) ,
unknown . toString ( ) ,
total . toString ( ) ,
durationFormatted ,
] ;
} ;
/ * *
2022-10-05 17:44:20 +00:00
* Construct the array to be used for the E2E table row .
2022-07-06 17:17:51 +00:00
*
2022-10-05 17:44:20 +00:00
* @ returns Array of E2E test result stats .
* /
const createE2ETableRow = ( ) => {
const { passed , failed , skipped , broken , unknown , total , duration } =
getAllureSummaryStats ( E2E _PW _SUMMARY _PATH ) ;
const durationFormatted = getFormattedDuration ( duration ) ;
return [
'E2E Tests' ,
passed . toString ( ) ,
failed . toString ( ) ,
broken . toString ( ) ,
skipped . toString ( ) ,
unknown . toString ( ) ,
total . toString ( ) ,
durationFormatted ,
] ;
} ;
/ * *
* Add a warning when the number of executed Playwright E2E tests were fewer than the total .
2022-07-06 17:17:51 +00:00
* /
2022-10-05 17:44:20 +00:00
const addWarningE2EIncomplete = ( warnings ) => {
const { statistic } = require ( E2E _PW _SUMMARY _PATH ) ;
const { total } = statistic ;
const expectedTotal = Number ( E2E _GRAND _TOTAL ) ;
if ( total < expectedTotal ) {
warnings . push (
` INCOMPLETE E2E TEST RUN. We have a total of ${ expectedTotal } E2E tests, but only ${ total } were executed. Note that in CI, E2E tests automatically end when they encounter too many failures. `
) ;
}
2022-07-06 17:17:51 +00:00
} ;
/ * *
*
2022-10-05 17:44:20 +00:00
* Add a warning when there are failures and broken tests .
2022-07-06 17:17:51 +00:00
* /
2022-10-05 17:44:20 +00:00
const addWarningFailuresBrokenTests = ( warnings ) => {
const { failed : apiFailed , broken : apiBroken } =
getAllureSummaryStats ( API _SUMMARY _PATH ) ;
const { failed : e2eFailed , broken : e2eBroken } =
getAllureSummaryStats ( E2E _PW _SUMMARY _PATH ) ;
if ( apiFailed || apiBroken || e2eFailed || e2eBroken ) {
warnings . push (
'FAILED/BROKEN TESTS. There were failed and/or broken API and E2E tests.'
) ;
}
2022-07-06 17:17:51 +00:00
} ;
/ * *
2022-10-05 17:44:20 +00:00
* Add warnings to the test summary .
2022-07-06 17:17:51 +00:00
*
2022-10-05 17:44:20 +00:00
* @ param core The GitHub Actions toolkit core object
2022-07-06 17:17:51 +00:00
* /
2022-10-05 17:44:20 +00:00
const addSummaryWarnings = ( core ) => {
const warnings = [ ] ;
addWarningFailuresBrokenTests ( warnings ) ;
addWarningE2EIncomplete ( warnings ) ;
if ( warnings . length > 0 ) {
core . summary
. addHeading ( ':warning: Warning' , 3 )
. addRaw (
'Please address the following issues prior to merging this pull request:'
)
. addList ( warnings ) ;
}
} ;
/ * *
* Create the heading , commit SHA , and test results table .
*
* @ param core The GitHub Actions toolkit core object
* /
const addSummaryHeadingAndTable = ( core ) => {
const apiTableRow = createAPITableRow ( ) ;
const e2eTableRow = createE2ETableRow ( ) ;
2022-07-06 17:17:51 +00:00
2022-10-05 17:44:20 +00:00
core . summary
2022-07-06 17:17:51 +00:00
. addHeading ( 'Test Results Summary' )
. addRaw ( ` Commit SHA: ${ SHA } ` )
. addBreak ( )
. addBreak ( )
. addTable ( [
[
{ data : 'Test :test_tube:' , header : true } ,
{ data : 'Passed :white_check_mark:' , header : true } ,
{ data : 'Failed :rotating_light:' , header : true } ,
{ data : 'Broken :construction:' , header : true } ,
{ data : 'Skipped :next_track_button:' , header : true } ,
{ data : 'Unknown :grey_question:' , header : true } ,
{ data : 'Total :bar_chart:' , header : true } ,
{ data : 'Duration :stopwatch:' , header : true } ,
] ,
2022-10-05 17:44:20 +00:00
apiTableRow ,
e2eTableRow ,
] ) ;
} ;
/ * *
* Add the summary footer .
*
* @ param core The GitHub Actions toolkit core object
* /
const addSummaryFooter = ( core ) => {
core . summary
. addSeparator ( )
2022-07-06 17:17:51 +00:00
. addRaw ( 'To view the full API test report, click ' )
. addLink (
'here.' ,
` https://woocommerce.github.io/woocommerce-test-reports/pr/ ${ PR _NUMBER } /api/ `
)
. addBreak ( )
. addRaw ( 'To view the full E2E test report, click ' )
. addLink (
'here.' ,
2022-07-14 21:15:20 +00:00
` https://woocommerce.github.io/woocommerce-test-reports/pr/ ${ PR _NUMBER } /e2e/ `
2022-07-06 17:17:51 +00:00
)
. addBreak ( )
. addRaw ( 'To view all test reports, visit the ' )
. addLink (
2022-07-14 21:15:20 +00:00
'WooCommerce Test Reports Dashboard.' ,
2022-07-06 17:17:51 +00:00
'https://woocommerce.github.io/woocommerce-test-reports/'
2022-10-05 17:44:20 +00:00
) ;
} ;
/ * *
* Generate the contents of the test results summary and post it on the workflow run .
*
* @ param { * } params Objects passed from the calling GitHub Action workflow .
* @ returns Stringified content of the test results summary .
* /
module . exports = async ( { core } ) => {
addSummaryHeadingAndTable ( core ) ;
addSummaryWarnings ( core ) ;
addSummaryFooter ( core ) ;
const summary = core . summary . stringify ( ) ;
2022-07-06 17:17:51 +00:00
await core . summary . write ( ) ;
2022-10-05 17:44:20 +00:00
return summary ;
2022-07-06 17:17:51 +00:00
} ;