Skip to content

Commit 388d395

Browse files
Merge pull request #1 from bencsr/publish_transient_retry_failure
report transient retry failures
2 parents 023f889 + 55af00e commit 388d395

File tree

5 files changed

+92
-1
lines changed

5 files changed

+92
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ Reporter options should also be strings exception for suiteNameTemplate, classNa
7474
| `JEST_JUNIT_INCLUDE_SHORT_CONSOLE_OUTPUT` | `includeShortConsoleOutput` | Adds short console output (only message value) to any testSuite that generates stdout during a test run. | `false` | N/A
7575
| `JEST_JUNIT_REPORT_TEST_SUITE_ERRORS` | `reportTestSuiteErrors` | Reports test suites that failed to execute altogether as `error`. _Note:_ since the suite name cannot be determined from files that fail to load, it will default to file path.| `false` | N/A
7676
| `JEST_JUNIT_NO_STACK_TRACE` | `noStackTrace` | Omit stack traces from test failure reports, similar to `jest --noStackTrace` | `false` | N/A
77+
| `JEST_JUNIT_PUBLISH_TRANSIENT_RETRY_FAILURES` | `publishTransientRetryFailures` | Create separate `testcase` entries with `failure` for transient errors when using `jest.retryTimes`. | `false` | N/A
7778
| `JEST_USE_PATH_FOR_SUITE_NAME` | `usePathForSuiteName` | **DEPRECATED. Use `suiteNameTemplate` instead.** Use file path as the `name` attribute of `<testsuite>` | `"false"` | N/A
7879
| `JEST_JUNIT_TEST_CASE_PROPERTIES_JSON_FILE` | `testCasePropertiesFile` | Name of the custom testcase properties file | `"junitProperties.js"` | N/A
7980
| `JEST_JUNIT_TEST_CASE_PROPERTIES_DIR` | `testCasePropertiesDirectory` | Location of the custom testcase properties file | `process.cwd()` | N/A

__mocks__/retried-tests.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@
5656
"status": "passed",
5757
"title": "should bar",
5858
"invocations": 2,
59-
"retryReasons": ["error"]
59+
"retryReasons": ["error on first try"]
6060
}
6161
],
6262
"skipped": false

__tests__/buildJsonResults.test.js

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -519,4 +519,74 @@ describe('buildJsonResults', () => {
519519
}
520520
`)
521521
});
522+
523+
it("should add transient retry-failures as testcase", () => {
524+
const retriedTestsReport = require("../__mocks__/retried-tests.json");
525+
526+
// Mock Date.now() to return a fixed later value
527+
const startDate = new Date(retriedTestsReport.startTime);
528+
jest.spyOn(Date, 'now').mockImplementation(() => startDate.getTime() + 1234);
529+
530+
jsonResults = buildJsonResults(retriedTestsReport, "/", {
531+
...constants.DEFAULT_OPTIONS,
532+
testSuitePropertiesFile: "not-existing-file", // Skip the contents of the default junitProperties.js
533+
publishTransientRetryFailures: "true",
534+
});
535+
536+
expect(jsonResults).toMatchInlineSnapshot(`
537+
Object {
538+
"testsuites": Array [
539+
Object {
540+
"_attr": Object {
541+
"errors": 0,
542+
"failures": 0,
543+
"name": "jest tests",
544+
"tests": 1,
545+
"time": 1.234,
546+
},
547+
},
548+
Object {
549+
"testsuite": Array [
550+
Object {
551+
"_attr": Object {
552+
"errors": 0,
553+
"failures": 0,
554+
"name": "foo",
555+
"skipped": 0,
556+
"tests": 1,
557+
"time": 0.12,
558+
"timestamp": "2017-03-17T01:05:47",
559+
},
560+
},
561+
Object {
562+
"testcase": Array [
563+
Object {
564+
"_attr": Object {
565+
"classname": "foo baz should bar",
566+
"name": "foo baz should bar",
567+
"time": 0.001,
568+
},
569+
},
570+
Object {
571+
"failure": "error on first try",
572+
},
573+
],
574+
},
575+
Object {
576+
"testcase": Array [
577+
Object {
578+
"_attr": Object {
579+
"classname": "foo baz should bar",
580+
"name": "foo baz should bar",
581+
"time": 0.001,
582+
},
583+
},
584+
],
585+
},
586+
],
587+
},
588+
],
589+
}
590+
`);
591+
});
522592
});

constants/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ module.exports = {
1717
JEST_JUNIT_INCLUDE_SHORT_CONSOLE_OUTPUT: 'includeShortConsoleOutput',
1818
JEST_JUNIT_REPORT_TEST_SUITE_ERRORS: 'reportTestSuiteErrors',
1919
JEST_JUNIT_NO_STACK_TRACE: "noStackTrace",
20+
JEST_JUNIT_PUBLISH_TRANSIENT_RETRY_FAILURES: "publishTransientRetryFailures",
2021
JEST_USE_PATH_FOR_SUITE_NAME: 'usePathForSuiteName',
2122
JEST_JUNIT_TEST_CASE_PROPERTIES_JSON_FILE: 'testCasePropertiesFile',
2223
JEST_JUNIT_TEST_CASE_PROPERTIES_DIR: 'testCasePropertiesDirectory',
@@ -39,6 +40,7 @@ module.exports = {
3940
includeShortConsoleOutput: 'false',
4041
reportTestSuiteErrors: 'false',
4142
noStackTrace: 'false',
43+
publishTransientRetryFailures: 'false',
4244
testCasePropertiesFile: 'junitTestCaseProperties.js',
4345
testCasePropertiesDirectory: process.cwd(),
4446
testSuitePropertiesFile: 'junitProperties.js',

utils/buildJsonResults.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,24 @@ module.exports = function (report, appDirectory, options, rootDir = null) {
265265

266266
// Iterate through test cases
267267
suite.testResults.forEach((tc) => {
268+
if (options.publishTransientRetryFailures === 'true' && Array.isArray(tc.retryReasons)) {
269+
const retriedFailureCases = tc.retryReasons.map(rr => {
270+
const tCase = generateTestCase(
271+
options,
272+
suiteOptions,
273+
tc,
274+
filepath,
275+
filename,
276+
suiteTitle,
277+
displayName,
278+
getTestCaseProperties
279+
);
280+
tCase.testcase.push({"failure": strip(rr)});
281+
return tCase;
282+
});
283+
testSuite.testsuite.push(...retriedFailureCases);
284+
}
285+
268286
const testCase = generateTestCase(
269287
options,
270288
suiteOptions,

0 commit comments

Comments
 (0)