From 8271e4ff97e4f368ca795c4146b5fadbe1e61617 Mon Sep 17 00:00:00 2001 From: Gleb Bahmutov Date: Mon, 9 Mar 2020 16:36:39 -0400 Subject: [PATCH 1/3] feat: handle before callback --- .../cypress/integration/spec.js | 1 + support.js | 36 +++++++++++++++---- 2 files changed, 30 insertions(+), 7 deletions(-) diff --git a/examples/before-all-visit/cypress/integration/spec.js b/examples/before-all-visit/cypress/integration/spec.js index 45f075e0b..d1c70b981 100644 --- a/examples/before-all-visit/cypress/integration/spec.js +++ b/examples/before-all-visit/cypress/integration/spec.js @@ -1,6 +1,7 @@ /// describe('coverage information', () => { before(() => { + cy.log('visiting index.html') cy.visit('index.html') }) diff --git a/support.js b/support.js index 03b45a030..d5b7f0701 100644 --- a/support.js +++ b/support.js @@ -46,18 +46,40 @@ if (Cypress.env('coverage') === false) { // to let the user know the coverage has been collected windowCoverageObjects = [] - // save reference to coverage for each app window loaded in the test - cy.on('window:load', win => { + const saveCoverageObject = win => { // if application code has been instrumented, the app iframe "window" has an object const applicationSourceCoverage = win.__coverage__ + console.log('app code coverage object', applicationSourceCoverage) + console.log( + 'existing %d', + windowCoverageObjects.length, + windowCoverageObjects + ) + if (!applicationSourceCoverage) { + return + } - if (applicationSourceCoverage) { - windowCoverageObjects.push({ - coverage: applicationSourceCoverage, - pathname: win.location.pathname + if ( + Cypress._.find(windowCoverageObjects, { + coverage: applicationSourceCoverage }) + ) { + // this application code coverage object is already known + // which can happen when combining `window:load` and `before` callbacks + return } - }) + + windowCoverageObjects.push({ + coverage: applicationSourceCoverage, + pathname: win.location.pathname + }) + } + + // save reference to coverage for each app window loaded in the test + cy.on('window:load', saveCoverageObject) + + // save reference if visiting a page inside a before() hook + cy.window().then(saveCoverageObject) }) afterEach(() => { From 8102351673e017cc64e422e2ae4cae0c10bd3e13 Mon Sep 17 00:00:00 2001 From: Gleb Bahmutov Date: Mon, 9 Mar 2020 16:44:45 -0400 Subject: [PATCH 2/3] update readme --- README.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/README.md b/README.md index ad31c9541..0c9094957 100644 --- a/README.md +++ b/README.md @@ -322,6 +322,13 @@ npm run dev:no:coverage ## Examples +### Internal examples + +- [examples/before-each-visit](examples/before-each-visit) checks if code coverage correctly keeps track of code when doing `cy.visit` before each test +- [examples-before-all-visit](examples/before-all-visit) checks if code coverage works when `cy.visit` is made once in the `before` hook + +### External examples + - [cypress-io/cypress-example-todomvc-redux](https://github.com/cypress-io/cypress-example-todomvc-redux) is a React / Redux application with 100% code coverage. - [cypress-io/cypress-example-realworld](https://github.com/cypress-io/cypress-example-realworld) shows how to collect the coverage information from both back and front end code and merge it into a single report. The E2E test step runs in parallel in several CI containers, each saving just partial test coverage information. Then a merge job runs taking artifacts and combining coverage into the final report to be sent to an exteral coverage as a service app. - [bahmutov/code-coverage-webpack-dev-server](https://github.com/bahmutov/code-coverage-webpack-dev-server) shows how to collect code coverage from an application that uses webpack-dev-server. From e40434463c8e6bf8e2b37665ba66836e11942003 Mon Sep 17 00:00:00 2001 From: Gleb Bahmutov Date: Mon, 9 Mar 2020 16:47:14 -0400 Subject: [PATCH 3/3] remove debug console.log calls --- support.js | 6 ------ 1 file changed, 6 deletions(-) diff --git a/support.js b/support.js index d5b7f0701..b815b5109 100644 --- a/support.js +++ b/support.js @@ -49,12 +49,6 @@ if (Cypress.env('coverage') === false) { const saveCoverageObject = win => { // if application code has been instrumented, the app iframe "window" has an object const applicationSourceCoverage = win.__coverage__ - console.log('app code coverage object', applicationSourceCoverage) - console.log( - 'existing %d', - windowCoverageObjects.length, - windowCoverageObjects - ) if (!applicationSourceCoverage) { return }