From 9f78d0224426c6ba2dfc089afc6990c4afdd1911 Mon Sep 17 00:00:00 2001 From: Marvin ROGER Date: Wed, 20 Apr 2022 18:03:26 +0200 Subject: [PATCH] Add filePathPrefix option --- README.md | 1 + __tests__/buildJsonResults.test.js | 15 +++++++++++++++ constants/index.js | 2 ++ utils/buildJsonResults.js | 2 +- 4 files changed, 19 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 9c4376b..242f2b4 100644 --- a/README.md +++ b/README.md @@ -69,6 +69,7 @@ Reporter options should also be strings exception for suiteNameTemplate, classNa | `JEST_JUNIT_TITLE` | `titleTemplate` | Template string for the `name` attribute of ``. | `"{classname} {title}"` | `{classname}`, `{title}`, `{filepath}`, `{filename}`, `{displayName}` | `JEST_JUNIT_ANCESTOR_SEPARATOR` | `ancestorSeparator` | Character(s) used to join the `describe` blocks. | `" "` | N/A | `JEST_JUNIT_ADD_FILE_ATTRIBUTE` | `addFileAttribute` | Add file attribute to the output (validated on CIRCLE CI and GitLab CI). Must be a string. | `"false"` | N/A +| `JEST_JUNIT_FILE_PATH_PREFIX` | `filePathPrefix` | Prefix to add to the test suite file path. The value will be prefixed using `path.join`. Useful in case of monorepo | `""` | N/A | `JEST_JUNIT_INCLUDE_CONSOLE_OUTPUT` | `includeConsoleOutput` | Adds console output to any testSuite that generates stdout during a test run. | `false` | N/A | `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 | `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 diff --git a/__tests__/buildJsonResults.test.js b/__tests__/buildJsonResults.test.js index 2e19c17..6eed871 100644 --- a/__tests__/buildJsonResults.test.js +++ b/__tests__/buildJsonResults.test.js @@ -364,6 +364,21 @@ describe('buildJsonResults', () => { expect(slash(jsonResults.testsuites[1].testsuite[2].testcase[0]._attr.file)).toBe('path/to/test/__tests__/foo.test.js'); }); + it('should prefix the file name with filePathPrefix', () => { + // Ignore junit errors for this attribute + // It is added for circle-ci and is known to not generate + // jenkins-compatible junit + ignoreJunitErrors = true; + + const noFailingTestsReport = require('../__mocks__/no-failing-tests.json'); + jsonResults = buildJsonResults(noFailingTestsReport, '/', + Object.assign({}, constants.DEFAULT_OPTIONS, { + addFileAttribute: "true", + filePathPrefix: "packages/foobar" + })); + expect(slash(jsonResults.testsuites[1].testsuite[2].testcase[0]._attr.file)).toBe('packages/foobar/path/to/test/__tests__/foo.test.js'); + }); + it('should show output of console if includeConsoleOutput is true', () => { const reportWithConsoleOutput = require('../__mocks__/test-with-console-output.json'); jsonResults = buildJsonResults(reportWithConsoleOutput, '/', diff --git a/constants/index.js b/constants/index.js index 72bcc55..1c7fe5d 100644 --- a/constants/index.js +++ b/constants/index.js @@ -12,6 +12,7 @@ module.exports = { JEST_JUNIT_TITLE: 'titleTemplate', JEST_JUNIT_ANCESTOR_SEPARATOR: 'ancestorSeparator', JEST_JUNIT_ADD_FILE_ATTRIBUTE: 'addFileAttribute', + JEST_JUNIT_FILE_PATH_PREFIX: 'filePathPrefix', JEST_JUNIT_INCLUDE_CONSOLE_OUTPUT: 'includeConsoleOutput', JEST_JUNIT_INCLUDE_SHORT_CONSOLE_OUTPUT: 'includeShortConsoleOutput', JEST_JUNIT_REPORT_TEST_SUITE_ERRORS: 'reportTestSuiteErrors', @@ -31,6 +32,7 @@ module.exports = { ancestorSeparator: ' ', usePathForSuiteName: 'false', addFileAttribute: 'false', + filePathPrefix: '', includeConsoleOutput: 'false', includeShortConsoleOutput: 'false', reportTestSuiteErrors: 'false', diff --git a/utils/buildJsonResults.js b/utils/buildJsonResults.js index a35df5f..d851b5f 100644 --- a/utils/buildJsonResults.js +++ b/utils/buildJsonResults.js @@ -99,7 +99,7 @@ module.exports = function (report, appDirectory, options, rootDir = null) { } // Build variables for suite name - const filepath = path.relative(appDirectory, suite.testFilePath); + const filepath = path.join(suiteOptions.filePathPrefix, path.relative(appDirectory, suite.testFilePath)); const filename = path.basename(filepath); const suiteTitle = suite.testResults[0].ancestorTitles[0]; const displayName = typeof suite.displayName === 'object'