Skip to content
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -549,14 +549,21 @@ Deeply nested objects will sometimes have `[object Object]` values printed. You
DEBUG_DEPTH=10 DEBUG=code-coverage npm run dev
```

### Common issues
## Common issues

Common issue: [not instrumenting your application when running Cypress](#instrument-your-application).

If the plugin worked before in version X but stopped after upgrading to version Y, please try the [released versions](https://github.com/cypress-io/code-coverage/releases) between X and Y to see where the breaking change was.

If you decide to open an issue in this repository, please fill in all information the [issue template](https://github.com/cypress-io/code-coverage/blob/master/.github/ISSUE_TEMPLATE/bug_report.md) asks for. The issues most likely to be resolved have debug logs, screenshots, and hopefully public repository links so we can try running the tests ourselves.

### Coverage reporting timeouts

If the plugin times out when sending coverage report data to be merged, this may be due to a very large
report being sent across processes. You can batch the report by setting the `sendCoverageBatchSize` environment
variable in your `cypress.config.js` file's 'env' section. Assign the variable an integer value representing
the number of report keys to send per batch.

## Contributing

You can test changes locally by running tests and confirming that the code coverage has been calculated and saved.
Expand Down
14 changes: 13 additions & 1 deletion support-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,19 @@ function fixSourcePaths(coverage) {
})
}

/**
* Validates and returns the configured batch size for
* sending coverage to the backend
*/
function getSendCoverageBatchSize() {
const batchSize = Cypress.env('sendCoverageBatchSize')
const parsedBatchSize = parseInt(batchSize)
const isValid = !isNaN(parsedBatchSize) && parsedBatchSize > 0
return isValid ? parsedBatchSize : null
}

module.exports = {
fixSourcePaths,
filterFilesFromCoverage
filterFilesFromCoverage,
getSendCoverageBatchSize
}
40 changes: 35 additions & 5 deletions support.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@

const dayjs = require('dayjs')
var duration = require('dayjs/plugin/duration')
const { filterFilesFromCoverage } = require('./support-utils')
const {
filterFilesFromCoverage,
getSendCoverageBatchSize
} = require('./support-utils')

dayjs.extend(duration)

Expand All @@ -16,10 +19,37 @@ const sendCoverage = (coverage, pathname = '/') => {

const totalCoverage = filterFilesFromCoverage(coverage)

// stringify coverage object for speed
cy.task('combineCoverage', JSON.stringify(totalCoverage), {
log: false
})
const envBatchSize = getSendCoverageBatchSize()
const keys = Object.keys(totalCoverage)

if (envBatchSize && envBatchSize < keys.length) {
sendBatchCoverage(totalCoverage, envBatchSize)
} else {
cy.task('combineCoverage', JSON.stringify(totalCoverage), {
log: false
})
}
}

/**
* Sends collected code coverage object to the backend code
* in batches via "cy.task".
*/
const sendBatchCoverage = (totalCoverage, batchSize) => {
const keys = Object.keys(totalCoverage)

for (let i = 0; i < keys.length; i += batchSize) {
const batchKeys = keys.slice(i, i + batchSize)
const batchCoverage = {}

batchKeys.forEach((key) => {
batchCoverage[key] = totalCoverage[key]
})

cy.task('combineCoverage', JSON.stringify(batchCoverage), {
log: false
})
}
}

/**
Expand Down
Loading