5
5
* via "cy.task".
6
6
*/
7
7
const sendCoverage = ( coverage , pathname = '/' ) => {
8
- cy . log ( `Saving code coverage **${ pathname } **` )
8
+ logMessage ( `Saving code coverage for **${ pathname } **` )
9
9
// stringify coverage object for speed
10
10
cy . task ( 'combineCoverage' , JSON . stringify ( coverage ) , {
11
11
log : false
12
12
} )
13
13
}
14
14
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
+
15
24
// to disable code coverage commands and save time
16
25
// pass environment variable coverage=false
17
26
// cypress run --env coverage=false
@@ -21,6 +30,10 @@ if (Cypress.env('coverage') === false) {
21
30
} else {
22
31
let windowCoverageObjects
23
32
33
+ const hasE2ECoverage = ( ) => Boolean ( windowCoverageObjects . length )
34
+
35
+ const hasUnitTestCoverage = ( ) => Boolean ( window . __coverage__ )
36
+
24
37
before ( ( ) => {
25
38
// we need to reset the coverage when running
26
39
// in the interactive mode, otherwise the counters will
@@ -53,9 +66,26 @@ if (Cypress.env('coverage') === false) {
53
66
windowCoverageObjects . forEach ( cover => {
54
67
sendCoverage ( cover . coverage , cover . pathname )
55
68
} )
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
+ }
56
82
} )
57
83
58
84
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
+
59
89
// there might be server-side code coverage information
60
90
// we should grab it once after all tests finish
61
91
const baseUrl = Cypress . config ( 'baseUrl' ) || cy . state ( 'window' ) . origin
0 commit comments