0
0
Cypresstesting~10 mins

Component test setup in Cypress - Test Execution Trace

Choose your learning style9 modes available
Test Overview

This test sets up a simple React component in Cypress for testing. It verifies that the component renders correctly with the expected text.

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

function Greeting({ name }) {
  return <h1>Hello, {name}!</h1>;
}

describe('Greeting component', () => {
  it('renders greeting message', () => {
    mount(<Greeting name="Alice" />);
    cy.get('h1').should('contain.text', 'Hello, Alice!');
  });
});
Execution Trace - 4 Steps
StepActionSystem StateAssertionResult
1Test startsCypress test runner is ready-PASS
2Mount the Greeting component with prop name='Alice'React component Greeting is rendered inside Cypress iframe-PASS
3Find the <h1> elementThe DOM contains <h1>Hello, Alice!</h1>cy.get('h1') locates the heading elementPASS
4Check that <h1> contains text 'Hello, Alice!'The heading text is visiblecy.get('h1').should('contain.text', 'Hello, Alice!') verifies correct textPASS
Failure Scenario
Failing Condition: The component does not render or the text is incorrect
Execution Trace Quiz - 3 Questions
Test your understanding
What does the mount() function do in this test?
AIt renders the React component inside Cypress for testing
BIt clicks a button in the component
CIt navigates to a new page
DIt waits for an element to appear
Key Result
Always verify your component is properly mounted with the correct props before asserting on its rendered output in component tests.