UNPKG

testcafe-reporter-junit

Version:
139 lines (108 loc) 4.52 kB
module.exports = () => ({ noColors: true, report: '', startTime: null, uaList: null, currentFixtureName: null, testCount: 0, skipped: 0, currentFixtureMeta: null, reportTaskStart (startTime, userAgents, testCount) { this.startTime = startTime; this.uaList = userAgents.join(', '); this.testCount = testCount; }, reportFixtureStart (name, path, meta) { this.currentFixtureName = this.escapeHtml(name); this.currentFixtureMeta = meta ? Object.assign({}, meta) : null; }, _renderMetadata (testMeta) { var metadata = Object.assign({}, this.currentFixtureMeta || {}, testMeta || {}); var attrs = ''; Object.keys(metadata).forEach(key => { var value = metadata[key]; if (typeof value === 'undefined') return; attrs += ` ${this.escapeHtml(key)}="${this.escapeHtml(String(value))}"`; }); return attrs; }, _renderErrors (testRunInfo) { this.report += this.indentString('<failure>\n', 4); this.report += this.indentString('<![CDATA[', 4); testRunInfo.errs.forEach((err, idx) => { err = this.formatError(err, `${idx + 1}) `); this.report += '\n'; this.report += this.indentString(err, 6); this.report += '\n'; }); this.report += this.indentString(']]>\n', 4); this.report += this.indentString('</failure>\n', 4); }, _renderSystemOut (testRunInfo) { this.report += this.indentString('<system-out>\n', 4); this.report += this.indentString('<![CDATA[', 4); if (testRunInfo.unstable) this.report += this.indentString('\n(unstable)\n', 6); if (testRunInfo.screenshotPath) this.report += this.indentString(`\n(screenshots: ${testRunInfo.screenshotPath})\n`, 6); this.report += this.indentString(']]>\n', 4); this.report += this.indentString('</system-out>\n', 4); }, reportTestDone (name, testRunInfo, meta) { var hasErr = !!testRunInfo.errs.length; var metadataAttributes = this._renderMetadata(meta); var openTag = `<testcase classname="${this.currentFixtureName}" ` + `name="${this.escapeHtml(name)}" time="${testRunInfo.durationMs / 1000}"`; openTag += metadataAttributes; openTag += '>\n'; this.report += this.indentString(openTag, 2); if (testRunInfo.skipped) { this.skipped++; this.report += this.indentString('<skipped/>\n', 4); } else if (hasErr) this._renderErrors(testRunInfo); if (testRunInfo.screenshotPath || testRunInfo.unstable) this._renderSystemOut(testRunInfo); this.report += this.indentString('</testcase>\n', 2); }, _renderWarnings (warnings) { this.setIndent(2) .write('<system-out>') .newline() .write('<![CDATA[') .newline() .setIndent(4) .write(`Warnings (${warnings.length}):`) .newline(); warnings.forEach(msg => { this.setIndent(4) .write('--') .newline() .setIndent(0) .write(this.indentString(msg, 6)) .newline(); }); this.setIndent(2) .write(']]>') .newline() .write('</system-out>') .newline(); }, reportTaskDone (endTime, passed, warnings) { var name = `TestCafe Tests: ${this.escapeHtml(this.uaList)}`; var failures = this.testCount - passed; var time = (endTime - this.startTime) / 1000; this.write('<?xml version="1.0" encoding="UTF-8" ?>') .newline() .write(`<testsuite name="${name}" tests="${this.testCount}" failures="${failures}" skipped="${this.skipped}"` + ` errors="${failures}" time="${time}" timestamp="${endTime.toUTCString()}" >`) .newline() .write(this.report); if (warnings.length) this._renderWarnings(warnings); this.setIndent(0) .write('</testsuite>'); } });