Skip to content

Commit

Permalink
handle test cases pending status
Browse files Browse the repository at this point in the history
  • Loading branch information
santoshshinde2012 committed May 16, 2024
1 parent fdb9e72 commit 5a270b4
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 42 deletions.
21 changes: 13 additions & 8 deletions coverage/lcov.info
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,19 @@ DA:50,1
DA:54,2
LF:27
LH:27
BRDA:14,0,0,1
BRDA:14,0,1,5
BRDA:16,1,0,1
BRDA:16,1,1,4
BRDA:18,2,0,1
BRDA:43,3,0,4
BRF:6
BRH:6
BRDA:12,0,0,3
BRDA:12,0,1,3
BRDA:14,1,0,1
BRDA:14,1,1,5
BRDA:14,2,0,6
BRDA:14,2,1,5
BRDA:16,3,0,1
BRDA:16,3,1,4
BRDA:18,4,0,1
BRDA:22,5,0,3
BRDA:43,6,0,4
BRF:11
BRH:11
end_of_record
TN:
SF:src/lib/SonarReporter.ts
Expand Down
43 changes: 16 additions & 27 deletions coverage/sonar-report.xml
Original file line number Diff line number Diff line change
@@ -1,35 +1,24 @@
<?xml version="1.0" encoding="UTF-8"?>
<testExecutions version="1">
<file path="/tests/lib/SonarReporter.spec.ts">
<testCase name="SonarReporter onTestResult should add test results to testFileResults" duration="0.002">
</testCase>
<testCase name="SonarReporter onRunComplete should write XML file with test results" duration="0.001">
</testCase>
<testCase name="SonarReporter onRunComplete should use default output file if not provided" duration="0.001">
</testCase>
<testCase name="SonarReporter onRunComplete should use provided output file" duration="0">
<testCase name="SonarReporter onTestResult should add test results to testFileResults" duration="0.002" />
<testCase name="SonarReporter onRunComplete should write XML file with test results" duration="0.001" />
<testCase name="SonarReporter onRunComplete should use default output file if not provided - skipped" duration="0" >
<skipped/>
</testCase>
<testCase name="SonarReporter onRunComplete should use default output file if not provided" duration="0.001" />
<testCase name="SonarReporter onRunComplete should use provided output file" duration="0" />
</file>
<file path="/tests/helpers/utils.spec.ts">
<testCase name="createFile function should create file with given data" duration="0.001">
</testCase>
<testCase name="createFile function should handle error when writing file" duration="0">
</testCase>
<testCase name="generateXML generates correct XML with test cases of different statuses" duration="0">
</testCase>
<testCase name="generateXML generates correct XML with test cases of 0 duration" duration="0">
</testCase>
<testCase name="generateXML handles empty testFileResults map" duration="0.001">
</testCase>
<testCase name="generateXML handles empty path" duration="0">
</testCase>
<testCase name="getRelativePath generates relative path correctly" duration="0">
</testCase>
<testCase name="getRelativePath handles 'process.cwd()' in path" duration="0">
</testCase>
<testCase name="getRelativePath handles empty input string" duration="0">
</testCase>
<testCase name="getRelativePath handles empty basePath" duration="0">
</testCase>
<testCase name="createFile function should create file with given data" duration="0.001" />
<testCase name="createFile function should handle error when writing file" duration="0" />
<testCase name="generateXML generates correct XML with test cases of different statuses" duration="0.001" />
<testCase name="generateXML generates correct XML with test cases of 0 duration" duration="0" />
<testCase name="generateXML handles empty testFileResults map" duration="0" />
<testCase name="generateXML handles empty path" duration="0.001" />
<testCase name="getRelativePath generates relative path correctly" duration="0" />
<testCase name="getRelativePath handles 'process.cwd()' in path" duration="0" />
<testCase name="getRelativePath handles empty input string" duration="0" />
<testCase name="getRelativePath handles empty basePath" duration="0" />
</file>
</testExecutions>
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "jest-to-sonar",
"version": "1.1.0",
"version": "1.2.0",
"description": "Convert the Jest test case report to a Sonar generic test execution report.",
"main": "dist/index.js",
"scripts": {
Expand Down
8 changes: 4 additions & 4 deletions src/helpers/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,17 @@ function generateXML(testFileResults: Map<string, ITestCase[]>): string {
xml += ` <file path="${path}">\n`;

for (const test of testCases) {
xml += ` <testCase name="${test.name.replace(/"/g, "'")}" duration="${test.duration / 1000}">\n`;

if (test.status === 'skipped') {
xml += ` <testCase name="${test.name.replace(/"/g, "'")}" duration="${test.duration / 1000}" ${test.status === 'passed' ? '/' : ''}>\n`;
if (test.status === 'skipped' || test.status === 'pending') {
xml += ' <skipped/>\n';
} else if (test.status === 'failed') {
xml += ' <failure/>\n';
} else if (test.status === 'disabled') {
xml += ' <disabled/>\n';
}

xml += ' </testCase>\n';
if (test.status !== 'passed') xml += ' </testCase>\n';
}

xml += ' </file>\n';
Expand Down
11 changes: 11 additions & 0 deletions tests/lib/SonarReporter.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,17 @@ describe('SonarReporter', () => {
expect(generateXMLMock).toHaveBeenCalledWith(reporter['testFileResults']);
});

it.skip('should use default output file if not provided - skipped', () => {
const reporter = new SonarReporter({}, { outputFile: 'sonar-report.xml' });
reporter['testFileResults'] = new Map();
const generateXMLMock = jest.spyOn(utils, 'generateXML').mockReturnValue('<xml></xml>');

reporter.onRunComplete();

expect(fs.writeFileSync).toHaveBeenCalledWith('sonar-report.xml', '<xml></xml>');
expect(generateXMLMock).toHaveBeenCalledWith(reporter['testFileResults']);
});

it('should use default output file if not provided', () => {
const reporter = new SonarReporter({}, { outputFile: 'sonar-report.xml' });
reporter['testFileResults'] = new Map();
Expand Down

0 comments on commit 5a270b4

Please sign in to comment.