diff --git a/support.js b/support.js index 70ad012e..a7baddc1 100644 --- a/support.js +++ b/support.js @@ -5,13 +5,22 @@ * via "cy.task". */ const sendCoverage = (coverage, pathname = '/') => { - cy.log(`Saving code coverage **${pathname}**`) + logMessage(`Saving code coverage for **${pathname}**`) // stringify coverage object for speed cy.task('combineCoverage', JSON.stringify(coverage), { log: false }) } +/** + * Consistently logs the given string to the Command Log + * so the user knows the log message is coming from this plugin. + * @param {string} s Message to log. + */ +const logMessage = s => { + cy.log(`${s} \`[@cypress/code-coverage]\``) +} + // to disable code coverage commands and save time // pass environment variable coverage=false // cypress run --env coverage=false @@ -21,6 +30,10 @@ if (Cypress.env('coverage') === false) { } else { let windowCoverageObjects + const hasE2ECoverage = () => Boolean(windowCoverageObjects.length) + + const hasUnitTestCoverage = () => Boolean(window.__coverage__) + before(() => { // we need to reset the coverage when running // in the interactive mode, otherwise the counters will @@ -53,9 +66,26 @@ if (Cypress.env('coverage') === false) { windowCoverageObjects.forEach(cover => { sendCoverage(cover.coverage, cover.pathname) }) + + if (!hasE2ECoverage()) { + if (hasUnitTestCoverage()) { + logMessage(`👉 Only found unit test code coverage.`) + } else { + logMessage(` + ⚠️ Could not find any coverage information in your application + by looking at the window coverage object. + Did you forget to instrument your application? + See [code-coverage#instrument-your-application](https://github.com/cypress-io/code-coverage#instrument-your-application) + `) + } + } }) after(() => { + // I wish I could fail the tests if there is no code coverage information + // but throwing an error here does not fail the test run due to + // https://github.com/cypress-io/cypress/issues/2296 + // there might be server-side code coverage information // we should grab it once after all tests finish const baseUrl = Cypress.config('baseUrl') || cy.state('window').origin