Skip to content

Commit 573bc6a

Browse files
authored
feat: warn if missing code coverage information (#128)
* feat: warn if missing code coverage information * add note to plugin's log messages
1 parent 80a5f1a commit 573bc6a

File tree

1 file changed

+31
-1
lines changed

1 file changed

+31
-1
lines changed

support.js

+31-1
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,22 @@
55
* via "cy.task".
66
*/
77
const sendCoverage = (coverage, pathname = '/') => {
8-
cy.log(`Saving code coverage **${pathname}**`)
8+
logMessage(`Saving code coverage for **${pathname}**`)
99
// stringify coverage object for speed
1010
cy.task('combineCoverage', JSON.stringify(coverage), {
1111
log: false
1212
})
1313
}
1414

15+
/**
16+
* Consistently logs the given string to the Command Log
17+
* so the user knows the log message is coming from this plugin.
18+
* @param {string} s Message to log.
19+
*/
20+
const logMessage = s => {
21+
cy.log(`${s} \`[@cypress/code-coverage]\``)
22+
}
23+
1524
// to disable code coverage commands and save time
1625
// pass environment variable coverage=false
1726
// cypress run --env coverage=false
@@ -21,6 +30,10 @@ if (Cypress.env('coverage') === false) {
2130
} else {
2231
let windowCoverageObjects
2332

33+
const hasE2ECoverage = () => Boolean(windowCoverageObjects.length)
34+
35+
const hasUnitTestCoverage = () => Boolean(window.__coverage__)
36+
2437
before(() => {
2538
// we need to reset the coverage when running
2639
// in the interactive mode, otherwise the counters will
@@ -53,9 +66,26 @@ if (Cypress.env('coverage') === false) {
5366
windowCoverageObjects.forEach(cover => {
5467
sendCoverage(cover.coverage, cover.pathname)
5568
})
69+
70+
if (!hasE2ECoverage()) {
71+
if (hasUnitTestCoverage()) {
72+
logMessage(`👉 Only found unit test code coverage.`)
73+
} else {
74+
logMessage(`
75+
⚠️ Could not find any coverage information in your application
76+
by looking at the window coverage object.
77+
Did you forget to instrument your application?
78+
See [code-coverage#instrument-your-application](https://github.com/cypress-io/code-coverage#instrument-your-application)
79+
`)
80+
}
81+
}
5682
})
5783

5884
after(() => {
85+
// I wish I could fail the tests if there is no code coverage information
86+
// but throwing an error here does not fail the test run due to
87+
// https://github.com/cypress-io/cypress/issues/2296
88+
5989
// there might be server-side code coverage information
6090
// we should grab it once after all tests finish
6191
const baseUrl = Cypress.config('baseUrl') || cy.state('window').origin

0 commit comments

Comments
 (0)