From dee46ff7ce105a9f34502b8555563708b5bf5f22 Mon Sep 17 00:00:00 2001 From: Fumiaki MATSUSHIMA Date: Sun, 13 Jan 2019 16:48:34 +0900 Subject: [PATCH] Add file attr if JEST_JUNIT_ADD_FILE_ATTRIBUTE is true --- README.md | 1 + __tests__/buildJsonResults.test.js | 17 ++++++++++++++++- constants/index.js | 2 ++ utils/buildJsonResults.js | 4 ++++ 4 files changed, 23 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 6697123..9aec0e1 100644 --- a/README.md +++ b/README.md @@ -65,6 +65,7 @@ Reporter options should also be strings exception for suiteNameTemplate, classNa | `JEST_JUNIT_CLASSNAME` | Template string for the `classname` attribute of ``. | `"{classname} {title}"` | `{classname}`, `{title}`, `{filepath}`, `{filename}`, `{displayName}` | `JEST_JUNIT_TITLE` | Template string for the `name` attribute of ``. | `"{classname} {title}"` | `{classname}`, `{title}`, `{filepath}`, `{filename}`, `{displayName}` | `JEST_JUNIT_ANCESTOR_SEPARATOR` | Character(s) used to join the `describe` blocks. | `" "` | N/A +| `JEST_JUNIT_ADD_FILE_ATTRIBUTE` | Add file attribute to the output. This config is primarily for Circle CI. This setting provides richer details but may break on other CI platforms. | `false` | N/A | `JEST_USE_PATH_FOR_SUITE_NAME` | **DEPRECATED. Use `suiteNameTemplate` instead.** Use file path as the `name` attribute of `` | `"false"` | N/A diff --git a/__tests__/buildJsonResults.test.js b/__tests__/buildJsonResults.test.js index b424817..1352a7e 100644 --- a/__tests__/buildJsonResults.test.js +++ b/__tests__/buildJsonResults.test.js @@ -155,4 +155,19 @@ describe('buildJsonResults', () => { expect(jsonResults).toMatchSnapshot(); }); -}); \ No newline at end of file + it('should not return the file name by default', () => { + const noFailingTestsReport = require('../__mocks__/no-failing-tests.json'); + const jsonResults = buildJsonResults(noFailingTestsReport, '/', constants.DEFAULT_OPTIONS); + expect(jsonResults.testsuites[1].testsuite[1].testcase[0]._attr.file).toBe(undefined); + }); + + it('should return the file name when addFileAttribute is "true"', () => { + const noFailingTestsReport = require('../__mocks__/no-failing-tests.json'); + const jsonResults = buildJsonResults(noFailingTestsReport, '/', + Object.assign({}, constants.DEFAULT_OPTIONS, { + addFileAttribute: "true" + })); + expect(jsonResults.testsuites[1].testsuite[1].testcase[0]._attr.file).toBe('path/to/test/__tests__/foo.test.js'); + }); + +}); diff --git a/constants/index.js b/constants/index.js index ba97e91..905b6ea 100644 --- a/constants/index.js +++ b/constants/index.js @@ -12,6 +12,7 @@ module.exports = { JEST_JUNIT_SUITE_NAME: 'suiteNameTemplate', JEST_JUNIT_TITLE: 'titleTemplate', JEST_JUNIT_ANCESTOR_SEPARATOR: 'ancestorSeparator', + JEST_JUNIT_ADD_FILE_ATTRIBUTE: 'addFileAttribute', JEST_USE_PATH_FOR_SUITE_NAME: 'usePathForSuiteName', }, DEFAULT_OPTIONS: { @@ -24,6 +25,7 @@ module.exports = { titleTemplate: '{classname} {title}', ancestorSeparator: ' ', usePathForSuiteName: 'false', + addFileAttribute: 'false', }, CLASSNAME_VAR: 'classname', FILENAME_VAR: 'filename', diff --git a/utils/buildJsonResults.js b/utils/buildJsonResults.js index b88506f..242b29a 100644 --- a/utils/buildJsonResults.js +++ b/utils/buildJsonResults.js @@ -124,6 +124,10 @@ module.exports = function (report, appDirectory, options) { }] }; + if (options.addFileAttribute === 'true') { + testCase.testcase[0]._attr.file = filepath; + } + // Write out all failure messages as tags // Nested underneath tag if (tc.status === 'failed') {