UNPKG

5.88 kBJavaScriptView Raw
1(function (factory) {
2 if (typeof module === "object" && typeof module.exports === "object") {
3 var v = factory(require, exports);
4 if (v !== undefined) module.exports = v;
5 }
6 else if (typeof define === "function" && define.amd) {
7 define(["require", "exports", "tslib", "fs", "path", "../Suite", "./Reporter", "../node/util"], factory);
8 }
9})(function (require, exports) {
10 "use strict";
11 Object.defineProperty(exports, "__esModule", { value: true });
12 var tslib_1 = require("tslib");
13 var fs_1 = require("fs");
14 var path_1 = require("path");
15 var Suite_1 = require("../Suite");
16 var Reporter_1 = tslib_1.__importStar(require("./Reporter"));
17 var util_1 = require("../node/util");
18 var JUnit = (function (_super) {
19 tslib_1.__extends(JUnit, _super);
20 function JUnit(executor, options) {
21 if (options === void 0) { options = {}; }
22 var _this = _super.call(this, executor, options) || this;
23 if (options.filename) {
24 _this.filename = options.filename;
25 if (path_1.dirname(_this.filename) !== '.') {
26 util_1.mkdirp(path_1.dirname(_this.filename));
27 }
28 _this.output = fs_1.createWriteStream(_this.filename);
29 }
30 return _this;
31 }
32 JUnit.prototype.runEnd = function () {
33 var _this = this;
34 var rootNode = new XmlNode('testsuites');
35 this.executor.suites.forEach(function (suite) {
36 rootNode.childNodes.push(createSuiteNode(suite, _this));
37 });
38 var report = '<?xml version="1.0" encoding="UTF-8" ?>' + rootNode.toString() + '\n';
39 this.output.end(report);
40 };
41 tslib_1.__decorate([
42 Reporter_1.eventHandler()
43 ], JUnit.prototype, "runEnd", null);
44 return JUnit;
45 }(Reporter_1.default));
46 exports.default = JUnit;
47 var XmlNode = (function () {
48 function XmlNode(nodeName, attributes) {
49 this.nodeName = '';
50 this.childNodes = [];
51 this.nodeName = nodeName;
52 if (attributes && attributes.childNodes) {
53 this.childNodes = attributes.childNodes;
54 attributes.childNodes = undefined;
55 }
56 this.attributes = attributes || {};
57 }
58 XmlNode.prototype.createNode = function (nodeName, attributes) {
59 var node = new XmlNode(nodeName, attributes);
60 this.childNodes.push(node);
61 return node;
62 };
63 XmlNode.prototype._escape = function (str) {
64 return String(str)
65 .replace(/&/g, '&amp;')
66 .replace(/</g, '&lt;')
67 .replace(/"/g, '&quot;');
68 };
69 XmlNode.prototype._serializeAttributes = function () {
70 var attributes = this.attributes;
71 var nodes = [];
72 for (var key in attributes) {
73 if (attributes[key] != null) {
74 nodes.push(key + '="' + this._escape(attributes[key]) + '"');
75 }
76 }
77 return nodes.length ? ' ' + nodes.join(' ') : '';
78 };
79 XmlNode.prototype._serializeContent = function () {
80 var nodeList = this.childNodes;
81 var nodes = [];
82 for (var i = 0, j = nodeList.length; i < j; ++i) {
83 nodes.push(typeof nodeList[i] === 'string'
84 ? this._escape(nodeList[i])
85 : nodeList[i].toString());
86 }
87 return nodes.join('');
88 };
89 XmlNode.prototype.toString = function () {
90 var children = this._serializeContent();
91 return ('<' +
92 this.nodeName +
93 this._serializeAttributes() +
94 (children.length ? '>' + children + '</' + this.nodeName + '>' : '/>'));
95 };
96 return XmlNode;
97 }());
98 function createChildErrorNode(error, reporter) {
99 return new XmlNode('error', {
100 childNodes: [reporter.formatError(error)],
101 message: error.message,
102 type: error.name
103 });
104 }
105 function createSuiteNode(suite, reporter) {
106 var error = suite.error;
107 var childNodes;
108 if (error && error.lifecycleMethod === 'before') {
109 childNodes = [createChildErrorNode(error, reporter)];
110 }
111 else {
112 childNodes = suite.tests.map(function (test) { return createTestNode(test, reporter); });
113 }
114 if (error && error.lifecycleMethod === 'after') {
115 childNodes.push(createChildErrorNode(error, reporter));
116 }
117 return new XmlNode('testsuite', {
118 name: suite.name || 'Node.js',
119 failures: suite.numFailedTests,
120 skipped: suite.numSkippedTests,
121 tests: suite.numTests,
122 time: suite.timeElapsed / 1000,
123 childNodes: childNodes
124 });
125 }
126 function createTestNode(test, reporter) {
127 if (Suite_1.isSuite(test)) {
128 return createSuiteNode(test, reporter);
129 }
130 var node = new XmlNode('testcase', {
131 name: test.name,
132 time: test.timeElapsed / 1000,
133 status: test.error ? 1 : 0
134 });
135 if (test.error || test.suiteError) {
136 var error = (test.error || test.suiteError);
137 node.createNode(error.name === 'AssertionError' ? 'failure' : 'error', {
138 childNodes: [reporter.formatError(error)],
139 message: error.message,
140 type: error.name
141 });
142 }
143 else if (test.skipped != null) {
144 node.createNode('skipped', {
145 childNodes: [test.skipped]
146 });
147 }
148 return node;
149 }
150});
151//# sourceMappingURL=JUnit.js.map
\No newline at end of file