Code coverage plugin in Cypress - Build an Automation Script
/// <reference types="cypress" /> // cypress/support/e2e.js import '@cypress/code-coverage/support'; // cypress/plugins/index.js const codeCoverageTask = require('@cypress/code-coverage/task'); module.exports = (on, config) => { codeCoverageTask(on, config); return config; }; // cypress/e2e/code_coverage_spec.cy.js describe('Code Coverage Plugin Test', () => { before(() => { cy.visit('/'); }); it('should run application code and collect coverage', () => { // Example interaction that triggers application code cy.get('button#increment').click(); cy.get('span#count').should('contain.text', '1'); }); after(() => { // Verify that coverage object exists in window cy.window().its('__coverage__').should('exist'); }); });
The cypress/support/e2e.js file imports the code coverage support commands to enable coverage collection during tests.
The cypress/plugins/index.js file registers the code coverage task that writes coverage data to disk after tests complete.
The test file code_coverage_spec.cy.js visits the application, performs an action to run application code, and asserts expected UI changes.
After the test, it checks that the __coverage__ object exists on the window, confirming coverage data was collected.
This setup follows Cypress best practices by using the official plugin API, proper hooks, and assertions to verify coverage collection.
Now add data-driven testing with 3 different button click counts and verify coverage is collected for each
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
