From 063eb65b2aba43a1018afdb66025becd1326a3f5 Mon Sep 17 00:00:00 2001 From: Revanth Kumar Date: Wed, 8 Dec 2021 12:16:44 -0600 Subject: [PATCH 1/7] Added results report functionality --- venia-integration-tests/package.json | 1 + venia-integration-tests/report-results.js | 57 +++++++++++++++++++++++ 2 files changed, 58 insertions(+) create mode 100755 venia-integration-tests/report-results.js diff --git a/venia-integration-tests/package.json b/venia-integration-tests/package.json index 9a9f1d1e5c..5c1a79dd21 100644 --- a/venia-integration-tests/package.json +++ b/venia-integration-tests/package.json @@ -11,6 +11,7 @@ "test:devTest": "jest", "test:firefox": "cypress open --browser firefox --config-file cypress.config.json", "test:headless": "./run-tests.js", + "posttest:headless": "./report-results.js", "test:run": "cypress run --browser chrome --config-file cypress.config.json" }, "author": "Magento Commerce", diff --git a/venia-integration-tests/report-results.js b/venia-integration-tests/report-results.js new file mode 100755 index 0000000000..ab8f7280fd --- /dev/null +++ b/venia-integration-tests/report-results.js @@ -0,0 +1,57 @@ +#!/usr/bin/env node + +let summary; +const failures = []; + +const processTests = (tests, testsFile) => { + const failedMessages = tests.filter(test => test.fail).map(test => `${testsFile} \n\n ${test.title}: \n\n\t ${test.err.estack}`); + + return failedMessages.join('\n'); +} + +try { + summary = require('./cypress-test-results.json'); +} catch (e) { + throw new Error('Unable to retrieve Cypress results. Please re-run the tests.') +} + +const testFailures = summary.stats.failures; + +const hasFailed = (result) => { + return result.suites.some(suite => suite.failures.length > 0); +} + +if (testFailures === 0) { + // no test failures + return; +} else { + // test failures, lets document them + const { results } = summary; + + results.filter(hasFailed).map(result => { + if (result.tests.length) { + // test failures + failures.push(processTests(result.tests, result.fullFile)); + } + if (result.suites.length) { + // suite failures + failures.push( + result.suites + .filter(suite => suite.failures.length) + .map(suite => processTests(suite.tests, result.fullFile)) + ); + } + }) +} + +const failSummary = failures.join('\n\n -------------------------------- \n\n') + +if (failSummary) { + console.error( + 'Cypress tests in the following files did not pass 😔. \n\n\n' + + failures.length + ' failures \n\n\n' + + failSummary + '\n\n' + ); +} else { + console.log('Cypress tests passed 👍'); +} \ No newline at end of file From d89beef22571d75b09de147f4a52db5bd903f969 Mon Sep 17 00:00:00 2001 From: Revanth Kumar Date: Wed, 8 Dec 2021 13:44:58 -0600 Subject: [PATCH 2/7] Minor --- venia-integration-tests/report-results.js | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/venia-integration-tests/report-results.js b/venia-integration-tests/report-results.js index ab8f7280fd..0904a27203 100755 --- a/venia-integration-tests/report-results.js +++ b/venia-integration-tests/report-results.js @@ -1,7 +1,6 @@ #!/usr/bin/env node let summary; -const failures = []; const processTests = (tests, testsFile) => { const failedMessages = tests.filter(test => test.fail).map(test => `${testsFile} \n\n ${test.title}: \n\n\t ${test.err.estack}`); @@ -23,8 +22,12 @@ const hasFailed = (result) => { if (testFailures === 0) { // no test failures + console.log('All Cypress tests passed 👍 \n'); + return; } else { + const failures = []; + // test failures, lets document them const { results } = summary; @@ -42,16 +45,12 @@ if (testFailures === 0) { ); } }) -} -const failSummary = failures.join('\n\n -------------------------------- \n\n') + const failSummary = failures.join('\n\n -------------------------------- \n\n') -if (failSummary) { console.error( 'Cypress tests in the following files did not pass 😔. \n\n\n' + failures.length + ' failures \n\n\n' + failSummary + '\n\n' ); -} else { - console.log('Cypress tests passed 👍'); } \ No newline at end of file From f9728e440decfff4863d66e4f836f2bb6a34d39e Mon Sep 17 00:00:00 2001 From: Revanth Kumar Date: Thu, 9 Dec 2021 16:12:30 -0600 Subject: [PATCH 3/7] Added table display --- venia-integration-tests/report-results.js | 72 ++++++++++++++++++++--- 1 file changed, 65 insertions(+), 7 deletions(-) diff --git a/venia-integration-tests/report-results.js b/venia-integration-tests/report-results.js index 0904a27203..66c579514e 100755 --- a/venia-integration-tests/report-results.js +++ b/venia-integration-tests/report-results.js @@ -3,7 +3,7 @@ let summary; const processTests = (tests, testsFile) => { - const failedMessages = tests.filter(test => test.fail).map(test => `${testsFile} \n\n ${test.title}: \n\n\t ${test.err.estack}`); + const failedMessages = tests.filter(test => test.fail).map(test => `${testsFile} \n\n${test.title}: \n\n${test.err.estack}`); return failedMessages.join('\n'); } @@ -20,9 +20,69 @@ const hasFailed = (result) => { return result.suites.some(suite => suite.failures.length > 0); } +const getTestStatuses = (result) => { + let passedTests = 0 + let failedTests = 0 + let totalDuration = 0; + + try { + if (result.tests.length) { + passedTests = passedTests + result.passes.length + failedTests = failedTests + result.failures.length + totalDuration = totalDuration + result.duration + } + + if (result.suites.length) { + result.suites.forEach(suite => { + if (suite.tests.length) { + passedTests = passedTests + suite.passes.length + failedTests = failedTests + suite.failures.length + totalDuration = totalDuration + suite.duration + } + + if (suite.suites.length) { + suite.suites.forEach(subSuite => { + const subSuiteResult = getTestStatuses(subSuite) + + passedTests = passedTests + subSuiteResult.passedTests + failedTests = failedTests + subSuiteResult.failedTests + totalDuration = totalDuration + subSuiteResult.totalDuration + }) + } + }) + } + } catch (e) { + console.error(e) + } + + return { + passedTests, + failedTests, + totalDuration + } +} + +const generateTestSummary = (result) => { + const testFile = result.fullFile + const { passedTests, failedTests, totalDuration } = getTestStatuses(result) + + return { + Spec: testFile, + Status: failedTests > 0 ? ' ❌ ' : ' ✅ ', + Passed: passedTests, + Failed: failedTests, + Duration: totalDuration + } +} + +const parsedSummary = summary.results.map(generateTestSummary); + +console.table(parsedSummary); +console.log('\n'); + if (testFailures === 0) { // no test failures - console.log('All Cypress tests passed 👍 \n'); + console.log('\x1b[32m', 'All Cypress tests passed 👍', '\x1b[0m', '\n\n'); return; } else { @@ -48,9 +108,7 @@ if (testFailures === 0) { const failSummary = failures.join('\n\n -------------------------------- \n\n') - console.error( - 'Cypress tests in the following files did not pass 😔. \n\n\n' + - failures.length + ' failures \n\n\n' + - failSummary + '\n\n' - ); + console.log('Cypress tests in the following files did not pass 😔 \n\n'); + console.log('\x1b[43m', `${failures.length} failures`, '\x1b[0m', '\n\n'); + console.log('\x1b[31m', `${failSummary} \n\n`, '\x1b[0m'); } \ No newline at end of file From 6a3ad07839e068c54faed7e83feebc403698866f Mon Sep 17 00:00:00 2001 From: Revanth Kumar Date: Thu, 9 Dec 2021 16:13:06 -0600 Subject: [PATCH 4/7] Delete old combined report before creating new one --- venia-integration-tests/run-tests.js | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/venia-integration-tests/run-tests.js b/venia-integration-tests/run-tests.js index 35bf35e159..ed3b58bead 100755 --- a/venia-integration-tests/run-tests.js +++ b/venia-integration-tests/run-tests.js @@ -68,22 +68,21 @@ if (port) { // run docker on local instance console.log(`Running tests on local instance ${baseUrl}`); - dockerCommand = `docker run --rm -v ${ - process.env.PWD - }:/venia-integration-tests -w /venia-integration-tests --entrypoint=cypress cypress/included:8.3.1 run --browser chrome --config baseUrl=https://host.docker.internal:${port},screenshotOnRunFailure=false --config-file cypress.config.json --env updateSnapshots=${update} --headless --reporter mochawesome --reporter-options reportDir=cypress/results,overwrite=false,html=false,json=true`; + dockerCommand = `docker run --rm -v ${process.env.PWD + }:/venia-integration-tests -w /venia-integration-tests --entrypoint=cypress cypress/included:8.3.1 run --browser chrome --config baseUrl=https://host.docker.internal:${port},screenshotOnRunFailure=false --config-file cypress.config.json --env updateSnapshots=${update} --headless --reporter mochawesome --reporter-options reportDir=cypress/results,overwrite=false,html=false,json=true`; } else { // run docker on remote instance console.log(`Running tests on remote instance ${baseUrl}`); - dockerCommand = `docker run --rm -v ${ - process.env.PWD - }:/venia-integration-tests -w /venia-integration-tests --entrypoint=cypress cypress/included:8.3.1 run --browser chrome --config baseUrl=${baseUrl},screenshotOnRunFailure=false --config-file cypress.config.json --env updateSnapshots=${update} --headless --reporter mochawesome --reporter-options reportDir=cypress/results,overwrite=false,html=false,json=true`; + dockerCommand = `docker run --rm -v ${process.env.PWD + }:/venia-integration-tests -w /venia-integration-tests --entrypoint=cypress cypress/included:8.3.1 run --browser chrome --config baseUrl=${baseUrl},screenshotOnRunFailure=false --config-file cypress.config.json --env updateSnapshots=${update} --headless --reporter mochawesome --reporter-options reportDir=cypress/results,overwrite=false,html=false,json=true`; } const start = process.hrtime(); // remove old test results rmSync('cypress/results', { recursive: true, force: true }); +rmSync('cypress-test-results.json', { force: true }); for (let i = 0; i < threads; i++) { const filesToTest = files.slice(testsPerRun * i, testsPerRun * (i + 1)); @@ -113,7 +112,7 @@ for (let i = 0; i < threads; i++) { console.log( `docker run ${i + - 1} exited with ${code} code in ${timeTaken} seconds` + 1} exited with ${code} code in ${timeTaken} seconds` ); if (Object.values(dockerRuns).every(r => r.completed)) { @@ -137,7 +136,7 @@ for (let i = 0; i < threads; i++) { }; } -process.on('SIGINT', function() { +process.on('SIGINT', function () { console.log('Received kill signal. Killing all cypress tests. \n'); exec( From e8b7c52be49826eeb0488adb09a956171771b6e5 Mon Sep 17 00:00:00 2001 From: Revanth Kumar Date: Fri, 10 Dec 2021 15:04:07 -0600 Subject: [PATCH 5/7] Prettier fixes --- venia-integration-tests/report-results.js | 69 +++++++++++++---------- venia-integration-tests/run-tests.js | 14 +++-- 2 files changed, 46 insertions(+), 37 deletions(-) diff --git a/venia-integration-tests/report-results.js b/venia-integration-tests/report-results.js index 66c579514e..1f80a02b37 100755 --- a/venia-integration-tests/report-results.js +++ b/venia-integration-tests/report-results.js @@ -3,68 +3,73 @@ let summary; const processTests = (tests, testsFile) => { - const failedMessages = tests.filter(test => test.fail).map(test => `${testsFile} \n\n${test.title}: \n\n${test.err.estack}`); + const failedMessages = tests + .filter(test => test.fail) + .map(test => `${testsFile} \n\n${test.title}: \n\n${test.err.estack}`); return failedMessages.join('\n'); -} +}; try { summary = require('./cypress-test-results.json'); } catch (e) { - throw new Error('Unable to retrieve Cypress results. Please re-run the tests.') + throw new Error( + 'Unable to retrieve Cypress results. Please re-run the tests.' + ); } const testFailures = summary.stats.failures; -const hasFailed = (result) => { +const hasFailed = result => { return result.suites.some(suite => suite.failures.length > 0); -} +}; -const getTestStatuses = (result) => { - let passedTests = 0 - let failedTests = 0 +const getTestStatuses = result => { + let passedTests = 0; + let failedTests = 0; let totalDuration = 0; try { if (result.tests.length) { - passedTests = passedTests + result.passes.length - failedTests = failedTests + result.failures.length - totalDuration = totalDuration + result.duration + passedTests = passedTests + result.passes.length; + failedTests = failedTests + result.failures.length; + totalDuration = totalDuration + result.duration; } if (result.suites.length) { result.suites.forEach(suite => { if (suite.tests.length) { - passedTests = passedTests + suite.passes.length - failedTests = failedTests + suite.failures.length - totalDuration = totalDuration + suite.duration + passedTests = passedTests + suite.passes.length; + failedTests = failedTests + suite.failures.length; + totalDuration = totalDuration + suite.duration; } if (suite.suites.length) { suite.suites.forEach(subSuite => { - const subSuiteResult = getTestStatuses(subSuite) + const subSuiteResult = getTestStatuses(subSuite); - passedTests = passedTests + subSuiteResult.passedTests - failedTests = failedTests + subSuiteResult.failedTests - totalDuration = totalDuration + subSuiteResult.totalDuration - }) + passedTests = passedTests + subSuiteResult.passedTests; + failedTests = failedTests + subSuiteResult.failedTests; + totalDuration = + totalDuration + subSuiteResult.totalDuration; + }); } - }) + }); } } catch (e) { - console.error(e) + console.error(e); } return { passedTests, failedTests, totalDuration - } -} + }; +}; -const generateTestSummary = (result) => { - const testFile = result.fullFile - const { passedTests, failedTests, totalDuration } = getTestStatuses(result) +const generateTestSummary = result => { + const testFile = result.fullFile; + const { passedTests, failedTests, totalDuration } = getTestStatuses(result); return { Spec: testFile, @@ -72,8 +77,8 @@ const generateTestSummary = (result) => { Passed: passedTests, Failed: failedTests, Duration: totalDuration - } -} + }; +}; const parsedSummary = summary.results.map(generateTestSummary); @@ -104,11 +109,13 @@ if (testFailures === 0) { .map(suite => processTests(suite.tests, result.fullFile)) ); } - }) + }); - const failSummary = failures.join('\n\n -------------------------------- \n\n') + const failSummary = failures.join( + '\n\n -------------------------------- \n\n' + ); console.log('Cypress tests in the following files did not pass 😔 \n\n'); console.log('\x1b[43m', `${failures.length} failures`, '\x1b[0m', '\n\n'); console.log('\x1b[31m', `${failSummary} \n\n`, '\x1b[0m'); -} \ No newline at end of file +} diff --git a/venia-integration-tests/run-tests.js b/venia-integration-tests/run-tests.js index ed3b58bead..c6a39757c9 100755 --- a/venia-integration-tests/run-tests.js +++ b/venia-integration-tests/run-tests.js @@ -68,14 +68,16 @@ if (port) { // run docker on local instance console.log(`Running tests on local instance ${baseUrl}`); - dockerCommand = `docker run --rm -v ${process.env.PWD - }:/venia-integration-tests -w /venia-integration-tests --entrypoint=cypress cypress/included:8.3.1 run --browser chrome --config baseUrl=https://host.docker.internal:${port},screenshotOnRunFailure=false --config-file cypress.config.json --env updateSnapshots=${update} --headless --reporter mochawesome --reporter-options reportDir=cypress/results,overwrite=false,html=false,json=true`; + dockerCommand = `docker run --rm -v ${ + process.env.PWD + }:/venia-integration-tests -w /venia-integration-tests --entrypoint=cypress cypress/included:8.3.1 run --browser chrome --config baseUrl=https://host.docker.internal:${port},screenshotOnRunFailure=false --config-file cypress.config.json --env updateSnapshots=${update} --headless --reporter mochawesome --reporter-options reportDir=cypress/results,overwrite=false,html=false,json=true`; } else { // run docker on remote instance console.log(`Running tests on remote instance ${baseUrl}`); - dockerCommand = `docker run --rm -v ${process.env.PWD - }:/venia-integration-tests -w /venia-integration-tests --entrypoint=cypress cypress/included:8.3.1 run --browser chrome --config baseUrl=${baseUrl},screenshotOnRunFailure=false --config-file cypress.config.json --env updateSnapshots=${update} --headless --reporter mochawesome --reporter-options reportDir=cypress/results,overwrite=false,html=false,json=true`; + dockerCommand = `docker run --rm -v ${ + process.env.PWD + }:/venia-integration-tests -w /venia-integration-tests --entrypoint=cypress cypress/included:8.3.1 run --browser chrome --config baseUrl=${baseUrl},screenshotOnRunFailure=false --config-file cypress.config.json --env updateSnapshots=${update} --headless --reporter mochawesome --reporter-options reportDir=cypress/results,overwrite=false,html=false,json=true`; } const start = process.hrtime(); @@ -112,7 +114,7 @@ for (let i = 0; i < threads; i++) { console.log( `docker run ${i + - 1} exited with ${code} code in ${timeTaken} seconds` + 1} exited with ${code} code in ${timeTaken} seconds` ); if (Object.values(dockerRuns).every(r => r.completed)) { @@ -136,7 +138,7 @@ for (let i = 0; i < threads; i++) { }; } -process.on('SIGINT', function () { +process.on('SIGINT', function() { console.log('Received kill signal. Killing all cypress tests. \n'); exec( From 98e5342f551a55c9571ece327503b06d515ee01c Mon Sep 17 00:00:00 2001 From: Revanth Kumar Date: Fri, 10 Dec 2021 15:05:30 -0600 Subject: [PATCH 6/7] Linter fix --- venia-integration-tests/package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/venia-integration-tests/package.json b/venia-integration-tests/package.json index 5c1a79dd21..dda770cbbd 100644 --- a/venia-integration-tests/package.json +++ b/venia-integration-tests/package.json @@ -5,13 +5,13 @@ "scripts": { "clearCache:mac": "rm -rf ~/Library/Application\\ Support/Cypress/cy/*", "danger": "danger-ci", + "posttest:headless": "./report-results.js", "test": "cypress open --browser chrome --config-file cypress.config.json", "test:ci": "./run-tests.js -t 4", "test:debug": "node --inspect-brk ./node_modules/cypress/bin/cypress run --browser chrome --config-file cypress.config.json", "test:devTest": "jest", "test:firefox": "cypress open --browser firefox --config-file cypress.config.json", "test:headless": "./run-tests.js", - "posttest:headless": "./report-results.js", "test:run": "cypress run --browser chrome --config-file cypress.config.json" }, "author": "Magento Commerce", From 0a6a9ceeadc2e1d7f1585edafde839982486c677 Mon Sep 17 00:00:00 2001 From: "hwyu@adobe.com" Date: Mon, 10 Jan 2022 17:36:04 -0600 Subject: [PATCH 7/7] PWA-2453: Add cypress status reporting while running headless - Updated logic for threadcount greater than test. - Modified script to process exit instead. - Updated display to show summary at bottom. --- venia-integration-tests/report-results.js | 26 +++++++++++------------ venia-integration-tests/run-tests.js | 13 ++++-------- 2 files changed, 17 insertions(+), 22 deletions(-) diff --git a/venia-integration-tests/report-results.js b/venia-integration-tests/report-results.js index 1f80a02b37..7d5851427b 100755 --- a/venia-integration-tests/report-results.js +++ b/venia-integration-tests/report-results.js @@ -2,6 +2,15 @@ let summary; +try { + summary = require('./cypress-test-results.json'); +} catch (e) { + console.error( + 'Unable to retrieve Cypress results. Please re-run the tests.' + ); + process.exit(1); +} + const processTests = (tests, testsFile) => { const failedMessages = tests .filter(test => test.fail) @@ -10,14 +19,6 @@ const processTests = (tests, testsFile) => { return failedMessages.join('\n'); }; -try { - summary = require('./cypress-test-results.json'); -} catch (e) { - throw new Error( - 'Unable to retrieve Cypress results. Please re-run the tests.' - ); -} - const testFailures = summary.stats.failures; const hasFailed = result => { @@ -82,11 +83,10 @@ const generateTestSummary = result => { const parsedSummary = summary.results.map(generateTestSummary); -console.table(parsedSummary); -console.log('\n'); - if (testFailures === 0) { // no test failures + console.table(parsedSummary); + console.log('\n'); console.log('\x1b[32m', 'All Cypress tests passed 👍', '\x1b[0m', '\n\n'); return; @@ -114,8 +114,8 @@ if (testFailures === 0) { const failSummary = failures.join( '\n\n -------------------------------- \n\n' ); - - console.log('Cypress tests in the following files did not pass 😔 \n\n'); console.log('\x1b[43m', `${failures.length} failures`, '\x1b[0m', '\n\n'); console.log('\x1b[31m', `${failSummary} \n\n`, '\x1b[0m'); + console.log('Cypress tests in the following files did not pass 😔 \n'); + console.table(parsedSummary); } diff --git a/venia-integration-tests/run-tests.js b/venia-integration-tests/run-tests.js index c6a39757c9..92a263d358 100755 --- a/venia-integration-tests/run-tests.js +++ b/venia-integration-tests/run-tests.js @@ -46,18 +46,13 @@ if (!baseUrl) { 'Missing baseUrl. Please provide a baseUrl using the --baseUrl arg' ); - return; + process.exit(1); } const files = spec ? spec.split(',') : glob.sync('./src/tests/**/*.spec.js'); -if (files.length < threads) { - console.error('Can not have more parallel runs than tests.'); - - return; -} - -const testsPerRun = files.length / threads; +const threadCount = Math.min(files.length, threads); +const testsPerRun = files.length / threadCount; const dockerRuns = {}; const port = new URL(baseUrl).port; @@ -86,7 +81,7 @@ const start = process.hrtime(); rmSync('cypress/results', { recursive: true, force: true }); rmSync('cypress-test-results.json', { force: true }); -for (let i = 0; i < threads; i++) { +for (let i = 0; i < threadCount; i++) { const filesToTest = files.slice(testsPerRun * i, testsPerRun * (i + 1)); const commandWithSpecFiles = `${dockerCommand} --spec ${filesToTest.join(