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.
Practice
Solution
Step 1: Understand code coverage concept
Code coverage helps identify which lines or branches of code are executed during tests.Step 2: Match purpose with plugin function
The Cypress code coverage plugin tracks and reports tested vs untested code parts.Final Answer:
To show which parts of your code are tested and which are not -> Option AQuick Check:
Code coverage = tested vs untested code [OK]
- Thinking it speeds up tests
- Believing it fixes bugs automatically
- Confusing it with screenshot tools
Solution
Step 1: Identify where plugin support code goes
The plugin support code is added in the support file to load before tests run.Step 2: Choose correct support file path
By default, Cypress usescypress/support/e2e.jsfor test support setup.Final Answer:
cypress/support/e2e.js -> Option AQuick Check:
Support file = cypress/support/e2e.js [OK]
- Modifying cypress.config.js only
- Changing package.json for plugin support
- Editing fixture files for plugin setup
cypress/support/e2e.js:
import '@cypress/code-coverage/support';What will happen when you run your tests?
Solution
Step 1: Understand import statement effect
Importing '@cypress/code-coverage/support' enables coverage collection hooks.Step 2: Recognize plugin behavior during test run
This import activates coverage tracking so data is gathered as tests run.Final Answer:
Code coverage data will be collected during tests -> Option CQuick Check:
Import support = coverage data collected [OK]
- Assuming import alone fails tests
- Thinking config changes alone enable coverage
- Believing coverage report is empty without extra steps
Solution
Step 1: Understand coverage instrumentation
Coverage requires your app code to be instrumented to track executed lines.Step 2: Identify missing instrumentation effect
Without instrumentation, coverage plugin cannot detect which code ran, so report is empty.Final Answer:
You forgot to instrument your app code with the coverage tool -> Option BQuick Check:
Missing instrumentation = empty coverage report [OK]
- Assuming no tests means empty report
- Thinking duplicate imports cause empty report
- Believing headless mode disables coverage
Solution
Step 1: Install the code coverage plugin
First, add the plugin package to your project dependencies.Step 2: Instrument your application code
Modify your app build to include coverage instrumentation for tracking.Step 3: Import the plugin support file in cypress/support/e2e.js
This enables coverage hooks during test execution.Step 4: Run your Cypress tests
Tests will now collect coverage data as they execute.Step 5: Check the generated coverage report
Review which code was covered and improve tests accordingly.Final Answer:
Install plugin, instrument app code, import support in e2e.js, run tests, check report -> Option DQuick Check:
Correct setup sequence = Install plugin, instrument app code, import support in e2e.js, run tests, check report [OK]
- Running tests before installing plugin
- Skipping instrumentation step
- Importing support after running tests
Ready to prove it?
Pass the test and this pattern moves unknown
