Code coverage plugin helps you see which parts of your code are tested. It shows what is covered and what is missed.
Code coverage plugin in Cypress
1. Install plugin: npm install --save-dev @cypress/code-coverage 2. Add to Cypress plugins file (cypress.config.js): import { defineConfig } from 'cypress' import codeCoverage from '@cypress/code-coverage/task' export default defineConfig({ e2e: { setupNodeEvents(on, config) { codeCoverage(on, config) return config } } }) 3. Add to support file (cypress/support/e2e.js): import '@cypress/code-coverage/support' 4. Run tests and view coverage report in coverage folder.
Make sure to instrument your app code for coverage before running tests.
Coverage reports are usually in lcov or html format for easy viewing.
import { defineConfig } from 'cypress' import codeCoverage from '@cypress/code-coverage/task' export default defineConfig({ e2e: { setupNodeEvents(on, config) { codeCoverage(on, config) return config } } })
import '@cypress/code-coverage/support' // Now your tests will collect coverage data automatically.
This complete setup shows how to configure Cypress with the code coverage plugin, import support, and write a simple test. When you run this test, coverage data is collected and saved.
// cypress.config.js import { defineConfig } from 'cypress' import codeCoverage from '@cypress/code-coverage/task' export default defineConfig({ e2e: { setupNodeEvents(on, config) { codeCoverage(on, config) return config } } }) // cypress/support/e2e.js import '@cypress/code-coverage/support' // cypress/e2e/sample_spec.cy.js describe('Simple test with coverage', () => { it('Visits example page', () => { cy.visit('https://example.cypress.io') cy.contains('type').click() cy.url().should('include', '/commands/actions') }) })
Always instrument your app code with a tool like babel-plugin-istanbul or nyc before running Cypress tests to get accurate coverage.
Coverage reports help find untested code so you can write better tests.
Use the coverage folder to open HTML reports in a browser for easy reading.
Code coverage plugin shows which code your tests run and which they miss.
Install and configure it in Cypress config and support files.
Run tests and check coverage reports to improve your test quality.