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

Add outputFile config and tests #168

Merged
merged 1 commit into from May 25, 2021
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
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -59,6 +59,7 @@ Reporter options should also be strings exception for suiteNameTemplate, classNa
| `JEST_SUITE_NAME` | `suiteName` | `name` attribute of `<testsuites>` | `"jest tests"` | N/A
| `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_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}`
Expand Down
28 changes: 28 additions & 0 deletions __tests__/testResultProcessor.test.js
Expand Up @@ -60,4 +60,32 @@ describe('jest-junit', () => {
const xmlDoc = libxmljs.parseXml(fs.writeFileSync.mock.calls[0][1]);
expect(xmlDoc).toBeTruthy();
});


it('should generate xml at the output filepath defined by JEST_JUNIT_OUTPUT_FILE', () => {
process.env.JEST_JUNIT_OUTPUT_FILE = 'path_to_output/output_name.xml'
const noFailingTestsReport = require('../__mocks__/no-failing-tests.json');
testResultProcessor(noFailingTestsReport);

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

// Ensure file would have been generated
expect(fs.writeFileSync).toHaveBeenLastCalledWith(
expect.stringMatching(/path_to_output\S+\output_name.xml/), expect.any(String)
);
});

it('should generate xml at the output filepath defined by outputFile config', () => {
const noFailingTestsReport = require('../__mocks__/no-failing-tests.json');
testResultProcessor(noFailingTestsReport, {outputFile: 'path_to_output/output_name.xml' });

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

// Ensure file would have been generated
expect(fs.writeFileSync).toHaveBeenLastCalledWith(
expect.stringMatching(/path_to_output\S+\output_name.xml/), expect.any(String)
);
});
});
1 change: 1 addition & 0 deletions constants/index.js
Expand Up @@ -5,6 +5,7 @@ module.exports = {
JEST_SUITE_NAME: 'suiteName',
JEST_JUNIT_OUTPUT_DIR: 'outputDirectory',
JEST_JUNIT_OUTPUT_NAME: 'outputName',
JEST_JUNIT_OUTPUT_FILE: 'outputFile',
JEST_JUNIT_UNIQUE_OUTPUT_NAME: 'uniqueOutputName',
JEST_JUNIT_CLASSNAME: 'classNameTemplate',
JEST_JUNIT_SUITE_NAME: 'suiteNameTemplate',
Expand Down
11 changes: 4 additions & 7 deletions index.js
Expand Up @@ -7,6 +7,7 @@ const path = require('path');

const buildJsonResults = require('./utils/buildJsonResults');
const getOptions = require('./utils/getOptions');
const getOutputPath = require('./utils/getOutputPath');

// Store console results from onTestResult to later
// append to result
Expand All @@ -23,17 +24,13 @@ const processor = (report, reporterOptions = {}, jestRootDir = null) => {

const jsonResults = buildJsonResults(report, fs.realpathSync(process.cwd()), options);

// Set output to use new outputDirectory and fallback on original output
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This block is refactored into ./utils.getOutputPath.js for cleaniness. I can move it back if you prefer it that way.

const outputName = (options.uniqueOutputName === 'true') ? getOptions.getUniqueOutputName() : options.outputName
const output = path.join(options.outputDirectory, outputName);

const finalOutput = getOptions.replaceRootDirInOutput(jestRootDir, output);
let outputPath = getOutputPath(options, jestRootDir);

// Ensure output path exists
mkdirp.sync(path.dirname(finalOutput));
mkdirp.sync(path.dirname(outputPath));

// Write data to file
fs.writeFileSync(finalOutput, xml(jsonResults, { indent: ' ', declaration: true }));
fs.writeFileSync(outputPath, xml(jsonResults, { indent: ' ', declaration: true }));

// Jest 18 compatibility
return report;
Expand Down
15 changes: 15 additions & 0 deletions utils/getOutputPath.js
@@ -0,0 +1,15 @@
const path = require('path');
const getOptions = require('./getOptions');

module.exports = (options, jestRootDir) => {
// Override outputName and outputDirectory with outputFile if outputFile is defined
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
output = path.join(options.outputDirectory, outputName);
}

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