Skip to content

feat: handle cy.visit inside before callback #152

New issue

Have a question about this project? No Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “No Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? No Sign in to your account

Merged
merged 3 commits into from
Mar 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
1 change: 1 addition & 0 deletions examples/before-all-visit/cypress/integration/spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/// <reference types="cypress" />
describe('coverage information', () => {
before(() => {
cy.log('visiting index.html')
cy.visit('index.html')
})

Expand Down
30 changes: 23 additions & 7 deletions support.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,18 +46,34 @@ 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__
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(() => {
Expand Down