or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

coverage-collection.mdcoverage-processing.mdindex.mdplugin-registration.mdserver-middleware.md
tile.json

coverage-collection.mddocs/

Coverage Collection

The coverage collection system provides browser-side hooks and utilities that automatically collect coverage data during Cypress test execution. It monitors window coverage objects, handles frontend and backend coverage aggregation, and manages coverage transmission to the plugin backend.

Capabilities

Coverage Data Transmission

Functions for sending collected coverage data from the browser to the Cypress backend via tasks.

/**
 * Sends collected coverage data to backend via cy.task
 * @param coverage - Coverage object collected from browser
 * @param pathname - Path identifier for the coverage (default: '/')
 */
function sendCoverage(coverage: Coverage, pathname?: string): void;

/**
 * Sends coverage data in batches for large datasets
 * @param totalCoverage - Complete coverage object to send
 * @param batchSize - Number of files to send per batch
 */
function sendBatchCoverage(totalCoverage: Coverage, batchSize: number): void;

Usage:

// Automatic usage via support hooks
// Manual usage (if needed):
sendCoverage(window.__coverage__, '/dashboard')

// For large coverage objects:
sendBatchCoverage(combinedCoverage, 50)

The system automatically filters coverage data before transmission:

  • Removes test specification files
  • Excludes Cypress support files
  • Applies custom exclusion patterns from configuration

Logging Utilities

Consistent logging system that identifies coverage plugin messages in Cypress Command Log.

/**
 * Logs messages with plugin identification
 * @param message - Message to display in Command Log
 */
function logMessage(message: string): void;

Messages appear as: message [@cypress/code-coverage]

Lifecycle Hooks

The coverage collection system automatically registers Cypress hooks to handle coverage throughout the test lifecycle.

Before Hook

Resets coverage for interactive mode testing to prevent accumulation across test runs.

/**
 * Automatically registered before hook
 * Resets coverage in interactive mode
 */
before(() => {
  cy.task('resetCoverage', { 
    isInteractive: Cypress.config('isInteractive') 
  })
})

Before Each Hook

Sets up coverage object tracking for each test, monitoring all window objects for coverage data.

/**
 * Automatically registered beforeEach hook
 * Initializes coverage tracking for current test
 */
beforeEach(() => {
  // Captures coverage from:
  // - window:load events (page navigation)  
  // - before() hook visits
  // - cy.window() calls
})

Handles multiple coverage scenarios:

  • Single page applications
  • Multi-page navigation
  • Cross-origin content (safely ignored)
  • Window reload events

After Each Hook

Collects and transmits coverage data accumulated during the test.

/**
 * Automatically registered afterEach hook
 * Sends collected coverage data to backend
 */
afterEach(() => {
  // Sends coverage for each tracked window
  // Handles unit test coverage from spec bundle
  // Warns if no coverage found (configurable)
})

Coverage collection priorities:

  1. E2E Coverage: From instrumented application windows
  2. Unit Test Coverage: From Cypress spec bundle if instrumented
  3. Warning Messages: If no coverage detected (can be disabled)

After Hooks

Final coverage collection and report generation after all tests complete.

/**
 * Backend coverage collection hook
 * Collects server-side coverage via HTTP requests
 */
after(function collectBackendCoverage() {
  // Requests coverage from /__coverage__ endpoints
  // Supports multiple backend URLs
  // Handles frontend-only vs backend-only scenarios
})

/**
 * Unit test coverage merging hook  
 * Merges coverage from instrumented spec files
 */
after(function mergeUnitTestCoverage() {
  // Collects window.__coverage__ from spec iframe
  // Merges with main coverage data
})

/**
 * Report generation hook
 * Triggers final coverage report creation
 */
after(function generateReport() {
  // Calls coverageReport task
  // 3-minute timeout for large reports
  // Returns coverage report folder path
})

Configuration Options

Coverage collection behavior is controlled through Cypress environment variables:

Coverage Control

// Disable coverage collection entirely
Cypress.env('coverage') // Set to false to disable

// Backend coverage settings
Cypress.env('codeCoverage') = {
  // Additional exclusion patterns
  exclude: ['**/vendor/**', '**/node_modules/**'],
  
  // Backend coverage endpoint (default: '/__coverage__')
  url: '/api/coverage',
  
  // Multiple backend URLs
  url: ['/api/coverage', '/admin/coverage'],
  
  // Coverage expectations
  expectBackendCoverageOnly: true,  // Only collect backend
  expectFrontendCoverageOnly: true, // Only collect frontend
}

// Batch transmission settings
Cypress.env('sendCoverageBatchSize') // Number for batch size

Path Exclusion

The system automatically excludes certain file patterns:

  • Test files: Based on specPattern or testFiles config
  • Integration folder: Legacy Cypress versions
  • Support files: Cypress support directory files
  • Custom patterns: Via codeCoverage.exclude environment variable

Error Handling

Coverage collection includes comprehensive error handling:

Cross-origin protection:

try {
  applicationSourceCoverage = win.__coverage__
} catch {
  // Safely ignore cross-origin access errors
}

Missing coverage warnings:

  • Detects when no coverage is collected
  • Provides setup guidance for instrumentation
  • Can be disabled via expectBackendCoverageOnly flag

Backend request failures:

  • Non-blocking HTTP requests for backend coverage
  • Graceful handling of missing endpoints
  • Detailed error messages for expected backend coverage

Performance Optimization

Batch transmission:

  • Automatically splits large coverage objects
  • Configurable batch size via environment variable
  • Prevents Cypress task payload limits

Lazy evaluation:

  • Coverage filtering applied before transmission
  • Duplicate coverage object detection
  • Efficient memory usage for long test suites