Skip to content

Commit

Permalink
adding suite specific custom testcase properties
Browse files Browse the repository at this point in the history
Adds a new configuration option called "propertiesFile" which lets you
configure a javascript file. This file then accepts "testsuite" as a parameter and returns a object which is then attached to the respective testsuite.
  • Loading branch information
kunalpanchal committed Aug 14, 2023
1 parent ebabb33 commit 924ac44
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 5 deletions.
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 = (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:
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, 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 [];
}
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, testSuite.testsuite, 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'
};
};

0 comments on commit 924ac44

Please sign in to comment.