From 1e361216d12eddcfce1eb9a9511aabf5a3a10412 Mon Sep 17 00:00:00 2001 From: Grant Bourque Date: Sun, 1 Sep 2019 14:04:37 -0500 Subject: [PATCH] Save coverage for each page in a test Save coverage for each page visited in a test by keeping a reference to the coverage for each app window loaded and then saving the coverage at the end after coverage is calculated. An additional page is added to the example test app and the page test navigates to the new page. Without this coverage change, coverage is lost for `app.js` due to the last page not loading it. With this coverage change, coverage is kept for `app.js` along with coverage for `about.js`. --- cypress/about.html | 5 +++++ cypress/about.js | 3 +++ cypress/index.html | 3 +++ cypress/integration/spec.js | 2 ++ support.js | 22 ++++++++++++++++------ 5 files changed, 29 insertions(+), 6 deletions(-) create mode 100644 cypress/about.html create mode 100644 cypress/about.js diff --git a/cypress/about.html b/cypress/about.html new file mode 100644 index 00000000..7316e847 --- /dev/null +++ b/cypress/about.html @@ -0,0 +1,5 @@ + +

About

+
+ + diff --git a/cypress/about.js b/cypress/about.js new file mode 100644 index 00000000..daff6cf4 --- /dev/null +++ b/cypress/about.js @@ -0,0 +1,3 @@ +document + .getElementById('content') + .appendChild(document.createTextNode('Est. 2019')) diff --git a/cypress/index.html b/cypress/index.html index faf8fb3b..c8eee001 100644 --- a/cypress/index.html +++ b/cypress/index.html @@ -1,4 +1,7 @@ +

Test page

Open the DevTools to see console messages

diff --git a/cypress/integration/spec.js b/cypress/integration/spec.js index 4aa19ae0..c353c07d 100644 --- a/cypress/integration/spec.js +++ b/cypress/integration/spec.js @@ -12,6 +12,8 @@ context('Page test', () => { cy.spy(win.console, 'log').as('log') } }) + + cy.contains('About').click() }) it('logs names', function () { diff --git a/support.js b/support.js index c1a6e1e0..04c2e650 100644 --- a/support.js +++ b/support.js @@ -19,6 +19,8 @@ const sendCoverage = coverage => { if (Cypress.env('coverage') === false) { console.log('Skipping code coverage hooks') } else { + let windowCoverageObjects + before(() => { // we need to reset the coverage when running // in the interactive mode, otherwise the counters will @@ -26,20 +28,28 @@ if (Cypress.env('coverage') === false) { cy.task('resetCoverage', { isInteractive: Cypress.config('isInteractive') }) }) - afterEach(() => { - // save coverage after each test - // because the entire "window" object is about - // to be recycled by Cypress before next test - cy.window().then(win => { + beforeEach(() => { + windowCoverageObjects = [] + + // save reference to coverage for each app window loaded in the test + cy.on('window:load', win => { // if application code has been instrumented, the app iframe "window" has an object const applicationSourceCoverage = win.__coverage__ if (applicationSourceCoverage) { - sendCoverage(applicationSourceCoverage) + windowCoverageObjects.push(applicationSourceCoverage) } }) }) + afterEach(() => { + // save coverage after the test + // because now the window coverage objects have been updated + windowCoverageObjects.forEach(coverage => { + sendCoverage(coverage) + }) + }) + after(() => { // there might be server-side code coverage information // we should grab it once after all tests finish