Component testing vs E2E in Cypress - Automation Approaches Compared
/// <reference types="cypress" /> // Component Test for Button import Button from '../../src/components/Button.vue'; describe('Button Component Test', () => { it('renders with text and responds to click', () => { const onClick = cy.stub(); cy.mount(<Button onClick={onClick}>Submit</Button>); cy.get('[data-cy=button]').should('be.visible').and('have.text', 'Submit'); cy.get('[data-cy=button]').click().then(() => { expect(onClick).to.have.been.calledOnce; }); }); }); // E2E Test for Login Flow describe('Login Flow E2E Test', () => { it('logs in and redirects to dashboard', () => { cy.visit('/login'); cy.get('[data-cy=email-input]').type('user@example.com'); cy.get('[data-cy=password-input]').type('Password123'); cy.get('[data-cy=login-button]').click(); cy.url().should('include', '/dashboard'); cy.get('[data-cy=welcome-text]').should('contain.text', 'Welcome, user!'); }); });
The solution has two parts: component test and E2E test.
In the component test, we mount the Button component using Cypress component testing. We pass a stub function to track clicks. We check the button text and simulate a click, then verify the click handler was called once.
In the E2E test, we visit the login page URL. We fill the email and password fields using selectors with data-cy attributes. We click the login button, then verify the URL changed to the dashboard. Finally, we check the dashboard page shows the welcome message.
This separation shows how component testing focuses on a small piece in isolation, while E2E tests the full user flow in the real app.
Now add data-driven testing for the login flow with 3 different user credentials
Practice
component testing in Cypress?Solution
Step 1: Understand component testing scope
Component testing focuses on small parts or components of the app, testing them alone without the full app context.Step 2: Compare with other testing types
E2E testing checks the whole app working together, so it is not the focus of component testing.Final Answer:
Testing individual parts of the app in isolation -> Option BQuick Check:
Component testing = isolated parts [OK]
- Confusing component testing with E2E testing
- Thinking component testing checks full app flow
- Assuming component testing checks performance
Solution
Step 1: Identify component testing command
In Cypress component testing,cy.mount()is used to render a component in isolation.Step 2: Eliminate other commands
cy.visit()is for E2E page visits,cy.get()selects elements, andcy.request()makes HTTP calls, none mount components.Final Answer:
cy.mount(<MyComponent />) -> Option CQuick Check:
Mount component = cy.mount() [OK]
- Using cy.visit() for component testing
- Confusing cy.get() with mounting
- Trying to mount components with cy.request()
describe('Login flow', () => {
it('should show error on wrong password', () => {
cy.visit('/login')
cy.get('input[name="username"]').type('user1')
cy.get('input[name="password"]').type('wrongpass')
cy.get('button[type="submit"]').click()
cy.contains('Invalid credentials').should('be.visible')
})
})Solution
Step 1: Analyze test steps
The test visits the login page, enters username and wrong password, clicks submit, then checks for error message visibility.Step 2: Verify correctness of commands
All commands are valid Cypress E2E commands. The assertion checks if the error message is visible, so test passes if message appears.Final Answer:
Test passes if 'Invalid credentials' message appears -> Option DQuick Check:
E2E test passes if error message visible [OK]
- Thinking cy.visit() is invalid in E2E
- Ignoring the assertion on error message
- Assuming syntax errors in selectors without cause
import MyButton from './MyButton'
describe('MyButton component', () => {
it('renders button text', () => {
cy.mount(MyButton)
cy.get('button').should('contain.text', 'Click me')
})
})Solution
Step 1: Check component mounting syntax
In Cypress component testing, components must be mounted using JSX syntax likecy.mount(<MyButton />), not just the component name.Step 2: Verify other parts
The selectorcy.get('button')and assertion are correct. cy.mount is valid in component tests.Final Answer:
Missing JSX syntax when mounting component -> Option AQuick Check:
Use JSX syntax in cy.mount() [OK]
- Passing component without JSX syntax to cy.mount
- Thinking cy.mount is invalid in component tests
- Misreading selector as incorrect
Solution
Step 1: Match testing types to tasks
Component testing is best for isolated UI parts like the login form component itself.Step 2: Use E2E for full app flow
To verify the entire login flow including navigation and backend, E2E testing is appropriate.Final Answer:
Use component testing for form UI; use E2E testing for full login flow -> Option AQuick Check:
Component = UI parts, E2E = full flow [OK]
- Mixing up which test type suits UI vs full flow
- Trying to test full flow with component tests
- Ignoring navigation in login flow testing
Ready to prove it?
Pass the test and this pattern moves unknown
