Component testing checks small parts of an app alone. E2E testing checks the whole app working together. Both help find bugs early and keep apps working well.
When you want to test a single button or form works correctly by itself.
When you want to check if a user can complete a full signup process from start to finish.
When you want to quickly find bugs in a small part without running the whole app.
When you want to make sure all parts of the app work together as expected.
When you want to test user flows like logging in, adding items to cart, and checking out.
Syntax
Cypress
Component Test Example:
cy.mount(<MyButton />)
cy.get('button').click()
cy.get('button').should('contain', 'Clicked')
E2E Test Example:
cy.visit('/login')
cy.get('input[name=email]').type('user@example.com')
cy.get('input[name=password]').type('password123')
cy.get('button[type=submit]').click()
cy.url().should('include', '/dashboard')
Component tests use cy.mount() to load just one part.
E2E tests use cy.visit() to open full pages like a user.
Examples
This component test checks the login form alone works without errors.
The component test mounts a button and checks it changes text on click. The E2E test visits the login page, fills the form, submits, and checks the URL changes to dashboard.
Cypress
// Component Testimport MyButton from'./MyButton'
describe('MyButton Component', () => {
it('shows clicked text after click', () => {
cy.mount(<MyButton />)
cy.get('button').click()
cy.get('button').should('contain', 'Clicked')
})
})
// E2E Test
describe('Login Flow', () => {
it('logs in user successfully', () => {
cy.visit('/login')
cy.get('input[name=email]').type('user@example.com')
cy.get('input[name=password]').type('password123')
cy.get('button[type=submit]').click()
cy.url().should('include', '/dashboard')
})
})
OutputSuccess
Important Notes
Component tests run faster because they test small parts only.
E2E tests cover full user journeys but take longer to run.
Use component tests for quick checks and E2E tests for full app confidence.
Summary
Component testing checks small parts alone.
E2E testing checks the whole app working together.
Both help catch bugs and improve app quality.
Practice
(1/5)
1. What is the main focus of component testing in Cypress?
easy
A. Testing the entire app flow from start to finish
B. Testing individual parts of the app in isolation
C. Checking the app's performance under load
D. Verifying database connections
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 B
Quick Check:
Component testing = isolated parts [OK]
Hint: Component testing isolates parts, not full app [OK]
Common Mistakes:
Confusing component testing with E2E testing
Thinking component testing checks full app flow
Assuming component testing checks performance
2. Which of the following is the correct Cypress command to mount a React component for component testing?
easy
A. cy.visit('/component')
B. cy.request('/api/component')
C. cy.mount()
D. cy.get('MyComponent').click()
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, and cy.request() makes HTTP calls, none mount components.
Final Answer:
cy.mount(<MyComponent />) -> Option C
Quick Check:
Mount component = cy.mount() [OK]
Hint: Use cy.mount() to render components in tests [OK]
Common Mistakes:
Using cy.visit() for component testing
Confusing cy.get() with mounting
Trying to mount components with cy.request()
3. Given this Cypress E2E test code, what will be the output in the test report?
In Cypress component testing, components must be mounted using JSX syntax like cy.mount(<MyButton />), not just the component name.
Step 2: Verify other parts
The selector cy.get('button') and assertion are correct. cy.mount is valid in component tests.
Final Answer:
Missing JSX syntax when mounting component -> Option A
Quick Check:
Use JSX syntax in cy.mount() [OK]
Hint: Always wrap component in <> when mounting [OK]
Common Mistakes:
Passing component without JSX syntax to cy.mount
Thinking cy.mount is invalid in component tests
Misreading selector as incorrect
5. You want to test a login form component in isolation but also verify the full login flow including navigation. Which Cypress testing approach should you use for each task?
hard
A. Use component testing for form UI; use E2E testing for full login flow
B. Use E2E testing for form UI; use component testing for full login flow
C. Use only component testing for both tasks
D. Use only E2E testing for both tasks
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 A