Skip to content

Commit 68b0907

Browse files
authored
feat: handle cy.visit inside before callback (#152)
* feat: handle before callback * update readme * remove debug console.log calls
1 parent fd01a7c commit 68b0907

File tree

3 files changed

+31
-7
lines changed

3 files changed

+31
-7
lines changed

Diff for: README.md

+7
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,13 @@ npm run dev:no:coverage
322322

323323
## Examples
324324

325+
### Internal examples
326+
327+
- [examples/before-each-visit](examples/before-each-visit) checks if code coverage correctly keeps track of code when doing `cy.visit` before each test
328+
- [examples-before-all-visit](examples/before-all-visit) checks if code coverage works when `cy.visit` is made once in the `before` hook
329+
330+
### External examples
331+
325332
- [cypress-io/cypress-example-todomvc-redux](https://github.com/cypress-io/cypress-example-todomvc-redux) is a React / Redux application with 100% code coverage.
326333
- [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.
327334
- [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.

Diff for: examples/before-all-visit/cypress/integration/spec.js

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/// <reference types="cypress" />
22
describe('coverage information', () => {
33
before(() => {
4+
cy.log('visiting index.html')
45
cy.visit('index.html')
56
})
67

Diff for: support.js

+23-7
Original file line numberDiff line numberDiff line change
@@ -46,18 +46,34 @@ if (Cypress.env('coverage') === false) {
4646
// to let the user know the coverage has been collected
4747
windowCoverageObjects = []
4848

49-
// save reference to coverage for each app window loaded in the test
50-
cy.on('window:load', win => {
49+
const saveCoverageObject = win => {
5150
// if application code has been instrumented, the app iframe "window" has an object
5251
const applicationSourceCoverage = win.__coverage__
52+
if (!applicationSourceCoverage) {
53+
return
54+
}
5355

54-
if (applicationSourceCoverage) {
55-
windowCoverageObjects.push({
56-
coverage: applicationSourceCoverage,
57-
pathname: win.location.pathname
56+
if (
57+
Cypress._.find(windowCoverageObjects, {
58+
coverage: applicationSourceCoverage
5859
})
60+
) {
61+
// this application code coverage object is already known
62+
// which can happen when combining `window:load` and `before` callbacks
63+
return
5964
}
60-
})
65+
66+
windowCoverageObjects.push({
67+
coverage: applicationSourceCoverage,
68+
pathname: win.location.pathname
69+
})
70+
}
71+
72+
// save reference to coverage for each app window loaded in the test
73+
cy.on('window:load', saveCoverageObject)
74+
75+
// save reference if visiting a page inside a before() hook
76+
cy.window().then(saveCoverageObject)
6177
})
6278

6379
afterEach(() => {

0 commit comments

Comments
 (0)