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

Generate unique xml file names #108

Merged
merged 3 commits into from
Oct 22, 2019
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
6 changes: 3 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
language: node_js
node_js:
- "6"
- "7"
- "8"
- "9"
- "10"
- "12"
env:
- JEST_VERSION=^22.0.0
- JEST_VERSION=^23.0.0
- JEST_VERSION=^24.0.0
- JEST_VERSION=^24.9.0
script:
- npm run test:ci
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,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_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}`, `{filepath}`, `{filename}`, `{displayName}`
| `JEST_JUNIT_TITLE` | `titleTemplate` | Template string for the `name` attribute of `<testcase>`. | `"{classname} {title}"` | `{classname}`, `{title}`, `{filepath}`, `{filename}`, `{displayName}`
Expand All @@ -84,6 +85,7 @@ Or you can also define a `jest-junit` key in your `package.json`. All are **str
"suiteName": "jest tests",
"outputDirectory": ".",
"outputName": "junit.xml",
"uniqueOutputName": "false"
"classNameTemplate": "{classname}-{title}",
"titleTemplate": "{classname}-{title}",
"ancestorSeparator": " › ",
Expand Down
25 changes: 22 additions & 3 deletions __tests__/testResultProcessor.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,18 @@ jest.mock('fs', () => {
});

const fs = require('fs');
const libxmljs = require("libxmljs");
const libxmljs = require('libxmljs');
const path = require('path');

const testResultProcessor = require('../');

describe('jest-junit', () => {
it('should generate valid xml', () => {

afterEach(() => jest.clearAllMocks())

it('should generate valid xml with default name', () => {
const noFailingTestsReport = require('../__mocks__/no-failing-tests.json');
const result = testResultProcessor(noFailingTestsReport);
testResultProcessor(noFailingTestsReport);

// Ensure fs.writeFileSync is called
expect(fs.writeFileSync).toHaveBeenCalledTimes(1);
Expand All @@ -41,4 +44,20 @@ describe('jest-junit', () => {
const xmlDoc = libxmljs.parseXml(fs.writeFileSync.mock.calls[0][1]);
expect(xmlDoc).toBeTruthy();
});

it('should generate valid xml with unique name', () => {
process.env.JEST_JUNIT_UNIQUE_OUTPUT_NAME = 'true'
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(/junit-\S+\.xml/), expect.any(String));

// Ensure generated file is valid xml
const xmlDoc = libxmljs.parseXml(fs.writeFileSync.mock.calls[0][1]);
expect(xmlDoc).toBeTruthy();
});
});
4 changes: 2 additions & 2 deletions constants/index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
'use strict';

const path = require('path');

module.exports = {
ENVIRONMENT_CONFIG_MAP: {
JEST_SUITE_NAME: 'suiteName',
JEST_JUNIT_OUTPUT_DIR: 'outputDirectory',
JEST_JUNIT_OUTPUT_NAME: 'outputName',
JEST_JUNIT_UNIQUE_OUTPUT_NAME: 'uniqueOutputName',
JEST_JUNIT_CLASSNAME: 'classNameTemplate',
JEST_JUNIT_SUITE_NAME: 'suiteNameTemplate',
JEST_JUNIT_TITLE: 'titleTemplate',
Expand All @@ -19,6 +18,7 @@ module.exports = {
suiteName: 'jest tests',
outputDirectory: process.cwd(),
outputName: 'junit.xml',
uniqueOutputName: 'false',
classNameTemplate: '{classname} {title}',
suiteNameTemplate: '{title}',
titleTemplate: '{classname} {title}',
Expand Down
3 changes: 2 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ 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
const output = path.join(options.outputDirectory, options.outputName);
const outputName = (options.uniqueOutputName === 'true') ? getOptions.getUniqueOutputName() : options.outputName
const output = path.join(options.outputDirectory, outputName);

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

Expand Down
2 changes: 1 addition & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ module.exports = {
"<rootDir>/integration-tests/testResultsProcessor/",
"<rootDir>/integration-tests/reporter/"
],
setupTestFrameworkScriptFile: "<rootDir>/__tests__/lib/setupTests.js"
setupFilesAfterEnv: ["<rootDir>/__tests__/lib/setupTests.js"]
};
11 changes: 6 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"author": "Jason Palmer",
"license": "Apache-2.0",
"engines": {
"node": ">=6.0.0"
"node": ">=8.0.0"
},
"files": [
"index.js",
Expand All @@ -20,13 +20,14 @@
"test:ci": "jest --ci"
},
"dependencies": {
"jest-validate": "^24.0.0",
"jest-validate": "^24.9.0",
"mkdirp": "^0.5.1",
"strip-ansi": "^4.0.0",
"strip-ansi": "^5.2.0",
"uuid": "^3.3.3",
"xml": "^1.0.1"
},
"devDependencies": {
"jest": "^24.0.0",
"libxmljs": "^0.18.4"
"jest": "^24.9.0",
"libxmljs": "^0.19.7"
}
}
8 changes: 7 additions & 1 deletion utils/getOptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

const path = require('path');
const fs = require('fs');
const uuid = require('uuid/v1');

const constants = require('../constants/index');

Expand Down Expand Up @@ -50,11 +51,16 @@ function replaceRootDirInOutput(rootDir, output) {
return rootDir !== null ? replaceRootDirInPath(rootDir, output) : output;
}

function getUniqueOutputName() {
return `junit-${uuid()}.xml`
}

module.exports = {
options: (reporterOptions = {}) => {
return Object.assign({}, constants.DEFAULT_OPTIONS, reporterOptions, getAppOptions(process.cwd()), getEnvOptions());
},
getAppOptions: getAppOptions,
getEnvOptions: getEnvOptions,
replaceRootDirInOutput: replaceRootDirInOutput
replaceRootDirInOutput: replaceRootDirInOutput,
getUniqueOutputName: getUniqueOutputName
};