The plugin registration system sets up Cypress tasks for coverage collection, data aggregation, and report generation. This is the core backend component that handles all Node.js-side coverage operations.
Registers all coverage-related Cypress tasks and configures the environment for coverage collection.
/**
* Main plugin registration function that sets up coverage tasks
* @param on - Cypress plugin events handler
* @param config - Cypress configuration object
* @returns Modified config with coverage environment variables
*/
function registerCodeCoverageTasks(
on: Cypress.PluginEvents,
config: Cypress.PluginConfigOptions
): Cypress.PluginConfigOptions;Usage:
// cypress.config.js
module.exports = defineConfig({
e2e: {
setupNodeEvents(on, config) {
require('@cypress/code-coverage/task')(on, config)
return config
},
},
})Simplified plugin setup that automatically registers the main plugin.
/**
* Convenience wrapper for automatic plugin registration
* @param on - Cypress plugin events handler
* @param config - Cypress configuration object
* @returns Modified config object
*/
function conveniencePlugin(
on: Cypress.PluginEvents,
config: Cypress.PluginConfigOptions
): Cypress.PluginConfigOptions;Usage:
// Alternative setup approach
module.exports = (on, config) => {
require('@cypress/code-coverage/plugins')(on, config)
return config
}The plugin registers three core tasks that handle coverage operations:
Clears accumulated coverage information, with different behavior for interactive vs headless modes.
/**
* Resets coverage data for new test runs
* @param options - Configuration for reset behavior
* @param options.isInteractive - Whether running in interactive mode
* @returns null
*/
task['resetCoverage'](options: { isInteractive: boolean }): null;cypress runMerges coverage data from individual tests with the accumulated coverage map.
/**
* Combines coverage from single test with accumulated data
* @param sentCoverage - Stringified coverage object from browser
* @returns null
*/
task['combineCoverage'](sentCoverage: string): null;Automatically:
Generated final coverage reports using NYC and saves them to disk.
/**
* Generates coverage reports in configured formats
* @returns Promise resolving to report directory path
*/
task['coverageReport'](): Promise<string>;Report generation process:
.nyc_output/out.jsoncoverage:report)Report formats supported:
The plugin reads configuration from multiple sources in order of precedence:
.nyc_output/ directory settingspackage.json nyc field.nycrc, .nycrc.json, .nycrc.yaml, .nycrc.yml filesnyc.config.js, nyc.config.cjs filesDefault configuration:
{
'report-dir': './coverage',
'temp-dir': './.nyc_output',
reporter: ['lcov', 'clover', 'json', 'json-summary'],
extension: ['.js', '.cjs', '.mjs', '.ts', '.tsx', '.jsx'],
excludeAfterRemap: false
}The plugin sets environment variables for coordination with browser-side hooks:
config.env.codeCoverageTasksRegistered = true - Signals tasks are availableThe plugin handles common error scenarios: