Test Overview
This test mounts a simple React component using Cypress. It verifies that the component renders correctly and displays the expected text.
This test mounts a simple React component using Cypress. It verifies that the component renders correctly and displays the expected text.
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!') }) })
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Test starts | Cypress test runner is ready | — | PASS |
| 2 | Mounts the Greeting React component using mount() | Browser displays the Greeting component with <h1>Hello, Cypress!</h1> | — | PASS |
| 3 | Finds the <h1> element using cy.get('h1') | The <h1> element is present in the DOM | — | PASS |
| 4 | Checks 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 |
mount() in Cypress when testing React components?mount() in Cypressmount() is used to render React components inside the Cypress test environment so you can interact with and test them.MyButton in Cypress?cy.mount() with the component JSX, like cy.mount(<MyButton />);.cy.mount but passes a string, which is incorrect. mount(<MyButton>); misses the closing slash in JSX, causing syntax error.import { mount } from 'cypress/react';
function Greeting() {
return <h1>Hello, Cypress!</h1>;
}
describe('Greeting component', () => {
it('shows greeting text', () => {
mount(<Greeting />);
cy.get('h1').should('contain.text', 'Hello, Cypress!');
});
});Greeting component, which renders an h1 with text 'Hello, Cypress!'.cy.get('h1').should('contain.text', 'Hello, Cypress!') which checks if the h1 contains that exact text.h1 element contains the text 'Hello, Cypress!' -> Option Cimport { mount } from 'cypress/react';
function Button() {
return <button>Click me</button>;
}
describe('Button component', () => {
it('renders button', () => {
mount(Button);
cy.get('button').should('exist');
});
});mount(Button) but it should pass JSX: mount(<Button />) to render the component.mount(<Button />) -> Option AUserCard that receives a name prop and displays it inside a span. Which is the correct way to mount it in Cypress and assert the name is shown?function UserCard({ name }) {
return <span>User: {name}</span>;
}cy.mount(<UserCard name='Alice' />) to mount the component with the prop.should('have.text', 'User: Alice') to check the full text content matches exactly.cy.mount(<UserCard name="Alice" />) and contain.text('User: Alice') uses substring check instead of exact text match.cy.mount(UserCard, { props: { name: 'Alice' } }) uses invalid React mount syntax.mount(<UserCard name='Alice' />) misses cy. prefix.Pass the test and this pattern moves unknown