From fef85faa0ab52e56785f5f1a395b440df5c9e7cc Mon Sep 17 00:00:00 2001 From: Adam Pash Date: Tue, 29 Jan 2019 16:57:00 -0800 Subject: [PATCH 1/2] dx: include test results in comment --- .gitignore | 1 + package.json | 2 +- scripts/comment-on-pr.js | 8 +++++- scripts/get-test-report.js | 51 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 60 insertions(+), 2 deletions(-) create mode 100644 scripts/get-test-report.js diff --git a/.gitignore b/.gitignore index 15b8f2c05..f2984975b 100644 --- a/.gitignore +++ b/.gitignore @@ -10,3 +10,4 @@ dist/mercury_test.js dist/mercury_test.js.map dist/mercury_test.web.js tmp/artifacts +test-output.json diff --git a/package.json b/package.json index 7d33878b5..5215791da 100644 --- a/package.json +++ b/package.json @@ -16,7 +16,7 @@ "build:generator": "rollup -c scripts/rollup.config.js", "test_build": "rollup -c", "test": "yarn test:node && yarn test:web", - "test:node": "jest", + "test:node": "jest --json --outputFile test-output.json", "test:web": "node ./node_modules/karma/bin/karma start karma.conf.js --auto-watch", "test:build": "cd ./scripts && jest check-build.test.js", "test:build:web": "node ./scripts/proxy-browser-test.js", diff --git a/scripts/comment-on-pr.js b/scripts/comment-on-pr.js index d58abbbda..4d019feb0 100644 --- a/scripts/comment-on-pr.js +++ b/scripts/comment-on-pr.js @@ -2,6 +2,7 @@ const bot = require('@jesses/circle-github-bot').default.create(); const Mercury = require('../dist/mercury.js'); const fs = require('fs'); +const getTestReport = require('./get-test-report'); const run = () => { const screenshotPath = process.argv[2]; @@ -34,6 +35,9 @@ const run = () => { fs.writeFileSync(jsonPath, JSON.stringify(json)); fs.writeFileSync(fixtureArtifactPath, html); + const testReport = + getTestReport('./test-output.json') || '✅ All tests passed'; + bot.comment( process.env.GH_AUTH_TOKEN, `### 🤖 Automated Parsing Preview 🤖 @@ -63,8 +67,10 @@ ${JSON.stringify(json, null, 2)} ${Object.keys(json) .map(key => (json[key] !== null ? '' : ` * \`${key}\n\``)) - .join('') || 'None'} + .join('\n\n') || 'None'} + +${testReport} ` ); }); diff --git a/scripts/get-test-report.js b/scripts/get-test-report.js new file mode 100644 index 000000000..2fe2c8f06 --- /dev/null +++ b/scripts/get-test-report.js @@ -0,0 +1,51 @@ +const fs = require('fs'); + +const getTestReport = path => { + const testReport = JSON.parse(fs.readFileSync(path)); + const { numFailedTests } = testReport; + if (numFailedTests === 0) { + return false; + } + const { testResults } = testReport; + const failedTests = testResults + .map(({ assertionResults }) => + assertionResults.filter(({ status }) => status !== 'passed') + ) + .reduce((acc, arr) => acc.concat(arr)); + + const failureReport = ` +
+ +${numFailedTests} failed tests 😱 + + +--- + +${failedTests + .map( + ({ fullName, failureMessages }) => + ` +**${fullName}** + +
+ + See what went wrong + + +\`\`\`bash + ${failureMessages.join('\n\n')} +\`\`\` + +
+ +--- + ` + ) + .join('\n\n')} + +
+ `; + return failureReport; +}; + +module.exports = getTestReport; From fccab94eba67df23cad803e3d86f27f399e1c710 Mon Sep 17 00:00:00 2001 From: Adam Pash Date: Tue, 29 Jan 2019 17:01:28 -0800 Subject: [PATCH 2/2] catch error in generating test report --- scripts/get-test-report.js | 43 +++++++++++++++++++++----------------- 1 file changed, 24 insertions(+), 19 deletions(-) diff --git a/scripts/get-test-report.js b/scripts/get-test-report.js index 2fe2c8f06..275386558 100644 --- a/scripts/get-test-report.js +++ b/scripts/get-test-report.js @@ -1,19 +1,20 @@ const fs = require('fs'); const getTestReport = path => { - const testReport = JSON.parse(fs.readFileSync(path)); - const { numFailedTests } = testReport; - if (numFailedTests === 0) { - return false; - } - const { testResults } = testReport; - const failedTests = testResults - .map(({ assertionResults }) => - assertionResults.filter(({ status }) => status !== 'passed') - ) - .reduce((acc, arr) => acc.concat(arr)); - - const failureReport = ` + try { + const testReport = JSON.parse(fs.readFileSync(path)); + const { numFailedTests } = testReport; + if (numFailedTests === 0) { + return false; + } + const { testResults } = testReport; + const failedTests = testResults + .map(({ assertionResults }) => + assertionResults.filter(({ status }) => status !== 'passed') + ) + .reduce((acc, arr) => acc.concat(arr)); + + const failureReport = `
${numFailedTests} failed tests 😱 @@ -22,9 +23,9 @@ const getTestReport = path => { --- ${failedTests - .map( - ({ fullName, failureMessages }) => - ` + .map( + ({ fullName, failureMessages }) => + ` **${fullName}**
@@ -40,12 +41,16 @@ ${failedTests --- ` - ) - .join('\n\n')} + ) + .join('\n\n')}
`; - return failureReport; + return failureReport; + } catch (e) { + console.log('Error generating test report', e); + return false; + } }; module.exports = getTestReport;