Skip to content

Commit 1c7ae48

Browse files
authored
dx: include test results in comment (#230)
1 parent bb6ad26 commit 1c7ae48

File tree

4 files changed

+65
-2
lines changed

4 files changed

+65
-2
lines changed

Diff for: .gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@ dist/mercury_test.js
1010
dist/mercury_test.js.map
1111
dist/mercury_test.web.js
1212
tmp/artifacts
13+
test-output.json

Diff for: package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
"build:generator": "rollup -c scripts/rollup.config.js",
1717
"test_build": "rollup -c",
1818
"test": "yarn test:node && yarn test:web",
19-
"test:node": "jest",
19+
"test:node": "jest --json --outputFile test-output.json",
2020
"test:web": "node ./node_modules/karma/bin/karma start karma.conf.js --auto-watch",
2121
"test:build": "cd ./scripts && jest check-build.test.js",
2222
"test:build:web": "node ./scripts/proxy-browser-test.js",

Diff for: scripts/comment-on-pr.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
const bot = require('@jesses/circle-github-bot').default.create();
33
const Mercury = require('../dist/mercury.js');
44
const fs = require('fs');
5+
const getTestReport = require('./get-test-report');
56

67
const run = () => {
78
const screenshotPath = process.argv[2];
@@ -34,6 +35,9 @@ const run = () => {
3435
fs.writeFileSync(jsonPath, JSON.stringify(json));
3536
fs.writeFileSync(fixtureArtifactPath, html);
3637

38+
const testReport =
39+
getTestReport('./test-output.json') || '✅ All tests passed';
40+
3741
bot.comment(
3842
process.env.GH_AUTH_TOKEN,
3943
`### 🤖 Automated Parsing Preview 🤖
@@ -63,8 +67,10 @@ ${JSON.stringify(json, null, 2)}
6367
6468
${Object.keys(json)
6569
.map(key => (json[key] !== null ? '' : ` * \`${key}\n\``))
66-
.join('') || 'None'}
70+
.join('\n\n') || 'None'}
71+
6772
73+
${testReport}
6874
`
6975
);
7076
});

Diff for: scripts/get-test-report.js

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
const fs = require('fs');
2+
3+
const getTestReport = path => {
4+
try {
5+
const testReport = JSON.parse(fs.readFileSync(path));
6+
const { numFailedTests } = testReport;
7+
if (numFailedTests === 0) {
8+
return false;
9+
}
10+
const { testResults } = testReport;
11+
const failedTests = testResults
12+
.map(({ assertionResults }) =>
13+
assertionResults.filter(({ status }) => status !== 'passed')
14+
)
15+
.reduce((acc, arr) => acc.concat(arr));
16+
17+
const failureReport = `
18+
<details>
19+
<summary>
20+
<b>${numFailedTests} failed tests 😱</b>
21+
</summary>
22+
23+
---
24+
25+
${failedTests
26+
.map(
27+
({ fullName, failureMessages }) =>
28+
`
29+
**${fullName}**
30+
31+
<details>
32+
<summary>
33+
See what went wrong
34+
</summary>
35+
36+
\`\`\`bash
37+
${failureMessages.join('\n\n')}
38+
\`\`\`
39+
40+
</details>
41+
42+
---
43+
`
44+
)
45+
.join('\n\n')}
46+
47+
</details>
48+
`;
49+
return failureReport;
50+
} catch (e) {
51+
console.log('Error generating test report', e);
52+
return false;
53+
}
54+
};
55+
56+
module.exports = getTestReport;

0 commit comments

Comments
 (0)