diff --git a/README.md b/README.md index 63a8668..4ba1979 100644 --- a/README.md +++ b/README.md @@ -76,6 +76,28 @@ var mocha = new Mocha({ }) ``` +You can also alternatively use the config `propertyFile` to use a javascript file instead which should return a object containing the properties. + +```javascript +// testProperties.js +module.exports = (testsuite) => { + return { + name: testsuite[0]._attr.name, + file: testsuite[0]._attr.file, + custom_property: custom_value + } +} +``` + +```javascript +var mocha = new Mocha({ + reporter: 'mocha-junit-reporter', + reporterOptions: { + propertiesFile: 'testProperties.js' + } +}) +``` + ### Results Report Results XML filename can contain `[hash]`, e.g. `./path_to_your/test-results.[hash].xml`. `[hash]` is replaced by MD5 hash of test results XML. This enables support of parallel execution of multiple `mocha-junit-reporter`'s writing test results in separate files. In addition to this these placeholders can also be used: diff --git a/index.js b/index.js index 060ff9d..be17241 100644 --- a/index.js +++ b/index.js @@ -64,6 +64,7 @@ function configureDefaults(options) { var config = findReporterOptions(options); debug('options', config); config.mochaFile = getSetting(config.mochaFile, 'MOCHA_FILE', 'test-results.xml'); + config.propertiesFile = getSetting(config.propertiesFile, 'PROPERTIES_FILE', null); config.attachments = getSetting(config.attachments, 'ATTACHMENTS', false); config.antMode = getSetting(config.antMode, 'ANT_MODE', false); config.jenkinsMode = getSetting(config.jenkinsMode, 'JENKINS_MODE', false); @@ -71,7 +72,7 @@ function configureDefaults(options) { config.toConsole = !!config.toConsole; config.rootSuiteTitle = config.rootSuiteTitle || 'Root Suite'; config.testsuitesTitle = config.testsuitesTitle || 'Mocha Tests'; - + if (config.antMode) { updateOptionsForAntMode(config); } @@ -168,8 +169,19 @@ function parsePropertiesFromEnv(envValue) { return null; } -function generateProperties(options) { - var props = options.properties; +function generateProperties(options, testsuite, propertiesFileRunner) { + var props; + + if(propertiesFileRunner) { + const fileProps = propertiesFileRunner(testsuite); + if(Object.keys(fileProps).length) { + props = []; + Object.keys(fileProps).forEach(key => props[key] = fileProps[key]); + } + } + if(options.properties) { + props = options.properties; + } if (!props) { return []; } @@ -296,7 +308,18 @@ MochaJUnitReporter.prototype.getTestsuiteData = function(suite) { testSuite.testsuite[0]._attr.file = suite.file; } - var properties = generateProperties(this._options); + if (this._options.propertiesFile) { + try { + this.propertiesFileRunner = require(this._options.propertiesFile); + } catch (error) { + if(error.code === 'MODULE_NOT_FOUND') { + debug('Couldnt find the file: ' + error); + } + debug(error); + } + } + + var properties = generateProperties(this._options, testSuite.testsuite, this.propertiesFileRunner); if (properties.length || antMode) { testSuite.testsuite.push({ properties: properties @@ -450,7 +473,7 @@ MochaJUnitReporter.prototype.getXml = function(testsuites) { var totalTests = 0; var stats = this._runner.stats; var antMode = this._options.antMode; - var hasProperties = (!!this._options.properties) || antMode; + var hasProperties = (!!this._options.properties) || (!!this._options.propertiesFile) || antMode; var Date = this._Date; testsuites.forEach(function(suite) { diff --git a/test/mocha-junit-reporter-spec.js b/test/mocha-junit-reporter-spec.js index 45e3b5d..c2b62f9 100644 --- a/test/mocha-junit-reporter-spec.js +++ b/test/mocha-junit-reporter-spec.js @@ -621,6 +621,64 @@ describe('mocha-junit-reporter', function() { }); }); + describe('when "propertiesFile" option is specified',function(){ + it('use properties from the file', function(done){ + var reporter = createReporter({propertiesFile: __dirname + '/mock-test-properties.js',mochaFile: 'test/output/mocha.xml'}); + runTests(reporter, function() { + verifyMochaFile(reporter.runner, filePath, { + properties: [ + { + name: 'CUSTOM_PROPERTY_FROM_FILE', + value: 'XYZ~321' + } + ] + }); + done(); + }); + }); + + it('wrong file name should be ignored', function(done){ + var reporter = createReporter({propertiesFile: 'wrong-file-directory',mochaFile: 'test/output/mocha.xml'}); + runTests(reporter, function() { + verifyMochaFile(reporter.runner, filePath); + done(); + }); + }); + + it('wrong file name should not effect config.properties', function(done){ + process.env.PROPERTIES = 'CUSTOM_PROPERTY:ABC~123'; + var reporter = createReporter({propertiesFile: 'wrong-file-directory',mochaFile: 'test/output/mocha.xml'}); + runTests(reporter, function() { + verifyMochaFile(reporter.runner, filePath, { + properties: [ + { + name: 'CUSTOM_PROPERTY', + value: 'ABC~123' + } + ] + }); + done(); + }); + }); + + it('config.properties to override config.propertiesFile', function(done){ + process.env.PROPERTIES = 'CUSTOM_PROPERTY:ABC~123'; + var reporter = createReporter({propertiesFile: __dirname + '/mock-test-properties.js' ,mochaFile: 'test/output/mocha.xml'}); + runTests(reporter, function() { + verifyMochaFile(reporter.runner, filePath, { + properties: [ + { + name: 'CUSTOM_PROPERTY', + value: 'ABC~123' + } + ] + }); + done(); + }); + }); + + }); + describe('Output', function() { it('skips suites with empty title', function(done) { var reporter = createReporter(); diff --git a/test/mock-test-properties.js b/test/mock-test-properties.js new file mode 100644 index 0000000..64f9175 --- /dev/null +++ b/test/mock-test-properties.js @@ -0,0 +1,7 @@ +"use strict"; + +module.exports = () => { + return { + CUSTOM_PROPERTY_FROM_FILE: 'XYZ~321' + }; +};