Skip to content
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

feat: support multiple servers from which to gather code coverage #880

Merged
Show file tree
Hide file tree
Changes from 1 commit
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
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,18 @@ if (global.__coverage__) {
}
```

Or if you have multiple servers from which you are wanting to gather code coverage, you can pass any array to `url as well:
ryanthemanuel marked this conversation as resolved.
Show resolved Hide resolved

```json
{
"env": {
"codeCoverage": {
"url": ["http://localhost:3000/__coverage__", "http://localhost:3001/__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
Expand Down
71 changes: 38 additions & 33 deletions support.js
ryanthemanuel marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,8 @@ const registerHooks = () => {
windowCoverageObjects = []

const saveCoverageObject = (win) => {
// if the application code has been instrumented, then the app iframe "win.__coverage__" will be available,
// in addition, accessing win.__coverage__ can throw when testing cross-origin code,
// because we don't control the cross-origin code, we can safely return
let applicationSourceCoverage
try {
applicationSourceCoverage = win?.__coverage__
} catch {}
// if application code has been instrumented, the app iframe "window" has an object
const applicationSourceCoverage = win.__coverage__
ryanthemanuel marked this conversation as resolved.
Show resolved Hide resolved
if (!applicationSourceCoverage) {
return
}
Expand Down Expand Up @@ -148,39 +143,49 @@ const registerHooks = () => {
// we can only request server-side code coverage
// if we are running end-to-end tests,
// otherwise where do we send the request?
const url = Cypress._.get(
const captureUrls = Cypress._.get(
Cypress.env('codeCoverage'),
'url',
'/__coverage__'
)
cy.request({
url,
log: false,
failOnStatusCode: false
})
.then((r) => {
return Cypress._.get(r, 'body.coverage', null)
function captureCoverage(url, suffix = '') {
cy.request({
url,
log: false,
failOnStatusCode: false
})
.then((coverage) => {
if (!coverage) {
// we did not get code coverage - this is the
// original failed request
const expectBackendCoverageOnly = Cypress._.get(
Cypress.env('codeCoverage'),
'expectBackendCoverageOnly',
false
)
if (expectBackendCoverageOnly) {
throw new Error(
`Expected to collect backend code coverage from ${url}`
.then((r) => {
return Cypress._.get(r, 'body.coverage', null)
})
.then((coverage) => {
if (!coverage) {
// we did not get code coverage - this is the
// original failed request
const expectBackendCoverageOnly = Cypress._.get(
Cypress.env('codeCoverage'),
'expectBackendCoverageOnly',
false
)
} else {
// we did not really expect to collect the backend code coverage
return
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')
})
sendCoverage(coverage, `backend${suffix}`)
})
}

if (Array.isArray(captureUrls)) {
for (const [index, url] of captureUrls.entries()) {
captureCoverage(url, `_${index}`)
}
} else {
captureCoverage(captureUrls)
}
}
})

Expand Down
Loading