Skip to content

Commit 3c68d50

Browse files
author
daivinhtran
committed
Add outputFile config and tests
1 parent 12da1a2 commit 3c68d50

File tree

5 files changed

+49
-7
lines changed

5 files changed

+49
-7
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ Reporter options should also be strings exception for suiteNameTemplate, classNa
5959
| `JEST_SUITE_NAME` | `suiteName` | `name` attribute of `<testsuites>` | `"jest tests"` | N/A
6060
| `JEST_JUNIT_OUTPUT_DIR` | `outputDirectory` | Directory to save the output. | `process.cwd()` | N/A
6161
| `JEST_JUNIT_OUTPUT_NAME` | `outputName` | File name for the output. | `"junit.xml"` | N/A
62+
| `JEST_JUNIT_OUTPUT_FILE` | `outputFile` | Fullpath for the output. If defined, `outputDirectory` and `outputName` will be overridden | `undefined` | N/A
6263
| `JEST_JUNIT_UNIQUE_OUTPUT_NAME` | `uniqueOutputName` | Create unique file name for the output `junit-${uuid}.xml`, overrides `outputName` | `false` | N/A
6364
| `JEST_JUNIT_SUITE_NAME` | `suiteNameTemplate` | Template string for `name` attribute of the `<testsuite>`. | `"{title}"` | `{title}`, `{filepath}`, `{filename}`, `{displayName}`
6465
| `JEST_JUNIT_CLASSNAME` | `classNameTemplate` | Template string for the `classname` attribute of `<testcase>`. | `"{classname} {title}"` | `{classname}`, `{title}`, `{suitename}`, `{filepath}`, `{filename}`, `{displayName}`

__tests__/testResultProcessor.test.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,4 +60,32 @@ describe('jest-junit', () => {
6060
const xmlDoc = libxmljs.parseXml(fs.writeFileSync.mock.calls[0][1]);
6161
expect(xmlDoc).toBeTruthy();
6262
});
63+
64+
65+
it('should generate xml at the output filepath defined by JEST_JUNIT_OUTPUT_FILE', () => {
66+
process.env.JEST_JUNIT_OUTPUT_FILE = 'path_to_output/output_name.xml'
67+
const noFailingTestsReport = require('../__mocks__/no-failing-tests.json');
68+
testResultProcessor(noFailingTestsReport);
69+
70+
// Ensure fs.writeFileSync is called
71+
expect(fs.writeFileSync).toHaveBeenCalledTimes(1);
72+
73+
// Ensure file would have been generated
74+
expect(fs.writeFileSync).toHaveBeenLastCalledWith(
75+
expect.stringMatching(/path_to_output\S+\output_name.xml/), expect.any(String)
76+
);
77+
});
78+
79+
it('should generate xml at the output filepath defined by outputFile config', () => {
80+
const noFailingTestsReport = require('../__mocks__/no-failing-tests.json');
81+
testResultProcessor(noFailingTestsReport, {outputFile: 'path_to_output/output_name.xml' });
82+
83+
// Ensure fs.writeFileSync is called
84+
expect(fs.writeFileSync).toHaveBeenCalledTimes(1);
85+
86+
// Ensure file would have been generated
87+
expect(fs.writeFileSync).toHaveBeenLastCalledWith(
88+
expect.stringMatching(/path_to_output\S+\output_name.xml/), expect.any(String)
89+
);
90+
});
6391
});

constants/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ module.exports = {
55
JEST_SUITE_NAME: 'suiteName',
66
JEST_JUNIT_OUTPUT_DIR: 'outputDirectory',
77
JEST_JUNIT_OUTPUT_NAME: 'outputName',
8+
JEST_JUNIT_OUTPUT_FILE: 'outputFile',
89
JEST_JUNIT_UNIQUE_OUTPUT_NAME: 'uniqueOutputName',
910
JEST_JUNIT_CLASSNAME: 'classNameTemplate',
1011
JEST_JUNIT_SUITE_NAME: 'suiteNameTemplate',

index.js

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const path = require('path');
77

88
const buildJsonResults = require('./utils/buildJsonResults');
99
const getOptions = require('./utils/getOptions');
10+
const getOutputPath = require('./utils/getOutputPath');
1011

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

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

26-
// Set output to use new outputDirectory and fallback on original output
27-
const outputName = (options.uniqueOutputName === 'true') ? getOptions.getUniqueOutputName() : options.outputName
28-
const output = path.join(options.outputDirectory, outputName);
29-
30-
const finalOutput = getOptions.replaceRootDirInOutput(jestRootDir, output);
27+
let outputPath = getOutputPath(options, jestRootDir);
3128

3229
// Ensure output path exists
33-
mkdirp.sync(path.dirname(finalOutput));
30+
mkdirp.sync(path.dirname(outputPath));
3431

3532
// Write data to file
36-
fs.writeFileSync(finalOutput, xml(jsonResults, { indent: ' ', declaration: true }));
33+
fs.writeFileSync(outputPath, xml(jsonResults, { indent: ' ', declaration: true }));
3734

3835
// Jest 18 compatibility
3936
return report;

utils/getOutputPath.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
const path = require('path');
2+
const getOptions = require('./getOptions');
3+
4+
module.exports = (options, jestRootDir) => {
5+
// Override outputName and outputDirectory with outputFile if outputFile is defined
6+
let output = options.outputFile;
7+
if (!output) {
8+
// Set output to use new outputDirectory and fallback on original output
9+
const outputName = (options.uniqueOutputName === 'true') ? getOptions.getUniqueOutputName() : options.outputName
10+
output = path.join(options.outputDirectory, outputName);
11+
}
12+
13+
const finalOutput = getOptions.replaceRootDirInOutput(jestRootDir, output);
14+
return finalOutput;
15+
};

0 commit comments

Comments
 (0)