How do e2e tests differ from component testing in Cypress?
An e2e test walks a scenario through the whole application, while component testing mounts a single component with cy.mount in isolation. Component tests are fast, isolated and good for trying states and props, but do not catch integration problems with the backend and routing. So component states are checked with component tests, and the main end-to-end paths stay with e2e.
Cypress · Architecture
To check a discount on a price, a test logs in, opens the catalog and the cart. Isn’t that too long?
A team has an e2e test for one button that walks half the app: login, catalog, cart. It is slow, breaks from unrelated changes and checks a small thing. Cypress offers a second kind of test in which such a component state is checked on its own. This lesson explains the difference between e2e and component testing and how to choose the kind of test.
How it works and why. E2E opens the whole application through cy.visit and walks a scenario like a user, with a real backend or network mocks. Component testing mounts one React, Vue or Angular component through cy.mount in a real browser, without the rest of the app. The browser and commands are the same, but the test subject differs: a page versus a component. So component testing is fast and isolated: handy for checking states of a button, form or table with different props without starting the whole front end.
Two kinds of tests in Cypress
The whole application
cy.visit opens the page and the test walks a scenario like a user, with a real backend or network mocks. Slower, closer to reality.
One component
cy.mount mounts a React, Vue or Angular component in a real browser without the rest of the app. Fast and isolated.
They are configured separately: the config has an e2e block and a component block, and for components you also say how to build them (framework and bundler) and register mount in the support/component.ts file.
Example: moving a check from e2e to a component test. Before:
// E2E: to see a discount we walk half the app
it('shows the discount in the cart', () => {
cy.login('qa@example.com', 'secret');
cy.visit('/catalog');
cy.get('[data-cy=product-42]').click();
cy.get('[data-cy=add-to-cart]').click();
cy.visit('/cart');
cy.get('[data-cy=final-price]').should('have.text', '80');
});
The test checks one thing, the final price calculation, but depends on login, the catalog, the cart and the data on the environment. If any step fails, the test is red although the component itself is fine. After: the same calculation is checked on the component with the needed props.
// a component test
import { PriceTag } from './PriceTag';
it('shows the discount', () => {
cy.mount(<PriceTag price={100} discount={20} />);
cy.get('[data-cy=final-price]').should('have.text', '80');
});
The test runs in seconds, does not depend on the backend and easily covers edge cases: zero discount, a fractional price, very large numbers. The config for components:
export default defineConfig({
component: {
// how components are built: framework and bundler
devServer: { framework: 'react', bundler: 'vite' },
},
});
E2E keeps what a component cannot prove: that login, the catalog and the cart work together. Such a split forms a practical pyramid: many fast component tests and a few end-to-end ones for the main paths.
In Playwright component testing is now built in: component tests are regular @playwright/test tests with the mount fixture, and the experimental @playwright/experimental-ct-* packages were removed and are no longer published. In Cypress CT has long been stable, with official adapters for React, Next.js, Vue and Angular (Svelte is still in alpha).
Limits and alternatives. A component test does not check integration with a real backend, routing and the page as a whole: that is e2e's job. It needs a configured build (a bundler) and may need mocks for contexts or services the component expects. If a component is hard to mount alone, that signals heavy dependencies. For pure logic with no DOM, plain unit tests are faster.
Common mistakes.
- Checking every small UI detail with an e2e test: slow and brittle.
- Expecting a component test to catch integration problems with the backend or routes.
- Forgetting to register
mountin support or to configuredevServer: the test will not build. - Using component tests as a replacement for e2e: the main user paths still need an end-to-end check.
Check yourself
Which kind of test do you choose to check that a button is disabled when a form is empty, and why?
Answer
A component one: the button state depends only on the component props and internal state, and cy.mount lets you mount it without login and a backend and quickly go through variants. E2E here is slower and depends on unrelated steps.
A component test of a payment form is green, but in production payment fails because of a wrong request URL. Why did the test not catch it?
Answer
The component is mounted apart from the real app and backend: integration with routes and the API was not checked. That is e2e territory (or a test with network interception) for the main payment path.