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
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
5 changes: 5 additions & 0 deletions cypress/about.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<body>
<h2>About</h2>
<main id="content"></main>
<script src="about.js"></script>
</body>
3 changes: 3 additions & 0 deletions cypress/about.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
document
.getElementById('content')
.appendChild(document.createTextNode('Est. 2019'))
3 changes: 3 additions & 0 deletions cypress/index.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
<body>
<nav>
<a href="/about.html">About</a>
</nav>
<h2>Test page</h2>
<p>Open the DevTools to see console messages</p>
<script src="app.js"></script>
Expand Down
2 changes: 2 additions & 0 deletions cypress/integration/spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ context('Page test', () => {
cy.spy(win.console, 'log').as('log')
}
})

cy.contains('About').click()
})

it('logs names', function () {
Expand Down
22 changes: 16 additions & 6 deletions support.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,27 +19,37 @@ 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
// keep increasing every time we rerun the tests
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
Expand Down