How to Reach 80%+ Test Coverage with Cypress Without Slowing Your Team Down
At Kepler51 I introduced Cypress tests to our React application and we reached more than 80% coverage across the app. High coverage is easy to get wrong: you can end up with a slow, flaky suite that nobody trusts. Here's the approach I recommend for getting there in a way that actually speeds the team up.
1. Start with what would hurt the business
Don't start by chasing a percentage. Start with a list of the user flows that would cause real damage if they broke: logging in, creating and editing the main entities, permissions, payments, exports. Write one end-to-end test per flow, covering the happy path first. Those first ten tests give you more confidence than the next hundred.
2. Use selectors that survive refactoring
Tests that select by CSS classes or DOM structure break every time someone touches the markup. Add dedicated test attributes and treat them as a contract:
<button data-testid="save-employee" onClick={save}>Save</button>
cy.get("[data-testid=save-employee]").click();
This matters even more when you are refactoring at the same time, for example while migrating React class components to hooks.
3. Control the network
Most flaky end-to-end tests are really waiting on a slow or unpredictable backend. With
cy.intercept you can wait for the exact request instead of guessing, and stub responses for
edge cases that are hard to reproduce (empty lists, errors, very long names):
cy.intercept("GET", "/api/employees*", { fixture: "employees.json" }).as("getEmployees");
cy.visit("/employees");
cy.wait("@getEmployees");
cy.get("[data-testid=employee-row]").should("have.length", 3);
Keep a small number of tests that run against the real API as well, so you know the contract between frontend and backend still holds.
4. Make login fast
Logging in through the UI in every test is slow. Test the login form once, and everywhere else log in through the API and cache the session:
// cypress/support/commands.js
Cypress.Commands.add("login", (email, password) => {
cy.session([email], () => {
cy.request("POST", "/api/auth/login", { email, password });
});
});
// in a test
beforeEach(() => {
cy.login("manager@example.com", "secret");
});
5. Measure coverage properly
To know what your end-to-end tests actually execute, instrument the app with Istanbul and collect the results with the official plugin:
// cypress.config.js
const { defineConfig } = require("cypress");
module.exports = defineConfig({
e2e: {
setupNodeEvents(on, config) {
require("@cypress/code-coverage/task")(on, config);
return config;
},
},
});
// cypress/support/e2e.js
import "@cypress/code-coverage/support";
Add babel-plugin-istanbul to the Babel config used for the test build only, so production
bundles are not instrumented. The HTML report shows exactly which branches no test reaches. That list
becomes your backlog for the next tests.
6. Run everything in CI, on every pull request
Tests that only run on someone's laptop don't protect anything. Run the suite in CI for every pull request, publish the coverage report as an artifact, and make a failing test block the merge. Once the suite grows, split it across parallel machines so feedback stays fast. Setting up a CI/CD pipeline that took deployments from days to hours earlier in my career taught me that speed of feedback is what makes people trust the process.
7. Treat flaky tests as bugs
- Never use fixed waits like
cy.wait(3000); wait for a request alias or an assertion instead. - Make each test create its own data, so tests don't depend on order.
- Quarantine a flaky test immediately and fix it within days, not months.
Coverage is a means, not the goal
80% was a useful target because it forced us to cover the important paths and many edge cases. But the real result was something else: the team could refactor and release with confidence. That is the metric that matters.
I'm a Software Engineer based in Koper, Slovenia. If your team wants to build a testing culture like this, let's talk.