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 "propertiesFile" for custom properties #196

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 = (suite) => {
return {
name: suite.name,
file: suite.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:
Expand Down
33 changes: 28 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,15 @@ 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);
config.properties = getSetting(config.properties, 'PROPERTIES', null, parsePropertiesFromEnv);
config.toConsole = !!config.toConsole;
config.rootSuiteTitle = config.rootSuiteTitle || 'Root Suite';
config.testsuitesTitle = config.testsuitesTitle || 'Mocha Tests';

if (config.antMode) {
updateOptionsForAntMode(config);
}
Expand Down Expand Up @@ -168,8 +169,19 @@ function parsePropertiesFromEnv(envValue) {
return null;
}

function generateProperties(options) {
var props = options.properties;
function generateProperties(options, suite, propertiesFileRunner) {
var props;

if(propertiesFileRunner) {
const fileProps = propertiesFileRunner(suite);
if(Object.keys(fileProps).length) {
props = [];
Object.keys(fileProps).forEach(key => props[key] = fileProps[key]);
}
}
if(options.properties) {
props = options.properties;
}
if (!props) {
return [];
}
Expand Down Expand Up @@ -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, suite, this.propertiesFileRunner);
if (properties.length || antMode) {
testSuite.testsuite.push({
properties: properties
Expand Down Expand Up @@ -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) {
Expand Down
58 changes: 58 additions & 0 deletions test/mocha-junit-reporter-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
7 changes: 7 additions & 0 deletions test/mock-test-properties.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
"use strict";

module.exports = () => {
return {
CUSTOM_PROPERTY_FROM_FILE: 'XYZ~321'
};
};