Skip to content

Commit 42b19de

Browse files
committed
keeping code coverage per individual test
1 parent 8fb2c91 commit 42b19de

File tree

6 files changed

+46
-2
lines changed

6 files changed

+46
-2
lines changed

cypress/app.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,7 @@ if (true) {
77
} else {
88
console.error('never reached')
99
}
10+
11+
document.getElementById('click').addEventListener('click', () => {
12+
console.log('clicked')
13+
})

cypress/index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<body>
22
<h2>Test page</h2>
33
<p>Open the DevTools to see console messages</p>
4+
<button id="click">click me</button>
45
<script src="app.js"></script>
56
</body>

cypress/integration/spec.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,9 @@ context('Page test', () => {
1616
.should('have.been.calledOnce')
1717
.should('have.been.calledWith', 'just names', ['joe', 'mary'])
1818
})
19+
20+
it('clicks on the button', function () {
21+
cy.get('#click').click()
22+
cy.get('@log').should('have.been.calledWith', 'clicked')
23+
})
1924
})

package-lock.json

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,5 +45,8 @@
4545
"parcel-bundler": "1.12.3",
4646
"start-server-and-test": "1.9.0",
4747
"semantic-release": "15.13.12"
48+
},
49+
"dependencies": {
50+
"its-name": "1.0.0"
4851
}
4952
}

support.js

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,38 @@
11
/// <reference types="cypress" />
2+
const itsName = require('its-name')
3+
4+
let codeCoveragePerTest = new Map()
5+
26
before(() => {
37
// we need to reset the coverage when running
48
// in the interactive mode, otherwise the counters will
59
// keep increasing every time we rerun the tests
6-
cy.task('resetCoverage', { isInteractive: Cypress.config('isInteractive') })
10+
const isInteractive = Cypress.config('isInteractive')
11+
12+
cy.task('resetCoverage', { isInteractive })
13+
14+
if (isInteractive) {
15+
codeCoveragePerTest = new Map()
16+
}
717
})
818

9-
afterEach(() => {
19+
// use "function () {...}" callback to get "this" pointing
20+
// at the right context
21+
afterEach(function () {
1022
// save coverage after each test
1123
// because the entire "window" object is about
1224
// to be recycled by Cypress before next test
1325
cy.window().then(win => {
1426
if (win.__coverage__) {
27+
// only keep track of code coverage per test in the interactive mode
28+
if (Cypress.config('isInteractive')) {
29+
// testName will be an array of strings
30+
const testName = itsName(this.currentTest)
31+
console.log('test that finished', testName)
32+
33+
codeCoveragePerTest[testName] = win.__coverage__
34+
}
35+
1536
cy.task('combineCoverage', win.__coverage__)
1637
}
1738
})
@@ -20,4 +41,9 @@ afterEach(() => {
2041
after(() => {
2142
// when all tests finish, lets generate the coverage report
2243
cy.task('coverageReport')
44+
45+
console.log(
46+
'after all tests finished, coverage per test is',
47+
codeCoveragePerTest
48+
)
2349
})

0 commit comments

Comments
 (0)