cucumber-to-junit
Version:
Converts [Cucumber.js](https://github.com/cucumber/cucumber-js) json results files to JUnit XML.
131 lines (101 loc) • 4.28 kB
JavaScript
;
Object.defineProperty(exports, '__esModule', { value: true });
function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
var jsonFile = _interopDefault(require('jsonfile'));
var builder = _interopDefault(require('junit-report-builder'));
function _unsupportedIterableToArray(o, minLen) {
if (!o) return;
if (typeof o === "string") return _arrayLikeToArray(o, minLen);
var n = Object.prototype.toString.call(o).slice(8, -1);
if (n === "Object" && o.constructor) n = o.constructor.name;
if (n === "Map" || n === "Set") return Array.from(o);
if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen);
}
function _arrayLikeToArray(arr, len) {
if (len == null || len > arr.length) len = arr.length;
for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i];
return arr2;
}
function _createForOfIteratorHelperLoose(o, allowArrayLike) {
var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"];
if (it) return (it = it.call(o)).next.bind(it);
if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") {
if (it) o = it;
var i = 0;
return function () {
if (i >= o.length) return {
done: true
};
return {
done: false,
value: o[i++]
};
};
}
throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
}
var CucumberConverter = /*#__PURE__*/function () {
function CucumberConverter(config) {
this.config = config;
}
var _proto = CucumberConverter.prototype;
_proto.convertToJunit = function convertToJunit(inputFilePath, outputFilePath) {
var cucumberReport = jsonFile.readFileSync(inputFilePath);
for (var _iterator = _createForOfIteratorHelperLoose(cucumberReport), _step; !(_step = _iterator()).done;) {
var feature = _step.value;
this.addTestSuite(feature);
}
builder.writeTo(outputFilePath);
};
_proto.addTestSuite = function addTestSuite(feature) {
var suiteDuration = 0;
var suite = builder.testSuite().name(feature.name);
for (var _iterator2 = _createForOfIteratorHelperLoose(feature.elements), _step2; !(_step2 = _iterator2()).done;) {
var scenario = _step2.value;
suiteDuration += this.addTestCases(scenario, suite);
}
suite.time(suiteDuration);
};
_proto.addTestCases = function addTestCases(scenario, suite) {
if (scenario.type === 'background') {
return 0;
}
var scenarioResult = this.getScenarioResult(scenario);
var testCase = suite.testCase().name(scenario.name).className(scenario.id).time(scenarioResult.duration);
if (scenarioResult.status === 'failed') {
testCase.failure(scenarioResult.failureMessage);
} else if (scenarioResult.status === 'skipped' || scenarioResult.status === 'undefined') {
testCase.skipped();
}
return scenarioResult.duration;
};
_proto.getScenarioResult = function getScenarioResult(scenario) {
var scenarioStatus = 'passed';
var failureMessage = '';
var scenarioDuration = 0;
for (var _iterator3 = _createForOfIteratorHelperLoose(scenario.steps), _step3; !(_step3 = _iterator3()).done;) {
var step = _step3.value;
scenarioDuration += step.result.duration ? step.result.duration / 1000000000 : 0; //ns to seconds;
if (scenarioStatus === 'failed') {
continue;
}
if (step.result.status === 'failed') {
scenarioStatus = 'failed';
failureMessage = step.result.error_message;
} else if (this.config.markUndefinedAsFailed && step.result.status === 'undefined') {
scenarioStatus = 'failed';
failureMessage = 'One or more steps are undefined';
} else {
scenarioStatus = step.result.status;
}
}
return {
status: scenarioStatus,
failureMessage: failureMessage,
duration: scenarioDuration
};
};
return CucumberConverter;
}();
exports.CucumberConverter = CucumberConverter;
//# sourceMappingURL=cucumber-to-junit.cjs.development.js.map