Skip to content

Commit

Permalink
Show nice error on tests failure (#416)
Browse files Browse the repository at this point in the history
* Handle test run error

* Change error formatting

* Updated snapshots
  • Loading branch information
ArtiomTr committed Mar 3, 2024
1 parent b44de20 commit 5038cb5
Show file tree
Hide file tree
Showing 12 changed files with 530 additions and 452 deletions.
367 changes: 186 additions & 181 deletions dist/index.js

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions dist/index.js.map

Large diffs are not rendered by default.

53 changes: 49 additions & 4 deletions src/format/formatErrors.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,47 @@
import { formatThresholdResults } from './formatThresholdResults';
import { ActionError } from '../typings/ActionError';
import { ThresholdResult } from '../typings/ThresholdResult';
import { getConsoleLink } from '../utils/getConsoleLink';
import { i18n } from '../utils/i18n';

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

export const formatErrors = (errors: Array<Error>) => {
const formatErrorsInner = (
errors: Array<Error>,
testsFailed: boolean,
thresholdResults: ThresholdResult[]
) => {
if (thresholdResults.length > 0) {
return (
i18n('errors.coverageFail') +
'\n' +
formatThresholdResults(thresholdResults) +
'\n'
);
}

if (errors.length === 0) {
return '';
return undefined;
}

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

if (error instanceof ActionError) {
return i18n(':x: {{ error }}', { error: error.toString() });
return i18n('{{ error }}', { error: error.toString() });
}

return i18n(':x: {{ unexpectedError }} \n```\n{{ error }}\n```', {
if (
error instanceof Error &&
/The process [^\s]+ failed with exit code 1($|\s)/.test(
error.message
) &&
testsFailed
) {
return i18n('errors.testFail');
}

return i18n('{{ unexpectedError }} \n```\n{{ error }}\n```', {
error: error.toString(),
unexpectedError: i18n('errors.unexpectedError', {
consoleLink: getConsoleLink(),
Expand All @@ -39,3 +64,23 @@ export const formatErrors = (errors: Array<Error>) => {
})
);
};

export const formatErrors = (
errors: Array<Error>,
testsFailed: boolean,
thresholdResults: ThresholdResult[]
) => {
const text = formatErrorsInner(errors, testsFailed, thresholdResults);

if (!text) {
return '';
}

return (
'> [!CAUTION]\n' +
text
.split('\n')
.map((it) => `> ${it}\n`)
.join('')
);
};
4 changes: 3 additions & 1 deletion src/format/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,9 @@
"multiple": "Multiple errors occurred",
"readingCoverageFileFailed": "Failed reading coverage file. (Error: {{ error }})",
"failedGettingCoverage": "Getting code coverage data failed.",
"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)."
"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).",
"testFail": "Test run failed",
"coverageFail": "Coverage does not meet threshold"
},
"detailsHidden": ":warning: Details were not displayed: the report size has exceeded the limit.",
"summaryTitle": "Coverage report {{ dir }}",
Expand Down
6 changes: 5 additions & 1 deletion src/format/summary/formatCoverageSummary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,9 @@ export const formatCoverageSummary = (
`${currSummary.covered}/${currSummary.total}`,
]),
],
{ align: ['c', 'l', 'l', 'c'] }
{
align: ['c', 'l', 'l', 'c'],
stringLength: () => 1,
alignDelimiters: false,
}
);
26 changes: 11 additions & 15 deletions src/stages/createReport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { GITHUB_MESSAGE_SIZE_LIMIT } from '../constants/GITHUB_MESSAGE_SIZE_LIMI
import { formatCoverage } from '../format/formatCoverage';
import { formatErrors } from '../format/formatErrors';
import { formatRunReport } from '../format/formatRunReport';
import { formatThresholdResults } from '../format/formatThresholdResults';
import { getFailureDetails } from '../format/getFailureDetails';
import template from '../format/template.md';
import { JsonReport } from '../typings/JsonReport';
Expand All @@ -30,9 +29,14 @@ export const createReport = (

const { errors, data } = dataCollector.get();
const [headReport, baseReport] = data;
const formattedErrors = formatErrors(errors);
const formattedErrors = formatErrors(
errors,
headReport.numFailedTests !== 0 ||
headReport.numFailedTestSuites !== 0 ||
headReport.numRuntimeErrorTestSuites !== 0,
thresholdResults
);

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

let templateText = insertArgs(template, {
body: [
formattedErrors,
formattedThresholdResults,
coverage,
formattedReport,
].join('\n'),
body: [formattedErrors, coverage, formattedReport].join('\n'),
dir: workingDirectory || '',
tag: getReportTag(options),
title: insertArgs(customTitle || i18n('summaryTitle'), {
Expand All @@ -81,12 +80,9 @@ export const createReport = (
);

templateText = insertArgs(template, {
body: [
formattedErrors,
formattedThresholdResults,
reducedCoverage,
formattedReport,
].join('\n'),
body: [formattedErrors, reducedCoverage, formattedReport].join(
'\n'
),
dir: workingDirectory || '',
tag: getReportTag(options),
title: insertArgs(customTitle || i18n('summaryTitle'), {
Expand Down
48 changes: 24 additions & 24 deletions tests/format/__snapshots__/formatCoverage.test.ts.snap
Original file line number Diff line number Diff line change
@@ -1,44 +1,44 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`formatCoverage should display warning if hiding details 1`] = `
"| <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 |
| :----------------------------------------------------------------------------------------------------------------------------------------------------: | :--------- | :--------- | :-------------: |
| 🟒 | Statements | 81.82% | 27/33 |
| 🟒 | Branches | 100% | 8/8 |
| 🟒 | Functions | 63.64% | 7/11 |
| 🟒 | Lines | 80.65% | 25/31 |
"| <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 |
| :-: | :- | :- | :-: |
| 🟒 | Statements | 81.82% | 27/33 |
| 🟒 | Branches | 100% | 8/8 |
| 🟒 | Functions | 63.64% | 7/11 |
| 🟒 | Lines | 80.65% | 25/31 |
> :warning: Details were not displayed: the report size has exceeded the limit."
`;
exports[`formatCoverage should format standard coverage 1`] = `
"| <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 |
| :----------------------------------------------------------------------------------------------------------------------------------------------------: | :--------- | :--------- | :-------------: |
| 🟒 | Statements | 81.82% | 27/33 |
| 🟒 | Branches | 100% | 8/8 |
| 🟒 | Functions | 63.64% | 7/11 |
| 🟒 | Lines | 80.65% | 25/31 |
"| <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 |
| :-: | :- | :- | :-: |
| 🟒 | Statements | 81.82% | 27/33 |
| 🟒 | Branches | 100% | 8/8 |
| 🟒 | Functions | 63.64% | 7/11 |
| 🟒 | Lines | 80.65% | 25/31 |
"
`;
exports[`formatCoverage should format standard coverage 2`] = `
"| <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 |
| :----------------------------------------------------------------------------------------------------------------------------------------------------: | :--------- | :--------- | :-------------: |
| 🟒 | Statements | 81.82% | 27/33 |
| 🟒 | Branches | 100% | 8/8 |
| 🟒 | Functions | 63.64% | 7/11 |
| 🟒 | Lines | 80.65% | 25/31 |
"| <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 |
| :-: | :- | :- | :-: |
| 🟒 | Statements | 81.82% | 27/33 |
| 🟒 | Branches | 100% | 8/8 |
| 🟒 | Functions | 63.64% | 7/11 |
| 🟒 | Lines | 80.65% | 25/31 |
"
`;
exports[`formatCoverage should format standard coverage 3`] = `
"| <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 |
| :----------------------------------------------------------------------------------------------------------------------------------------------------: | :--------- | :--------- | :-------------: |
| 🟒 | Statements | 81.82% | 27/33 |
| 🟒 | Branches | 100% | 8/8 |
| 🟑 | Functions | 63.64% | 7/11 |
| 🟒 | Lines | 80.65% | 25/31 |
"| <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 |
| :-: | :- | :- | :-: |
| 🟒 | Statements | 81.82% | 27/33 |
| 🟒 | Branches | 100% | 8/8 |
| 🟑 | Functions | 63.64% | 7/11 |
| 🟒 | Lines | 80.65% | 25/31 |
"
`;

0 comments on commit 5038cb5

Please sign in to comment.