From 4788b30e5505868f2ed176432c697fd3892889e4 Mon Sep 17 00:00:00 2001 From: Justyna Olszak Date: Fri, 14 Jan 2022 15:15:43 +0100 Subject: [PATCH] Extensible approach to custom reports --- core/command/report.js | 28 +++++++++++++------ core/util/engineTools.js | 7 ++--- core/util/extendConfig.js | 1 + test/configs/backstop.json | 7 ++++- .../customReports/xrayReport.js | 23 +++++++-------- 5 files changed, 41 insertions(+), 25 deletions(-) rename core/util/writeXrayReport.js => test/configs/backstop_data/engine_scripts/customReports/xrayReport.js (73%) diff --git a/core/command/report.js b/core/command/report.js index 0b3b21d48..c999efe31 100644 --- a/core/command/report.js +++ b/core/command/report.js @@ -7,7 +7,6 @@ const allSettled = require('../util/allSettled'); const fs = require('../util/fs'); const logger = require('../util/logger')('report'); const compare = require('../util/compare/'); -const writeXrayReport = require('../util/writeXrayReport'); function replaceInFile (file, search, replace) { return new Promise((resolve, reject) => { @@ -26,20 +25,33 @@ function replaceInFile (file, search, replace) { }); } +async function processCustomReport (config, reporter) { + const engineScriptsPath = config.engine_scripts; + const customReport = config.customReport; + if (customReport) { + const customReportScript = path.resolve(engineScriptsPath, customReport.script); + if (fs.existsSync(customReportScript)) { + return await require(customReportScript)(config, reporter); + } else { + console.warn('WARNING: reporting script not found: ' + customReportScript); + } + } +} + function writeReport (config, reporter) { const promises = []; if (config.report && config.report.indexOf('CI') > -1 && config.ciReport.format === 'junit') { promises.push(writeJunitReport(config, reporter)); - } else if (config.report && config.report.indexOf('Xray') > -1) { - promises.push(writeXrayReport(config, reporter)) - } else { - if (config.report && config.report.indexOf('json') > -1) { - promises.push(writeJsonReport(config, reporter)); - } - promises.push(writeBrowserReport(config, reporter)); } + if (config.report && config.report.indexOf('json') > -1) { + promises.push(writeJsonReport(config, reporter)); + } + + promises.push(writeBrowserReport(config, reporter)); + promises.push(processCustomReport(config, reporter)); + return allSettled(promises); } diff --git a/core/util/engineTools.js b/core/util/engineTools.js index 4aaf5d8ac..1cb642ced 100644 --- a/core/util/engineTools.js +++ b/core/util/engineTools.js @@ -113,14 +113,11 @@ function generateTestPair (config, scenario, viewport, variantOrScenarioLabelSaf test: testFilePath, selector: selector, fileName: fileName, - label: scenario.label, requireSameDimensions: getRequireSameDimensions(scenario, config), misMatchThreshold: getMisMatchThreshHold(scenario, config), - url: scenario.url, - referenceUrl: scenario.referenceUrl, expect: getScenarioExpect(scenario), - viewportLabel: viewport.label, - metadata: scenario.metadata + veportLabel: viewport.label, + ...scenario }; } diff --git a/core/util/extendConfig.js b/core/util/extendConfig.js index 92790bf36..fd91ecd38 100644 --- a/core/util/extendConfig.js +++ b/core/util/extendConfig.js @@ -23,6 +23,7 @@ function extendConfig (config, userConfig) { config.asyncCompareLimit = userConfig.asyncCompareLimit; config.backstopVersion = version; config.dockerCommandTemplate = userConfig.dockerCommandTemplate; + config.customReport = userConfig.customReport; return config; } diff --git a/test/configs/backstop.json b/test/configs/backstop.json index 420fe8261..ef2a839b8 100644 --- a/test/configs/backstop.json +++ b/test/configs/backstop.json @@ -30,7 +30,7 @@ "postInteractionWait": 0, "selectors": [], "selectorExpansion": true, - "misMatchThreshold" : 0.1, + "misMatchThreshold": 0.1, "requireSameDimensions": true, "metadata": ["TEST-1"] } @@ -47,6 +47,11 @@ "engineOptions": { "args": ["--no-sandbox"] }, + "customReport": { + "script": "customReports/xrayReport.js", + "reportLocation": "backstop_data/xray_report", + "reportName": "xrayReport.json" + }, "asyncCaptureLimit": 5, "asyncCompareLimit": 50, "debug": false, diff --git a/core/util/writeXrayReport.js b/test/configs/backstop_data/engine_scripts/customReports/xrayReport.js similarity index 73% rename from core/util/writeXrayReport.js rename to test/configs/backstop_data/engine_scripts/customReports/xrayReport.js index 5c103fda9..07fd82aa2 100644 --- a/core/util/writeXrayReport.js +++ b/test/configs/backstop_data/engine_scripts/customReports/xrayReport.js @@ -1,12 +1,11 @@ -const fs = require('../util/fs'); +const { writeFile } = require('fs'); +const { ensureDir } = require('fs-extra'); const path = require('path'); -const logger = require('../util/logger')('Xray report'); const _ = require('lodash'); const cloneDeep = require('lodash/cloneDeep'); -const { stat } = require('fs'); +const util = require('util'); module.exports = function (config, reporter) { - const jsonReporter = cloneDeep(reporter); function toAbsolute (p) { return path.isAbsolute(p) ? p : path.join(config.projectPath, p); @@ -44,7 +43,6 @@ module.exports = function (config, reporter) { ); } - debugger; return transformedTestCases; } @@ -56,19 +54,22 @@ module.exports = function (config, reporter) { return results; } - logger.log('Writing Xray json report'); + const jsonReporter = cloneDeep(reporter); + const ensureDirPromise = util.promisify(ensureDir); + const writeFilePromise = util.promisify(writeFile); - return fs.ensureDir(toAbsolute(config.json_report)).then(function () { + return ensureDirPromise(toAbsolute(config.customReport.reportLocation)).then(function () { const res = transformToXrayJson(jsonReporter.tests); + const reportPath = toAbsolute(path.join(config.customReport.reportLocation, config.customReport.reportName)); - return fs.writeFile(toAbsolute(config.compareJsonFileName), JSON.stringify(res, null, 2)).then( + return writeFilePromise(reportPath, JSON.stringify(res, null, 2)).then( function () { - logger.log('Wrote Xray Json report to: ' + toAbsolute(config.compareJsonFileName)); + console.log('Wrote Xray report to: ' + reportPath); }, function (err) { - logger.error('Failed writing Xray Json report to: ' + toAbsolute(config.compareJsonFileName)); + console.error('Failed writing Xray report to: ' + reportPath); throw err; } ); }); -}; +}; \ No newline at end of file