Skip to content

Commit 69dc06f

Browse files
committedSep 16, 2024
feat: support multiple servers from which to gather code coverage
1 parent 5670f05 commit 69dc06f

File tree

2 files changed

+50
-33
lines changed

2 files changed

+50
-33
lines changed
 

‎README.md

+12
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,18 @@ if (global.__coverage__) {
214214
}
215215
```
216216

217+
Or if you have multiple servers from which you are wanting to gather code coverage, you can pass any array to `url as well:
218+
219+
```json
220+
{
221+
"env": {
222+
"codeCoverage": {
223+
"url": ["http://localhost:3000/__coverage__", "http://localhost:3001/__coverage__"]
224+
}
225+
}
226+
}
227+
```
228+
217229
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.
218230

219231
### expectBackendCoverageOnly

‎support.js

+38-33
Original file line numberDiff line numberDiff line change
@@ -66,13 +66,8 @@ const registerHooks = () => {
6666
windowCoverageObjects = []
6767

6868
const saveCoverageObject = (win) => {
69-
// if the application code has been instrumented, then the app iframe "win.__coverage__" will be available,
70-
// in addition, accessing win.__coverage__ can throw when testing cross-origin code,
71-
// because we don't control the cross-origin code, we can safely return
72-
let applicationSourceCoverage
73-
try {
74-
applicationSourceCoverage = win?.__coverage__
75-
} catch {}
69+
// if application code has been instrumented, the app iframe "window" has an object
70+
const applicationSourceCoverage = win.__coverage__
7671
if (!applicationSourceCoverage) {
7772
return
7873
}
@@ -148,39 +143,49 @@ const registerHooks = () => {
148143
// we can only request server-side code coverage
149144
// if we are running end-to-end tests,
150145
// otherwise where do we send the request?
151-
const url = Cypress._.get(
146+
const captureUrls = Cypress._.get(
152147
Cypress.env('codeCoverage'),
153148
'url',
154149
'/__coverage__'
155150
)
156-
cy.request({
157-
url,
158-
log: false,
159-
failOnStatusCode: false
160-
})
161-
.then((r) => {
162-
return Cypress._.get(r, 'body.coverage', null)
151+
function captureCoverage(url, suffix = '') {
152+
cy.request({
153+
url,
154+
log: false,
155+
failOnStatusCode: false
163156
})
164-
.then((coverage) => {
165-
if (!coverage) {
166-
// we did not get code coverage - this is the
167-
// original failed request
168-
const expectBackendCoverageOnly = Cypress._.get(
169-
Cypress.env('codeCoverage'),
170-
'expectBackendCoverageOnly',
171-
false
172-
)
173-
if (expectBackendCoverageOnly) {
174-
throw new Error(
175-
`Expected to collect backend code coverage from ${url}`
157+
.then((r) => {
158+
return Cypress._.get(r, 'body.coverage', null)
159+
})
160+
.then((coverage) => {
161+
if (!coverage) {
162+
// we did not get code coverage - this is the
163+
// original failed request
164+
const expectBackendCoverageOnly = Cypress._.get(
165+
Cypress.env('codeCoverage'),
166+
'expectBackendCoverageOnly',
167+
false
176168
)
177-
} else {
178-
// we did not really expect to collect the backend code coverage
179-
return
169+
if (expectBackendCoverageOnly) {
170+
throw new Error(
171+
`Expected to collect backend code coverage from ${url}`
172+
)
173+
} else {
174+
// we did not really expect to collect the backend code coverage
175+
return
176+
}
180177
}
181-
}
182-
sendCoverage(coverage, 'backend')
183-
})
178+
sendCoverage(coverage, `backend${suffix}`)
179+
})
180+
}
181+
182+
if (Array.isArray(captureUrls)) {
183+
for (const [index, url] of captureUrls.entries()) {
184+
captureCoverage(url, `_${index}`)
185+
}
186+
} else {
187+
captureCoverage(captureUrls)
188+
}
184189
}
185190
})
186191

0 commit comments

Comments
 (0)