0
0
Cypresstesting~10 mins

Component testing vs E2E in Cypress - Test Execution Compared

Choose your learning style9 modes available
Test Overview

This test compares a simple component test and an end-to-end (E2E) test using Cypress. The component test checks if a button renders and responds to a click event. The E2E test verifies the full user flow of opening a page, clicking a button, and seeing a result.

Test Code - Cypress
Cypress
import { mount } from 'cypress/react';
import React, { useState } from 'react';

// Simple Button component
function Button() {
  const [clicked, setClicked] = useState(false);
  return (
    <button onClick={() => setClicked(true)} aria-label="click-button">
      {clicked ? 'Clicked!' : 'Click me'}
    </button>
  );
}

// Component Test
describe('Button Component Test', () => {
  it('renders and changes text on click', () => {
    mount(<Button />);
    cy.get('button[aria-label="click-button"]').should('contain.text', 'Click me');
    cy.get('button[aria-label="click-button"]').click();
    cy.get('button[aria-label="click-button"]').should('contain.text', 'Clicked!');
  });
});

// E2E Test
// Assume the app page renders the Button component
describe('Button E2E Test', () => {
  it('loads page and button changes text on click', () => {
    cy.visit('/button-page');
    cy.get('button[aria-label="click-button"]').should('contain.text', 'Click me');
    cy.get('button[aria-label="click-button"]').click();
    cy.get('button[aria-label="click-button"]').should('contain.text', 'Clicked!');
  });
});
Execution Trace - 8 Steps
StepActionSystem StateAssertionResult
1Test starts for Button Component TestCypress test runner initializes component mount environment-PASS
2Mount Button component in isolationButton renders with text 'Click me'Check button text is 'Click me'PASS
3Click the buttonButton state changes internally-PASS
4Verify button text changed to 'Clicked!'Button shows updated textButton text is 'Clicked!'PASS
5Test starts for Button E2E TestBrowser opens and navigates to '/button-page'-PASS
6Find button on pageButton visible with text 'Click me'Button text is 'Click me'PASS
7Click the buttonButton updates after click-PASS
8Verify button text changed to 'Clicked!'Button shows updated textButton text is 'Clicked!'PASS
Failure Scenario
Failing Condition: Button element not found or text does not update after click
Execution Trace Quiz - 3 Questions
Test your understanding
What does the component test verify?
AButton renders and changes text on click
BFull page navigation and button click
CDatabase connection
DAPI response status
Key Result
Component tests focus on isolated UI parts quickly, while E2E tests cover full user flows including navigation and integration.