From 623fe4ce96a4f276f8f0ae01cdfdaf58866c3b61 Mon Sep 17 00:00:00 2001 From: Gleb Bahmutov Date: Tue, 4 Feb 2020 10:41:04 -0500 Subject: [PATCH 1/2] feat: warn if missing code coverage information --- support.js | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/support.js b/support.js index 70ad012ec..4d9f37bf8 100644 --- a/support.js +++ b/support.js @@ -5,7 +5,7 @@ * via "cy.task". */ const sendCoverage = (coverage, pathname = '/') => { - cy.log(`Saving code coverage **${pathname}**`) + cy.log(`Saving code coverage for **${pathname}**`) // stringify coverage object for speed cy.task('combineCoverage', JSON.stringify(coverage), { log: false @@ -21,6 +21,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 +57,26 @@ if (Cypress.env('coverage') === false) { windowCoverageObjects.forEach(cover => { sendCoverage(cover.coverage, cover.pathname) }) + + if (!hasE2ECoverage()) { + if (hasUnitTestCoverage()) { + cy.log(`👉 Only found unit test code coverage.`) + } else { + cy.log(` + ⚠️ 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 From 1363a2759ce1dbc48529f0645659c2e32145e846 Mon Sep 17 00:00:00 2001 From: Gleb Bahmutov Date: Tue, 4 Feb 2020 14:13:13 -0500 Subject: [PATCH 2/2] add note to plugin's log messages --- support.js | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/support.js b/support.js index 4d9f37bf8..a7baddc13 100644 --- a/support.js +++ b/support.js @@ -5,13 +5,22 @@ * via "cy.task". */ const sendCoverage = (coverage, pathname = '/') => { - cy.log(`Saving code coverage for **${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 @@ -60,9 +69,9 @@ if (Cypress.env('coverage') === false) { if (!hasE2ECoverage()) { if (hasUnitTestCoverage()) { - cy.log(`👉 Only found unit test code coverage.`) + logMessage(`👉 Only found unit test code coverage.`) } else { - cy.log(` + logMessage(` ⚠️ Could not find any coverage information in your application by looking at the window coverage object. Did you forget to instrument your application?