diff --git a/README.md b/README.md index 28896aa9..1b845848 100644 --- a/README.md +++ b/README.md @@ -207,6 +207,29 @@ if (global.__coverage__) { That should be enough - the code coverage from the server will be requested at the end of the test run and merged with the client-side code coverage, producing a combined report. +### expectBackendCoverageOnly + +If there is NO frontend code coverage, and you want to only collect the backend code coverage using Cypress tests, set `expectBackendCoverageOnly: true` in `cypress.json` file. Otherwise Cypress complains that it cannot find the frontend code coverage. + +Default: + +![No frontend code coverage warning](./images/warning.png) + +After: + +```json +{ + "env": { + "codeCoverage": { + "url": "http://localhost:3003/__coverage__", + "expectBackendCoverageOnly": true + } + } +} +``` + +![Cypress knows to expect the backend code coverage only](./images/expect-backend.png) + ## Custom report folder You can specify custom report folder by adding `nyc` object to the `package.json` file. For example to save reports to `cypress-coverage` folder, use: diff --git a/examples/backend/cypress.json b/examples/backend/cypress.json index 3fdedda2..9930e7a1 100644 --- a/examples/backend/cypress.json +++ b/examples/backend/cypress.json @@ -3,7 +3,8 @@ "baseUrl": "http://localhost:3003", "env": { "codeCoverage": { - "url": "http://localhost:3003/__coverage__" + "url": "http://localhost:3003/__coverage__", + "expectBackendCoverageOnly": true } } } diff --git a/examples/backend/package.json b/examples/backend/package.json index c39c662f..fc6d9dc0 100644 --- a/examples/backend/package.json +++ b/examples/backend/package.json @@ -2,6 +2,7 @@ "name": "example-backend", "description": "Code coverage for backend", "devDependencies": {}, + "private": true, "scripts": { "start": "../../node_modules/.bin/nyc --silent node server/server", "cy:open": "../../node_modules/.bin/cypress open", diff --git a/images/expect-backend.png b/images/expect-backend.png new file mode 100644 index 00000000..e1f6d5dd Binary files /dev/null and b/images/expect-backend.png differ diff --git a/images/warning.png b/images/warning.png new file mode 100644 index 00000000..8f0cdc9d Binary files /dev/null and b/images/warning.png differ diff --git a/support.js b/support.js index 131303c6..5836bea3 100644 --- a/support.js +++ b/support.js @@ -134,12 +134,19 @@ const registerHooks = () => { 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) - `) + const expectBackendCoverageOnly = Cypress._.get( + Cypress.env('codeCoverage'), + 'expectBackendCoverageOnly', + false + ) + if (!expectBackendCoverageOnly) { + 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) + `) + } } } }) @@ -179,7 +186,19 @@ const registerHooks = () => { if (!coverage) { // we did not get code coverage - this is the // original failed request - return + const expectBackendCoverageOnly = Cypress._.get( + Cypress.env('codeCoverage'), + 'expectBackendCoverageOnly', + false + ) + if (expectBackendCoverageOnly) { + throw new Error( + `Expected to collect backend code coverage from ${url}` + ) + } else { + // we did not really expect to collect the backend code coverage + return + } } sendCoverage(coverage, 'backend') })