mocha-reporter-sonarqube
Advanced tools
Comparing version 2.0.6 to 2.1.0
@@ -1,2 +0,2 @@ | ||
import { type MochaOptions, type Runner, reporters } from 'mocha'; | ||
import { type MochaOptions, Runner, reporters } from 'mocha'; | ||
declare class SonarQubeReporter extends reporters.Base { | ||
@@ -9,3 +9,2 @@ private tests; | ||
private readonly _onTestEnd; | ||
private readonly _onFailedTest; | ||
private readonly _onEnd; | ||
@@ -12,0 +11,0 @@ private static _generateTestCaseTag; |
@@ -6,47 +6,8 @@ "use strict"; | ||
const mocha_1 = require("mocha"); | ||
const xmldom_1 = require("@xmldom/xmldom"); | ||
const utils_1 = require("./utils"); | ||
class SonarQubeReporter extends mocha_1.reporters.Base { | ||
tests = {}; | ||
stream = process.stdout; | ||
constructor(runner, options) { | ||
super(runner, options); | ||
this.tests = {}; | ||
this.stream = process.stdout; | ||
// istanbul ignore next | ||
// eslint-disable-next-line @typescript-eslint/class-methods-use-this | ||
this._onStreamError = (e) => { | ||
console.error(e); | ||
throw e; | ||
}; | ||
this._onStart = () => { | ||
this.tests = {}; | ||
}; | ||
this._onTestEnd = (test) => { | ||
const { file } = test; | ||
if (file !== undefined) { | ||
this.tests[file] = this.tests[file] ?? []; | ||
this.tests[file].push(test); | ||
} | ||
}; | ||
// eslint-disable-next-line @typescript-eslint/class-methods-use-this | ||
this._onFailedTest = (test, err) => { | ||
test.err = err; | ||
}; | ||
this._onEnd = () => { | ||
const dom = new xmldom_1.DOMImplementation(); | ||
const doc = dom.createDocument(null, null); | ||
const xmlPI = doc.createProcessingInstruction('xml', 'version="1.0" encoding="UTF-8"'); | ||
const testExecutions = doc.createElement('testExecutions'); | ||
testExecutions.setAttribute('version', '1'); | ||
doc.appendChild(xmlPI); | ||
doc.appendChild(doc.createTextNode('\n')); | ||
doc.appendChild(testExecutions); | ||
Object.keys(this.tests).forEach((fileName) => { | ||
const tests = this.tests[fileName]; | ||
const file = doc.createElement('file'); | ||
file.setAttribute('path', fileName); | ||
testExecutions.appendChild(file); | ||
tests.forEach((test) => file.appendChild(SonarQubeReporter._generateTestCaseTag(doc, test))); | ||
}); | ||
const serializer = new xmldom_1.XMLSerializer(); | ||
this.stream.write(serializer.serializeToString(doc)); | ||
}; | ||
const reporterOptions = options?.reporterOptions; | ||
@@ -67,27 +28,49 @@ // istanbul ignore else | ||
} | ||
runner.on('start', this._onStart); | ||
runner.on('test end', this._onTestEnd); | ||
runner.on('fail', this._onFailedTest); | ||
runner.on('end', this._onEnd); | ||
runner.on(mocha_1.Runner.constants.EVENT_RUN_BEGIN, this._onStart); | ||
runner.on(mocha_1.Runner.constants.EVENT_TEST_END, this._onTestEnd); | ||
runner.on(mocha_1.Runner.constants.EVENT_RUN_END, this._onEnd); | ||
this.stream.on('error', this._onStreamError); | ||
} | ||
static _generateTestCaseTag(doc, test) { | ||
const testCase = doc.createElement('testCase'); | ||
testCase.setAttribute('name', test.titlePath().join(' » ')); | ||
testCase.setAttribute('duration', /* istanbul ignore next */ test.duration ? test.duration.toFixed() : '0'); | ||
if (test.state === 'passed') { | ||
// Do nothing | ||
// istanbul ignore next | ||
// eslint-disable-next-line @typescript-eslint/class-methods-use-this | ||
_onStreamError = (e) => { | ||
console.error(e); | ||
throw e; | ||
}; | ||
_onStart = () => { | ||
this.tests = {}; | ||
}; | ||
_onTestEnd = (test) => { | ||
const { file } = test; | ||
if (file !== undefined) { | ||
this.tests[file] = this.tests[file] ?? []; | ||
this.tests[file].push(test); | ||
} | ||
else if (test.state === 'failed') { | ||
const failure = doc.createElement('failure'); | ||
failure.setAttribute('message', /* istanbul ignore next */ test.err?.message ?? ''); | ||
failure.appendChild(doc.createTextNode(/* istanbul ignore next */ test.err?.stack ?? '')); | ||
testCase.appendChild(failure); | ||
}; | ||
_onEnd = () => { | ||
let output = '<?xml version="1.0" encoding="UTF-8"?>\n<testExecutions version="1">'; | ||
Object.keys(this.tests).forEach((fileName) => { | ||
const tests = this.tests[fileName]; | ||
output += (0, utils_1.tag)('file', { path: fileName }, false); | ||
tests.forEach((test) => { | ||
output += SonarQubeReporter._generateTestCaseTag(test); | ||
}); | ||
output += '</file>'; | ||
}); | ||
output += '</testExecutions>'; | ||
this.stream.write(output); | ||
}; | ||
static _generateTestCaseTag(test) { | ||
let inner; | ||
if (test.state === 'failed') { | ||
inner = (0, utils_1.tag)('failure'); | ||
} | ||
else if (test.state === 'pending') { | ||
const skipped = doc.createElement('skipped'); | ||
skipped.setAttribute('message', 'Pending test'); | ||
testCase.appendChild(skipped); | ||
inner = (0, utils_1.tag)('skipped'); | ||
} | ||
return testCase; | ||
const attrs = { | ||
name: test.titlePath().join(' » '), | ||
duration: test.duration ? test.duration.toFixed() : '0', | ||
}; | ||
return (0, utils_1.tag)('testCase', attrs, !inner, inner); | ||
} | ||
@@ -94,0 +77,0 @@ static createWriteStream(filename) { |
{ | ||
"name": "mocha-reporter-sonarqube", | ||
"description": "Mocha reporter for SonarQube", | ||
"version": "2.0.6", | ||
"version": "2.1.0", | ||
"main": "dist/lib/index.js", | ||
@@ -9,2 +9,3 @@ "scripts": { | ||
"lint": "eslint .", | ||
"typecheck": "tsc --noEmit", | ||
"test": "mocha test", | ||
@@ -30,15 +31,13 @@ "test:coverage": "nyc mocha test", | ||
"@myrotvorets/buffer-stream": "^1.4.0", | ||
"@myrotvorets/eslint-config-myrotvorets-ts": "^2.22.6", | ||
"@types/mocha": "^10.0.3", | ||
"@types/node": "^20.8.9", | ||
"eslint-plugin-mocha": "^10.2.0", | ||
"mocha": "^10.2.0", | ||
"@myrotvorets/eslint-config-myrotvorets-ts": "^2.26.0", | ||
"@types/mocha": "^10.0.7", | ||
"@types/node": "^22.0.0", | ||
"@xmldom/xmldom": "^0.8.10", | ||
"eslint-plugin-mocha": "^10.4.3", | ||
"mocha": "^10.6.0", | ||
"mocha-reporter-gha": "^1.1.1", | ||
"nyc": "^15.1.0", | ||
"ts-node": "^10.9.1", | ||
"typescript": "^5.2.2" | ||
"nyc": "^17.0.0", | ||
"ts-node": "^10.9.2", | ||
"typescript": "^5.5.3" | ||
}, | ||
"dependencies": { | ||
"@xmldom/xmldom": "^0.8.0" | ||
}, | ||
"repository": { | ||
@@ -45,0 +44,0 @@ "type": "git", |
@@ -5,3 +5,3 @@ # mocha-reporter-sonarqube | ||
See https://docs.sonarqube.org/latest/analysis/generic-test/#header-2 for details. | ||
See https://docs.sonarsource.com/sonarqube/latest/analyzing-source-code/test-coverage/generic-test-data/#generic-test-execution for details. | ||
@@ -20,2 +20,2 @@ ## Installation | ||
if `filename` option is not specified, the report will be written to `process.stdout`. | ||
if the `filename` option is not specified, the report will be written to `process.stdout`. |
Sorry, the diff of this file is not supported yet
0
10017
11
100
- Removed@xmldom/xmldom@^0.8.0
- Removed@xmldom/xmldom@0.8.10(transitive)