Mount a simple React Button component and verify its text
Preconditions (2)
✅ Expected Result: The Button component mounts successfully and the button displays the text 'Click Me'
import React from 'react'; import { mount } from '@cypress/react'; import Button from '../../src/components/Button'; describe('Button component mount test', () => { it('mounts Button and checks label text', () => { mount(<Button label="Click Me" />); cy.get('button').should('exist').and('have.text', 'Click Me'); }); });
The test imports React and the mount function from @cypress/react. It also imports the Button component to test.
Inside the test, mount() renders the Button with the prop label="Click Me". Then, cy.get('button') finds the button element in the mounted component.
The assertions check that the button exists and its text exactly matches 'Click Me'. This confirms the component rendered correctly with the given prop.
This approach uses Cypress best practices: mounting React components directly, using semantic selectors, and clear assertions.
Now add data-driven testing to mount the Button component with 3 different labels and verify each text
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