Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Possibility of uniqueOutputName to use a specified prefix rather than just junit-uuid.xml #203

Merged
merged 3 commits into from Apr 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -63,7 +63,7 @@ Reporter options should also be strings exception for suiteNameTemplate, classNa
| `JEST_JUNIT_OUTPUT_DIR` | `outputDirectory` | Directory to save the output. | `process.cwd()` | N/A
| `JEST_JUNIT_OUTPUT_NAME` | `outputName` | File name for the output. | `"junit.xml"` | N/A
| `JEST_JUNIT_OUTPUT_FILE` | `outputFile` | Fullpath for the output. If defined, `outputDirectory` and `outputName` will be overridden | `undefined` | N/A
| `JEST_JUNIT_UNIQUE_OUTPUT_NAME` | `uniqueOutputName` | Create unique file name for the output `junit-${uuid}.xml`, overrides `outputName` | `false` | N/A
| `JEST_JUNIT_UNIQUE_OUTPUT_NAME` | `uniqueOutputName` | Create unique file name for the output leveraging the `outputName` as a prefix if given `${outputName}-${uuid}.xml` or a default of `junit-${uuid}.xml` if `outputName` is not specified, overrides `outputName` | `false` | N/A
| `JEST_JUNIT_SUITE_NAME` | `suiteNameTemplate` | Template string for `name` attribute of the `<testsuite>`. | `"{title}"` | `{title}`, `{filepath}`, `{filename}`, `{displayName}`
| `JEST_JUNIT_CLASSNAME` | `classNameTemplate` | Template string for the `classname` attribute of `<testcase>`. | `"{classname} {title}"` | `{classname}`, `{title}`, `{suitename}`, `{filepath}`, `{filename}`, `{displayName}`
| `JEST_JUNIT_TITLE` | `titleTemplate` | Template string for the `name` attribute of `<testcase>`. | `"{classname} {title}"` | `{classname}`, `{title}`, `{filepath}`, `{filename}`, `{displayName}`
Expand Down
17 changes: 17 additions & 0 deletions __tests__/getOptions.test.js
Expand Up @@ -31,6 +31,23 @@ describe('getOptions', () => {
});
});

describe('getUniqueOutputName', () =>{
const defaultPrefix = 'junit'

it(`should return default ${defaultPrefix} value if given no preferred prefix`, () => {
const uniqueOutput = getOptions.getUniqueOutputName()
expect(uniqueOutput).toContain(defaultPrefix)
})

it(`should return apply custom prefix value if given prefix`, () => {
const customPrefix = "foo"
const uniqueOutput = getOptions.getUniqueOutputName(customPrefix)
expect(uniqueOutput).not.toContain(defaultPrefix)
expect(uniqueOutput).toContain(customPrefix)
})

})

describe('replace <rootDir>', () => {
it('should replace <rootDir> in output path', () => {
const rootDir = 'testRootDir';
Expand Down
8 changes: 7 additions & 1 deletion __tests__/testResultProcessor.test.js
Expand Up @@ -47,14 +47,20 @@ describe('jest-junit', () => {

it('should generate valid xml with unique name', () => {
process.env.JEST_JUNIT_UNIQUE_OUTPUT_NAME = 'true'

const outputPrefix = "foo"

process.env.JEST_JUNIT_OUTPUT_NAME = outputPrefix;
const noFailingTestsReport = require('../__mocks__/no-failing-tests.json');
testResultProcessor(noFailingTestsReport);

// Ensure fs.writeFileSync is called
expect(fs.writeFileSync).toHaveBeenCalledTimes(1);

const expectedOutputRegex = new RegExp(`${outputPrefix}-\\S+.xml`, "g")

// Ensure file would have been generated
expect(fs.writeFileSync).toHaveBeenLastCalledWith(expect.stringMatching(/junit-\S+\.xml/), expect.any(String));
expect(fs.writeFileSync).toHaveBeenLastCalledWith(expect.stringMatching(expectedOutputRegex), expect.any(String));

// Ensure generated file is valid xml
const xmlDoc = libxmljs.parseXml(fs.writeFileSync.mock.calls[0][1]);
Expand Down
5 changes: 3 additions & 2 deletions utils/getOptions.js
Expand Up @@ -49,8 +49,9 @@ function replaceRootDirInOutput(rootDir, output) {
return rootDir !== null ? replaceRootDirInPath(rootDir, output) : output;
}

function getUniqueOutputName() {
return `junit-${uuid()}.xml`
function getUniqueOutputName(outputName) {
const outputPrefix = outputName ? outputName : 'junit'
return `${outputPrefix}-${uuid()}.xml`
}

module.exports = {
Expand Down
4 changes: 2 additions & 2 deletions utils/getOutputPath.js
Expand Up @@ -6,12 +6,12 @@ module.exports = (options, jestRootDir) => {
let output = options.outputFile;
if (!output) {
// Set output to use new outputDirectory and fallback on original output
const outputName = (options.uniqueOutputName === 'true') ? getOptions.getUniqueOutputName() : options.outputName
const outputName = (options.uniqueOutputName === 'true') ? getOptions.getUniqueOutputName(options.outputName) : options.outputName
output = getOptions.replaceRootDirInOutput(jestRootDir, options.outputDirectory);
const finalOutput = path.join(output, outputName);
return finalOutput;
}

const finalOutput = getOptions.replaceRootDirInOutput(jestRootDir, output);
return finalOutput;
};