Skip to content

feat: warn if missing code coverage information #128

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 11, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 31 additions & 1 deletion support.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down