UNPKG

@applitools/bongo

Version:
77 lines (63 loc) 2.55 kB
const fs = require('fs') const path = require('path') const chalk = require('chalk') const fetch = require('node-fetch') const {glob} = require('glob') const {processXunit} = require('./process-xunit') const processReport = { xunit: processXunit, } async function sendTestReport({reportId, name, params, metaPath, resultPath, resultFormat, sandbox = false}) { if (!resultPath) resultPath = './coverage-test-report.xml' if (!metaPath) metaPath = './coverage-tests-metadata.json' if (!resultFormat) { resultFormat = resultPath.endsWith('.xml') ? 'xunit' : 'mocha' } let metadata try { metadata = require(path.resolve(metaPath)) } catch (error) { console.log(chalk.red('No metadata file found')) } const [resolvedResultPath] = await glob(resultPath, {absolute: true}) const file = fs.readFileSync(resolvedResultPath, {encoding: 'utf-8'}) const report = { id: reportId, sdk: name, results: resultFormat === 'raw' ? JSON.parse(file).results : processReport[resultFormat](file, {metadata, params}), sandbox, } console.log('Report was successfully generated!\n') if (report.id) console.log(`${chalk.bold('Report ID')}: ${report.id}\n`) const total = report.results.length const {passed, failed, skipped, generic, custom} = report.results.reduce( (counts, result) => { if (result.isGeneric) counts.generic += 1 else counts.custom += 1 if (result.isSkipped) counts.skipped += 1 else if (result.passed) counts.passed += 1 else counts.failed += 1 return counts }, {passed: 0, failed: 0, skipped: 0, generic: 0, custom: 0}, ) console.log( `${chalk.bold(`${total}`.padEnd(3))} total including ${chalk.blue.bold( `${generic} generic`, )} and ${chalk.magenta.bold(`${custom} custom`)} test(s)`, ) console.log(chalk.green(`${chalk.bold(`${passed}`.padEnd(3))} passed test(s)`)) console.log(chalk.cyan(`${chalk.bold(`${skipped}`.padEnd(3))} skipped test(s)`)) console.log(chalk.red(`${chalk.bold(`${failed}`.padEnd(3))} failed test(s)`)) process.stdout.write(`\nSending report to QA dashboard ${sandbox ? '(sandbox)' : ''}... `) const response = await fetch('http://applitools-quality-server.herokuapp.com/result', { method: 'post', headers: {'Content-Type': 'application/json'}, body: JSON.stringify(report), }) if (response.status !== 200) { console.error(await response.text()) throw new Error(`Request failed with status ${response.status}`) } } module.exports = {sendTestReport}