forked from jest-community/jest-junit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
87 lines (67 loc) · 2.54 KB
/
index.js
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
'use strict';
const xml = require('xml');
const mkdirp = require('mkdirp');
const fs = require('fs');
const path = require('path');
const buildJsonResults = require('./utils/buildJsonResults');
const getOptions = require('./utils/getOptions');
const getOutputPath = require('./utils/getOutputPath');
// Store console results from onTestResult to later
// append to result
const consoleBuffer = {};
const processor = (report, reporterOptions = {}, jestRootDir = null) => {
// If jest-junit is used as a reporter allow for reporter options
// to be used. Env and package.json will override.
const options = getOptions.options(reporterOptions);
report.testResults.forEach((t, i) => {
t.console = consoleBuffer[t.testFilePath];
});
const jsonResults = buildJsonResults(
report,
fs.realpathSync(process.cwd()),
options,
jestRootDir
);
let outputPath = getOutputPath(options, jestRootDir);
// Ensure output path exists
mkdirp.sync(path.dirname(outputPath));
// Write data to file
fs.writeFileSync(outputPath, xml(jsonResults, { indent: ' ', declaration: true }));
// Jest 18 compatibility
return report;
};
/*
At the end of ALL of the test suites this method is called
It's responsible for generating a single junit.xml file which
Represents the status of the test runs
Expected input and workflow documentation here:
https://jestjs.io/docs/en/configuration#testresultsprocessor-string
Intended output (junit XML) documentation here:
http://help.catchsoftware.com/display/ET/JUnit+Format
*/
// This is an old school "class" in order
// for the constructor to be invoked statically and via "new"
// so we can support both testResultsProcessor and reporters
// TODO: refactor to es6 class after testResultsProcessor support is removed
function JestJUnit (globalConfig, options) {
// See if constructor was invoked statically
// which indicates jest-junit was invoked as a testResultsProcessor
// and show deprecation warning
if (globalConfig.hasOwnProperty('testResults')) {
const newConfig = JSON.stringify({
reporters: ['jest-junit']
}, null, 2);
return processor(globalConfig);
}
this._globalConfig = globalConfig;
this._options = options;
this.onTestResult = (test, testResult, aggregatedResult) => {
if (testResult.console && testResult.console.length > 0) {
consoleBuffer[testResult.testFilePath] = testResult.console;
}
};
this.onRunComplete = (contexts, results) => {
processor(results, this._options, this._globalConfig.rootDir);
};
}
module.exports = JestJUnit;