From 35a489360544877476ba2671263b170bf446350b Mon Sep 17 00:00:00 2001 From: Matt Lowry Date: Mon, 25 Jun 2018 14:39:59 +0100 Subject: [PATCH] test: Local generate report (#43) * fix: removing uneeded config from existing tests * test: generate report local * feat: exit code 1 if fs fails --- .../{compare.test.js => compareLocal.test.js} | 0 .../generateReportConfig.json | 13 +++++++ .../generateReportLocal.test.js | 37 +++++++++++++++++++ .../snap/{snap.test.js => snapLocal.test.js} | 2 +- .../updateBaseline/updateBaselineConfig.json | 2 - ...ne.test.js => updateBaselineLocal.test.js} | 0 src/generateReport.js | 13 +++++-- 7 files changed, 60 insertions(+), 7 deletions(-) rename e2eTests/compare/{compare.test.js => compareLocal.test.js} (100%) create mode 100644 e2eTests/generateHtmlReport/generateReportConfig.json create mode 100644 e2eTests/generateHtmlReport/generateReportLocal.test.js rename e2eTests/snap/{snap.test.js => snapLocal.test.js} (94%) rename e2eTests/updateBaseline/{updateBaseline.test.js => updateBaselineLocal.test.js} (100%) diff --git a/e2eTests/compare/compare.test.js b/e2eTests/compare/compareLocal.test.js similarity index 100% rename from e2eTests/compare/compare.test.js rename to e2eTests/compare/compareLocal.test.js diff --git a/e2eTests/generateHtmlReport/generateReportConfig.json b/e2eTests/generateHtmlReport/generateReportConfig.json new file mode 100644 index 0000000..f5e590c --- /dev/null +++ b/e2eTests/generateHtmlReport/generateReportConfig.json @@ -0,0 +1,13 @@ +{ + "gridUrl": "http://selenium.com:4444/wd/hub", + "baseline": "./e2eTests/generateHtmlReport/baseline", + "latest": "./e2eTests/generateHtmlReport/mockImages", + "generatedDiffs": "./e2eTests/generateHtmlReport/generatedDiffs", + "report": "./e2eTests/generateHtmlReport/reports", + "scenarios": [ + { + "url": "http:/google.com/", + "label": "homepage" + }] + } + diff --git a/e2eTests/generateHtmlReport/generateReportLocal.test.js b/e2eTests/generateHtmlReport/generateReportLocal.test.js new file mode 100644 index 0000000..9ff831a --- /dev/null +++ b/e2eTests/generateHtmlReport/generateReportLocal.test.js @@ -0,0 +1,37 @@ +/* globals expect */ + +import path from 'path'; +import fs from 'fs'; +import { execSync } from 'child_process'; +import config from './generateReportConfig'; + +describe('e2e Tests for generating html report locally', () => { + let dirPath; + + beforeEach(() => { + dirPath = path.resolve(config.report); + + if (fs.existsSync(dirPath)) { + const files = fs.readdirSync(dirPath); + files.forEach(file => fs.unlinkSync(`${dirPath}/${file}`)); + fs.rmdirSync(dirPath); + } + }); + it('generates the report', () => { + let exitCode = 0; + + try { + const stdout = execSync( + 'node ./lib/bin/run.js generate-report --browser chrome --config e2eTests/generateHtmlReport/generateReportConfig.json' + ).toString(); + //pipe stdout to Jest console + console.log(stdout); + } catch (error) { + exitCode = error.status; + } + + expect(exitCode).toEqual(0); + const latestDirFiles = fs.readdirSync(dirPath); + expect(latestDirFiles).toEqual(['index.html']); + }); +}); diff --git a/e2eTests/snap/snap.test.js b/e2eTests/snap/snapLocal.test.js similarity index 94% rename from e2eTests/snap/snap.test.js rename to e2eTests/snap/snapLocal.test.js index 4449264..0ce6bf9 100644 --- a/e2eTests/snap/snap.test.js +++ b/e2eTests/snap/snapLocal.test.js @@ -5,7 +5,7 @@ import fs from 'fs'; import { execSync } from 'child_process'; import config from './snapConfig'; -describe('e2e Tests taking snaps', () => { +describe('e2e Tests taking snaps locally', () => { let dirPath; beforeEach(() => { diff --git a/e2eTests/updateBaseline/updateBaselineConfig.json b/e2eTests/updateBaseline/updateBaselineConfig.json index 0d16a5c..d6d1b32 100644 --- a/e2eTests/updateBaseline/updateBaselineConfig.json +++ b/e2eTests/updateBaseline/updateBaselineConfig.json @@ -10,8 +10,6 @@ { "url": "http://cps-render-ci.elb.tnl-dev.ntch.co.uk/", "label": "homepage", - "height": 2400, - "width": 1024, "removeSelectors": ["#ad-header", ".AD"], "cookies": [ { diff --git a/e2eTests/updateBaseline/updateBaseline.test.js b/e2eTests/updateBaseline/updateBaselineLocal.test.js similarity index 100% rename from e2eTests/updateBaseline/updateBaseline.test.js rename to e2eTests/updateBaseline/updateBaselineLocal.test.js diff --git a/src/generateReport.js b/src/generateReport.js index d194e42..57af790 100644 --- a/src/generateReport.js +++ b/src/generateReport.js @@ -54,10 +54,15 @@ const writeReport = (config, reportsData) => { const reportPresentation = compileTemplate({ reportsData }); const reportDir = path.resolve(config.report); - if (!fs.existsSync(reportDir)) fs.mkdirSync(reportDir); - fs.writeFileSync(`${reportDir}/index.html`, reportPresentation); - logger.info('generate-report', 'successfully created report!'); - return `${reportDir}/index.html`; + try { + if (!fs.existsSync(reportDir)) fs.mkdirSync(reportDir); + fs.writeFileSync(`${reportDir}/index.html`, reportPresentation); + logger.info('generate-report', 'successfully created report!'); + return `${reportDir}/index.html`; + } catch (err) { + logger.error(err); + process.exitCode = 1; + } }; const generateRemoteReport = async config => {