Skip to content

Commit 5038cb5

Browse files
authored
Show nice error on tests failure (#416)
* Handle test run error * Change error formatting * Updated snapshots
1 parent b44de20 commit 5038cb5

File tree

12 files changed

+530
-452
lines changed

12 files changed

+530
-452
lines changed

β€Ždist/index.jsβ€Ž

Lines changed: 186 additions & 181 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

β€Ždist/index.js.mapβ€Ž

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

β€Žsrc/format/formatErrors.tsβ€Ž

Lines changed: 49 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,47 @@
1+
import { formatThresholdResults } from './formatThresholdResults';
12
import { ActionError } from '../typings/ActionError';
3+
import { ThresholdResult } from '../typings/ThresholdResult';
24
import { getConsoleLink } from '../utils/getConsoleLink';
35
import { i18n } from '../utils/i18n';
46

57
const getNumberWidth = (index: number) => Math.floor(Math.log10(index));
68

7-
export const formatErrors = (errors: Array<Error>) => {
9+
const formatErrorsInner = (
10+
errors: Array<Error>,
11+
testsFailed: boolean,
12+
thresholdResults: ThresholdResult[]
13+
) => {
14+
if (thresholdResults.length > 0) {
15+
return (
16+
i18n('errors.coverageFail') +
17+
'\n' +
18+
formatThresholdResults(thresholdResults) +
19+
'\n'
20+
);
21+
}
22+
823
if (errors.length === 0) {
9-
return '';
24+
return undefined;
1025
}
1126

1227
if (errors.length === 1) {
1328
const error = errors[0];
1429

1530
if (error instanceof ActionError) {
16-
return i18n(':x: {{ error }}', { error: error.toString() });
31+
return i18n('{{ error }}', { error: error.toString() });
1732
}
1833

19-
return i18n(':x: {{ unexpectedError }} \n```\n{{ error }}\n```', {
34+
if (
35+
error instanceof Error &&
36+
/The process [^\s]+ failed with exit code 1($|\s)/.test(
37+
error.message
38+
) &&
39+
testsFailed
40+
) {
41+
return i18n('errors.testFail');
42+
}
43+
44+
return i18n('{{ unexpectedError }} \n```\n{{ error }}\n```', {
2045
error: error.toString(),
2146
unexpectedError: i18n('errors.unexpectedError', {
2247
consoleLink: getConsoleLink(),
@@ -39,3 +64,23 @@ export const formatErrors = (errors: Array<Error>) => {
3964
})
4065
);
4166
};
67+
68+
export const formatErrors = (
69+
errors: Array<Error>,
70+
testsFailed: boolean,
71+
thresholdResults: ThresholdResult[]
72+
) => {
73+
const text = formatErrorsInner(errors, testsFailed, thresholdResults);
74+
75+
if (!text) {
76+
return '';
77+
}
78+
79+
return (
80+
'> [!CAUTION]\n' +
81+
text
82+
.split('\n')
83+
.map((it) => `> ${it}\n`)
84+
.join('')
85+
);
86+
};

β€Žsrc/format/strings.jsonβ€Ž

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,9 @@
104104
"multiple": "Multiple errors occurred",
105105
"readingCoverageFileFailed": "Failed reading coverage file. (Error: {{ error }})",
106106
"failedGettingCoverage": "Getting code coverage data failed.",
107-
"reportGenerationError": "Action wasn't able to generate report within GitHub comment limit. If you're facing this issue, please let me know by commenting under [this issue](https://github.com/ArtiomTr/jest-coverage-report-action/issues/404)."
107+
"reportGenerationError": "Action wasn't able to generate report within GitHub comment limit. If you're facing this issue, please let me know by commenting under [this issue](https://github.com/ArtiomTr/jest-coverage-report-action/issues/404).",
108+
"testFail": "Test run failed",
109+
"coverageFail": "Coverage does not meet threshold"
108110
},
109111
"detailsHidden": ":warning: Details were not displayed: the report size has exceeded the limit.",
110112
"summaryTitle": "Coverage report {{ dir }}",

β€Žsrc/format/summary/formatCoverageSummary.tsβ€Ž

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,9 @@ export const formatCoverageSummary = (
2929
`${currSummary.covered}/${currSummary.total}`,
3030
]),
3131
],
32-
{ align: ['c', 'l', 'l', 'c'] }
32+
{
33+
align: ['c', 'l', 'l', 'c'],
34+
stringLength: () => 1,
35+
alignDelimiters: false,
36+
}
3337
);

β€Žsrc/stages/createReport.tsβ€Ž

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import { GITHUB_MESSAGE_SIZE_LIMIT } from '../constants/GITHUB_MESSAGE_SIZE_LIMI
55
import { formatCoverage } from '../format/formatCoverage';
66
import { formatErrors } from '../format/formatErrors';
77
import { formatRunReport } from '../format/formatRunReport';
8-
import { formatThresholdResults } from '../format/formatThresholdResults';
98
import { getFailureDetails } from '../format/getFailureDetails';
109
import template from '../format/template.md';
1110
import { JsonReport } from '../typings/JsonReport';
@@ -30,9 +29,14 @@ export const createReport = (
3029

3130
const { errors, data } = dataCollector.get();
3231
const [headReport, baseReport] = data;
33-
const formattedErrors = formatErrors(errors);
32+
const formattedErrors = formatErrors(
33+
errors,
34+
headReport.numFailedTests !== 0 ||
35+
headReport.numFailedTestSuites !== 0 ||
36+
headReport.numRuntimeErrorTestSuites !== 0,
37+
thresholdResults
38+
);
3439

35-
const formattedThresholdResults = formatThresholdResults(thresholdResults);
3640
const coverage = formatCoverage(headReport, baseReport, undefined, false);
3741
const runReport: TestRunReport = headReport.success
3842
? {
@@ -58,12 +62,7 @@ export const createReport = (
5862
const formattedReport = formatRunReport(runReport);
5963

6064
let templateText = insertArgs(template, {
61-
body: [
62-
formattedErrors,
63-
formattedThresholdResults,
64-
coverage,
65-
formattedReport,
66-
].join('\n'),
65+
body: [formattedErrors, coverage, formattedReport].join('\n'),
6766
dir: workingDirectory || '',
6867
tag: getReportTag(options),
6968
title: insertArgs(customTitle || i18n('summaryTitle'), {
@@ -81,12 +80,9 @@ export const createReport = (
8180
);
8281

8382
templateText = insertArgs(template, {
84-
body: [
85-
formattedErrors,
86-
formattedThresholdResults,
87-
reducedCoverage,
88-
formattedReport,
89-
].join('\n'),
83+
body: [formattedErrors, reducedCoverage, formattedReport].join(
84+
'\n'
85+
),
9086
dir: workingDirectory || '',
9187
tag: getReportTag(options),
9288
title: insertArgs(customTitle || i18n('summaryTitle'), {
Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,44 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

33
exports[`formatCoverage should display warning if hiding details 1`] = `
4-
"| <div title=\\"Status of coverage:&#10; 🟒 - ok&#10; 🟑 - slightly more than threshold&#10; πŸ”΄ - under the threshold\\">St.<sup>:grey_question:</sup></div> | Category | Percentage | Covered / Total |
5-
| :----------------------------------------------------------------------------------------------------------------------------------------------------: | :--------- | :--------- | :-------------: |
6-
| 🟒 | Statements | 81.82% | 27/33 |
7-
| 🟒 | Branches | 100% | 8/8 |
8-
| 🟒 | Functions | 63.64% | 7/11 |
9-
| 🟒 | Lines | 80.65% | 25/31 |
4+
"| <div title=\\"Status of coverage:&#10; 🟒 - ok&#10; 🟑 - slightly more than threshold&#10; πŸ”΄ - under the threshold\\">St.<sup>:grey_question:</sup></div> | Category | Percentage | Covered / Total |
5+
| :-: | :- | :- | :-: |
6+
| 🟒 | Statements | 81.82% | 27/33 |
7+
| 🟒 | Branches | 100% | 8/8 |
8+
| 🟒 | Functions | 63.64% | 7/11 |
9+
| 🟒 | Lines | 80.65% | 25/31 |
1010
> :warning: Details were not displayed: the report size has exceeded the limit."
1111
`;
1212
1313
exports[`formatCoverage should format standard coverage 1`] = `
14-
"| <div title=\\"Status of coverage:&#10; 🟒 - ok&#10; 🟑 - slightly more than threshold&#10; πŸ”΄ - under the threshold\\">St.<sup>:grey_question:</sup></div> | Category | Percentage | Covered / Total |
15-
| :----------------------------------------------------------------------------------------------------------------------------------------------------: | :--------- | :--------- | :-------------: |
16-
| 🟒 | Statements | 81.82% | 27/33 |
17-
| 🟒 | Branches | 100% | 8/8 |
18-
| 🟒 | Functions | 63.64% | 7/11 |
19-
| 🟒 | Lines | 80.65% | 25/31 |
14+
"| <div title=\\"Status of coverage:&#10; 🟒 - ok&#10; 🟑 - slightly more than threshold&#10; πŸ”΄ - under the threshold\\">St.<sup>:grey_question:</sup></div> | Category | Percentage | Covered / Total |
15+
| :-: | :- | :- | :-: |
16+
| 🟒 | Statements | 81.82% | 27/33 |
17+
| 🟒 | Branches | 100% | 8/8 |
18+
| 🟒 | Functions | 63.64% | 7/11 |
19+
| 🟒 | Lines | 80.65% | 25/31 |
2020
2121
"
2222
`;
2323
2424
exports[`formatCoverage should format standard coverage 2`] = `
25-
"| <div title=\\"Status of coverage:&#10; 🟒 - ok&#10; 🟑 - slightly more than threshold&#10; πŸ”΄ - under the threshold\\">St.<sup>:grey_question:</sup></div> | Category | Percentage | Covered / Total |
26-
| :----------------------------------------------------------------------------------------------------------------------------------------------------: | :--------- | :--------- | :-------------: |
27-
| 🟒 | Statements | 81.82% | 27/33 |
28-
| 🟒 | Branches | 100% | 8/8 |
29-
| 🟒 | Functions | 63.64% | 7/11 |
30-
| 🟒 | Lines | 80.65% | 25/31 |
25+
"| <div title=\\"Status of coverage:&#10; 🟒 - ok&#10; 🟑 - slightly more than threshold&#10; πŸ”΄ - under the threshold\\">St.<sup>:grey_question:</sup></div> | Category | Percentage | Covered / Total |
26+
| :-: | :- | :- | :-: |
27+
| 🟒 | Statements | 81.82% | 27/33 |
28+
| 🟒 | Branches | 100% | 8/8 |
29+
| 🟒 | Functions | 63.64% | 7/11 |
30+
| 🟒 | Lines | 80.65% | 25/31 |
3131
3232
"
3333
`;
3434
3535
exports[`formatCoverage should format standard coverage 3`] = `
36-
"| <div title=\\"Status of coverage:&#10; 🟒 - ok&#10; 🟑 - slightly more than threshold&#10; πŸ”΄ - under the threshold\\">St.<sup>:grey_question:</sup></div> | Category | Percentage | Covered / Total |
37-
| :----------------------------------------------------------------------------------------------------------------------------------------------------: | :--------- | :--------- | :-------------: |
38-
| 🟒 | Statements | 81.82% | 27/33 |
39-
| 🟒 | Branches | 100% | 8/8 |
40-
| 🟑 | Functions | 63.64% | 7/11 |
41-
| 🟒 | Lines | 80.65% | 25/31 |
36+
"| <div title=\\"Status of coverage:&#10; 🟒 - ok&#10; 🟑 - slightly more than threshold&#10; πŸ”΄ - under the threshold\\">St.<sup>:grey_question:</sup></div> | Category | Percentage | Covered / Total |
37+
| :-: | :- | :- | :-: |
38+
| 🟒 | Statements | 81.82% | 27/33 |
39+
| 🟒 | Branches | 100% | 8/8 |
40+
| 🟑 | Functions | 63.64% | 7/11 |
41+
| 🟒 | Lines | 80.65% | 25/31 |
4242
4343
"
4444
`;

0 commit comments

Comments
Β (0)