Skip to content

Save coverage for each page in a test #70

New issue

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

By clicking “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? Sign in to your account

Merged
merged 2 commits into from
Jan 27, 2020
Merged

Save coverage for each page in a test #70

merged 2 commits into from
Jan 27, 2020

Conversation

grantbdev
Copy link
Contributor

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.

The implementation in the PR keeps the behavior of saving coverage after each test since that is the current behavior and I believe it would be necessary for #4. However, I think it would also be possible to initialize the array and event listener in before() and only save coverage in after() if that is desired instead.

Closes #69

@CLAassistant
Copy link

CLAassistant commented Sep 1, 2019

CLA assistant check
All committers have signed the CLA.

@bahmutov
Copy link
Contributor

Hmm, @grantbdev I love it. I just need an example site showing this in action.

@grantbdev
Copy link
Contributor Author

@bahmutov I can do that. Do you have any preferences on what I base it on or is any minimum repo that has different JS (be it jQuery, React, etc.) loaded/executed across windows sufficient?

swhmirror pushed a commit to SoftwareHeritage/swh-web that referenced this pull request Sep 27, 2019
@grantbdev
Copy link
Contributor Author

@bahmutov clarification on what you need for an example site would be appreciated 😄

@matinfo
Copy link

matinfo commented Dec 6, 2019

very interesting, let go!

@bahmutov
Copy link
Contributor

bahmutov commented Dec 9, 2019

@grantbdev as an example I would love to see a simple site with 2 pages, each loading its own code. The test could open the first page, then click on a link to go to the second page. The collected coverage object should show coverage for the code loaded on the first page and the code loaded on the second page

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`.
@grantbdev
Copy link
Contributor Author

@bahmutov PR rebased and updated with testing sample code in this package's test suite if that is OK with you.

@3590Duncan
Copy link

3590Duncan commented Jan 20, 2020

Any updates on when this could potentially be released?

Copy link
Contributor

@bahmutov bahmutov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, I will merge this, test locally and will release it.

@bahmutov bahmutov merged commit f933e9d into cypress-io:master Jan 27, 2020
@grantbdev grantbdev deleted the grantbdev/feature--multiple-windows branch January 27, 2020 21:49
@bahmutov
Copy link
Contributor

🎉 This PR is included in version 1.11.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

@windmaomao
Copy link

I'm not sure this is the right place, but i found out the bug might be related to the following lines for beforeEach.

    // 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({ log: false }).then(saveCoverageObject)

If i have a test file with multiple tests

describe('Button component', () => {
  it('should display label', () => {
    mount(<Button />);
  });
  it('should ..', () => {
    mount(<Button />)
  })

What happens is that window.__coverage__ does not get reset when going from the first it to the second it. So when combining them, it gets a weird behavior, for example, all the counts in the second it would carry all the counts in the first it already.

@grantbdev
Copy link
Contributor Author

@windmaomao It's been a long time since I worked on this, but I believe the intended behavior is to give the total counts/coverage of all the tests run at once. I think that is also consistent with coverage tracking in other testing libraries where it typically shows the totals for a testing session. If you want to see the coverage from a specific test you would have to run it in isolation.

@windmaomao
Copy link

windmaomao commented May 31, 2022

thanks @grantbdev , i appreciate your time. Actually i'm asking the opposite. The window.__coverage__ does cover the overall counts. For example, if i go from test 1 to test 2 within the same unit, it'll show me the the combination of test 1 and 2. However the problem is the save part, it occurs to me (I want to confirm when i get time), the results are "merged" at both test 1 and 2, therefore making test1 results double counted. Hopefully i'm making sense here.

I think i know exactly what i want to get. I want the test 2 results gets merged at the end of this entire unit, but not after test 1 is finished.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Saving coverage before page changes in test
6 participants