Skip to content

Commit 610d0b5

Browse files
authored
feat: add option to expect the backend code coverage only (#373)
`expectBackendCoverageOnly: true`
1 parent 35791eb commit 610d0b5

File tree

6 files changed

+52
-8
lines changed

6 files changed

+52
-8
lines changed

README.md

+23
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,29 @@ if (global.__coverage__) {
207207

208208
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.
209209

210+
### expectBackendCoverageOnly
211+
212+
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.
213+
214+
Default:
215+
216+
![No frontend code coverage warning](./images/warning.png)
217+
218+
After:
219+
220+
```json
221+
{
222+
"env": {
223+
"codeCoverage": {
224+
"url": "http://localhost:3003/__coverage__",
225+
"expectBackendCoverageOnly": true
226+
}
227+
}
228+
}
229+
```
230+
231+
![Cypress knows to expect the backend code coverage only](./images/expect-backend.png)
232+
210233
## Custom report folder
211234

212235
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:

examples/backend/cypress.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
"baseUrl": "http://localhost:3003",
44
"env": {
55
"codeCoverage": {
6-
"url": "http://localhost:3003/__coverage__"
6+
"url": "http://localhost:3003/__coverage__",
7+
"expectBackendCoverageOnly": true
78
}
89
}
910
}

examples/backend/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"name": "example-backend",
33
"description": "Code coverage for backend",
44
"devDependencies": {},
5+
"private": true,
56
"scripts": {
67
"start": "../../node_modules/.bin/nyc --silent node server/server",
78
"cy:open": "../../node_modules/.bin/cypress open",

images/expect-backend.png

141 KB
Loading

images/warning.png

200 KB
Loading

support.js

+26-7
Original file line numberDiff line numberDiff line change
@@ -134,12 +134,19 @@ const registerHooks = () => {
134134
if (hasUnitTestCoverage()) {
135135
logMessage(`👉 Only found unit test code coverage.`)
136136
} else {
137-
logMessage(`
138-
⚠️ Could not find any coverage information in your application
139-
by looking at the window coverage object.
140-
Did you forget to instrument your application?
141-
See [code-coverage#instrument-your-application](https://github.com/cypress-io/code-coverage#instrument-your-application)
142-
`)
137+
const expectBackendCoverageOnly = Cypress._.get(
138+
Cypress.env('codeCoverage'),
139+
'expectBackendCoverageOnly',
140+
false
141+
)
142+
if (!expectBackendCoverageOnly) {
143+
logMessage(`
144+
⚠️ Could not find any coverage information in your application
145+
by looking at the window coverage object.
146+
Did you forget to instrument your application?
147+
See [code-coverage#instrument-your-application](https://github.com/cypress-io/code-coverage#instrument-your-application)
148+
`)
149+
}
143150
}
144151
}
145152
})
@@ -179,7 +186,19 @@ const registerHooks = () => {
179186
if (!coverage) {
180187
// we did not get code coverage - this is the
181188
// original failed request
182-
return
189+
const expectBackendCoverageOnly = Cypress._.get(
190+
Cypress.env('codeCoverage'),
191+
'expectBackendCoverageOnly',
192+
false
193+
)
194+
if (expectBackendCoverageOnly) {
195+
throw new Error(
196+
`Expected to collect backend code coverage from ${url}`
197+
)
198+
} else {
199+
// we did not really expect to collect the backend code coverage
200+
return
201+
}
183202
}
184203
sendCoverage(coverage, 'backend')
185204
})

0 commit comments

Comments
 (0)