UNPKG

@testim/testim-cli

Version:

Command line interface for running Testing on you CI

92 lines (81 loc) 2.99 kB
var fs = require('fs'); var xml2js = require("xml2js"); var Promise = require('promise'); var report = function(testResults, projectId) { function createTestCaseObject(testResult, testId, projectId){ function getTestUrl(projectId, testId, resultId) { var testUrl = ""; if(projectId && testId) { var domain = "testim.io/"; testUrl = "https://" + domain + "app/#/project/" + projectId + "/test/" + testId; if(resultId) { testUrl += "/result/" + resultId; } } return encodeURI(testUrl); }; var testResultUrl = getTestUrl(projectId, testId, testResult.resultId); var testResultObject = { "$" : { "name": testResult.name, "classname" : "testim.io.test" } }; if(testResult.success !== true){ testResultObject.error = { _ : testResult.failureReason + " More info at: " + testResultUrl, $ : { "message" : "step failed" } }; } testResultObject['system-out'] = testResultUrl; return testResultObject; } testResults = testResults || {}; var testCount = Object.keys(testResults).length; var failedCount = Object.keys(testResults).filter(function(testId){ return testResults[testId].testResults.success !== true; }).length; var testResultObject = { testsuites : { testsuite: { $: { "name": "selenium run", "tests": testCount, "failure": failedCount, "timestamp": Date.now() }, testcase: Object.keys(testResults).map(function (testId) { return createTestCaseObject(testResults[testId].testResults, testId, projectId); }) } } }; var builder = new xml2js.Builder(); var jUnitXmlReporter = builder.buildObject(testResultObject); return Promise.resolve(jUnitXmlReporter); }; function createResultsReport(reportFile, projectId, testResults) { function reportResults(reportText) { return new Promise(function (resolve) { if (reportFile) { fs.writeFile(reportFile, reportText, function (err) { if (err) { console.error("could not save report file", reportFile, err); } else { console.log("file saved to", reportFile); } resolve(testResults); }); } else { console.log(reportText); resolve(testResults); } }); } return report(testResults, projectId).then(reportResults); } module.exports = { createResultsReport : createResultsReport };