Skip to content

Commit

Permalink
feat(reporter): ignore empty test suites to prevent non-compliant rep…
Browse files Browse the repository at this point in the history
…ort generation

Skip the generation of SonarQube XML reports when the test suite contains zero tests, adhering to compliance standards that require test results to be present in the reports. This ensures no empty reports are created, which would not comply with the expected report structure.
  • Loading branch information
dikkadev committed Nov 16, 2023
1 parent 9e334f7 commit 09e06e4
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 4 deletions.
8 changes: 7 additions & 1 deletion src/SonarQubeCypressReporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const {
formatTest,
writeFile
} = require('./ReporterUtils');
const ReporterUtils = require('./ReporterUtils');


// Mocha runner events
Expand Down Expand Up @@ -40,6 +41,7 @@ class SonarQubeCypressReporter {
constructor(runner, options) {
this.options = Object.assign(defaultOptions, options.reporterOptions);
this.specFilename = 'none';
this.totalTestCount = 0;

runner.once(EVENT_RUN_END, () => {
this.onDone(runner);
Expand All @@ -56,6 +58,10 @@ class SonarQubeCypressReporter {
.attribute('version', 1)
.element('file');
this.traverseSuite(node, runner.suite);
if (this.totalTestCount === 0) {
ReporterUtils.warn('Test suite is empty, not writing XML File')
return;
}
const xml = node.end({ pretty: true });
writeFile(this.specFilename, xml, this.options);
}
Expand All @@ -67,7 +73,7 @@ class SonarQubeCypressReporter {
* @param {object} suite a Mocha suite
*/
traverseSuite(node, suite) {

this.totalTestCount += suite.tests.length;
if (suite.parent && suite.parent.root) {
this.specFilename = extractSpecFromSuite(suite, { useAbsoluteSpecPath: false });
const specFilePath = extractSpecFromSuite(suite, this.options);
Expand Down
69 changes: 66 additions & 3 deletions test/specs/Reporter.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ const {
overwriteConfig,
readFile,
verifyReportExists,
verifyReport
verifyReport,
verifyReportDoesNotExist
} = require('./TestUtils');

// Folder that will contains all generated files from tests
Expand All @@ -23,7 +24,7 @@ const testOuputDir = path.resolve('dist', 'test');
* @param {number} minorVersion
* @returns {boolean} true if Cypress version is greater than the expected
*/
const isCypressVersionAtLeast = (majorVersion, minorVersion = 0) => {
const isCypressVersionAtLeast = (majorVersion, minorVersion = 0) => {
const cypressVersion = require('cypress/package.json').version;
const splitted = cypressVersion.split('.');
const cypressMajorVersion = parseInt(splitted[0]);
Expand Down Expand Up @@ -312,6 +313,38 @@ describe('Testing reporter', () => {

});

describe('empty test', () => {

const conditionalTest = isCypressVersionAtLeast(6, 2) ? test : test.skip;
const testDir = path.resolve(testOuputDir, 'empty');
const reportPath = path.resolve(testDir, 'Empty.spec.js.xml');
const nonePath = path.resolve(testDir, 'none.xml');

beforeAll(() => {
cleanOuputDir(testDir);
});

conditionalTest('running Cypress', () => {
const config = overwriteConfig(cypressDefaultConfig, {
reporterOptions: {
outputDir: testDir,
overwrite: false,
preserveSpecsDir: false
}
});
if (isCypressVersionGreaterThanV10()) {
config.spec = '**/Empty.spec.js';
} else {
config.config.testFiles = '**/Empty.spec.js';
}
return cypress.run(config).then(() => {
verifyReportDoesNotExist(reportPath);
verifyReportDoesNotExist(nonePath);
});
}, cypressRunTimeout);

});

describe('component test', () => {

const conditionalTest = isCypressVersionAtLeast(11, 0) ? test : test.skip;
Expand All @@ -331,7 +364,7 @@ describe('Testing reporter', () => {
preserveSpecsDir: false
},
});
config.spec= '**/MyComponent.spec.js';
config.spec = '**/MyComponent.spec.js';

return cypress.run(config).then(() => {
verifyReport(reportPath, config, 'MyComponent.spec.js', true, true);
Expand All @@ -340,4 +373,34 @@ describe('Testing reporter', () => {

});

describe('empty component test', () => {

const conditionalTest = isCypressVersionAtLeast(11, 0) ? test : test.skip;
const testDir = path.resolve(testOuputDir, 'MyEmptyComponent');
const reportPath = path.resolve(testDir, 'MyEmptyComponent.spec.js.xml');
const nonePath = path.resolve(testDir, 'none.xml');

beforeAll(() => {
cleanOuputDir(testDir);
});

conditionalTest('running Cypress', () => {
const config = overwriteConfig(cypressDefaultConfig, {
testingType: 'component',
reporterOptions: {
outputDir: testDir,
overwrite: false,
preserveSpecsDir: false
},
});
config.spec = '**/MyEmptyComponent.spec.js';

return cypress.run(config).then(() => {
verifyReportDoesNotExist(reportPath);
verifyReportDoesNotExist(nonePath);
});
});

});

});
5 changes: 5 additions & 0 deletions test/specs/TestUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ const verifyReportExists = (reportPath) => {
expect(fse.existsSync(reportPath)).toBeTruthy();
};

const verifyReportDoesNotExist = (reportPath) => {
expect(fse.existsSync(reportPath)).toBeFalsy();
};


const verifyGeneratedReport = (reportPath, options, specFileName = 'Sample.spec.js', cypressVersionGreaterThan10 = false, isComponentTest = false) => {
const titleSeparator = options?.titleSeparator ?? ' - ';
Expand Down Expand Up @@ -132,6 +136,7 @@ module.exports = {
overwriteConfig,
readFile,
verifyReportExists,
verifyReportDoesNotExist,
verifyGeneratedReport,
verifyReport
};

0 comments on commit 09e06e4

Please sign in to comment.