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.
This test sets up a simple React component in Cypress for testing. It verifies that the component renders correctly with the expected text.
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!'); }); });
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Test starts | Cypress test runner is ready | — | PASS |
| 2 | Mount the Greeting component with prop name='Alice' | React component Greeting is rendered inside Cypress iframe | — | PASS |
| 3 | Find the <h1> element | The DOM contains <h1>Hello, Alice!</h1> | cy.get('h1') locates the heading element | PASS |
| 4 | Check that <h1> contains text 'Hello, Alice!' | The heading text is visible | cy.get('h1').should('contain.text', 'Hello, Alice!') verifies correct text | PASS |
mount() in Cypress component testing?mount()mount() is used to load a UI component inside Cypress so you can test it in isolation.mount() focuses on small parts (components).beforeEach() runs before every test, ideal for fresh component setup.beforeEach(() => { mount(<MyComponent />) }) correctly mounts the component before each test.describe('Button component', () => {
beforeEach(() => {
mount(Click me)
})
it('checks button text', () => {
cy.get('button').should('have.text', 'Click me')
})
})mount() loads a button with text 'Click me'. The selector cy.get('button') correctly targets it.should('have.text', 'Click me') matches the button's text exactly, so it passes.describe('Input component', () => {
beforeEach(() => {
mount('')
})
it('checks placeholder', () => {
cy.get('input').should('have.attr', 'placeholder', 'Enter name')
})
})cy.get('input') and attribute check are correct, so no error there.<List items={["a", "b"]} />. This is correct in beforeEach(() => { mount(<List items={["a", "b"]} />) }).Pass the test and this pattern moves unknown