forked from CasualBot/jest-sonar-reporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
51 lines (37 loc) · 1.51 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import xml from 'xml';
const mkdirp = require('mkdirp'); // eslint-disable-line
import * as fs from 'fs';
import * as path from 'path';
import buildXmlReport from './src/utils/buildXmlReport';
import getOptions from './src/utils/getOptions';
import getOutputPath from './src/utils/getOutputPath';
const consoleBuffer: any = {};
const processor = (report: any, reporterOptions: any = {}, jestRootDir = null) => {
const options = getOptions.options(reporterOptions);
report.testResults.forEach((t: any, i: any) => {
t.console = consoleBuffer[t.testFilePath];
});
const outputPath = getOutputPath(options, jestRootDir);
mkdirp.sync(path.dirname(outputPath));
fs.writeFileSync(outputPath, xml(buildXmlReport(report, options), {declaration: false, indent: ' '}));
return report;
};
function JestSonar(this: any, globalConfig: any, options: any): void {
if (globalConfig.hasOwnProperty('testResults')) { // eslint-disable-line
const newConfig = JSON.stringify({
reporters: ['@casualbot/jest-sonar-reporter']
}, null, 2);
return processor(globalConfig);
}
this._globalConfig = globalConfig;
this._options = options;
this.onTestResult = (test: any, testResult: any, aggregatedResult: any) => {
if (testResult.console && testResult.console.length > 0) {
consoleBuffer[testResult.testFilePath] = testResult.console;
}
};
this.onRunComplete = (contexts: any, results: any) => {
processor(results, this._options, this._globalConfig.rootDir);
};
}
module.exports = JestSonar;