0
0
Cypresstesting~10 mins

Mounting React components in Cypress - Test Execution Trace

Choose your learning style9 modes available
Test Overview

This test mounts a simple React component using Cypress. It verifies that the component renders correctly and displays the expected text.

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

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

describe('Mount React Component', () => {
  it('renders the Greeting component', () => {
    mount(<Greeting />)
    cy.get('h1').should('contain.text', 'Hello, Cypress!')
  })
})
Execution Trace - 4 Steps
StepActionSystem StateAssertionResult
1Test startsCypress test runner is ready-PASS
2Mounts the Greeting React component using mount()Browser displays the Greeting component with <h1>Hello, Cypress!</h1>-PASS
3Finds the <h1> element using cy.get('h1')The <h1> element is present in the DOM-PASS
4Checks that the <h1> contains text 'Hello, Cypress!' using .should('contain.text')The <h1> text matches 'Hello, Cypress!'Verify <h1> text content equals 'Hello, Cypress!'PASS
Failure Scenario
Failing Condition: The Greeting component does not render or the <h1> text is different
Execution Trace Quiz - 3 Questions
Test your understanding
What does the mount() function do in this test?
AIt waits for the component to load data
BIt clicks a button inside the component
CIt renders the React component inside the Cypress test browser
DIt asserts the component text
Key Result
Always verify that the React component renders the expected content by asserting visible text after mounting it in Cypress.