forked from jest-community/jest-junit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
72 lines (54 loc) · 2.52 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
'use strict';
const xml = require('xml');
const mkdirp = require('mkdirp');
const fs = require('fs');
const path = require('path');
const jestValidate = require('jest-validate');
const buildJsonResults = require('./utils/buildJsonResults');
const getOptions = require('./utils/getOptions');
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);
const jsonResults = buildJsonResults(report, fs.realpathSync(process.cwd()), options);
// Set output to use new outputDirectory and fallback on original output
const output = options.outputDirectory === null ? options.output : path.join(options.outputDirectory, options.outputName);
const finalOutput = getOptions.replaceRootDirInOutput(jestRootDir, output);
// Ensure output path exists
mkdirp.sync(path.dirname(finalOutput));
// Write data to file
fs.writeFileSync(finalOutput, 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://facebook.github.io/jest/docs/configuration.html#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);
jestValidate.logValidationWarning('testResultsProcessor support is deprecated. Please use jest reporter. See https://github.com/jest-community/jest-junit#usage', newConfig);
return processor(globalConfig);
}
this._globalConfig = globalConfig;
this._options = options;
this.onRunComplete = (contexts, results) => {
processor(results, this._options, this._globalConfig.rootDir);
};
}
module.exports = JestJUnit;