Skip to content

feat: add option to expect the backend code coverage only #373

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 1 commit into from
Jan 6, 2021
Merged
Show file tree
Hide file tree
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
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
3 changes: 2 additions & 1 deletion examples/backend/cypress.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
"baseUrl": "http://localhost:3003",
"env": {
"codeCoverage": {
"url": "http://localhost:3003/__coverage__"
"url": "http://localhost:3003/__coverage__",
"expectBackendCoverageOnly": true
}
}
}
1 change: 1 addition & 0 deletions examples/backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Binary file added images/expect-backend.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/warning.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
33 changes: 26 additions & 7 deletions support.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
`)
}
}
}
})
Expand Down Expand Up @@ -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')
})
Expand Down