Setup and render a React component for testing
Preconditions (2)
✅ Expected Result: The Button component is rendered and visible in the Cypress test runner
import React from 'react'; import { mount } from '@cypress/react'; import Button from '../../src/components/Button'; describe('Component test setup for Button', () => { it('should render the Button component and verify visibility', () => { mount(<Button label="Click me" />); cy.get('button').should('be.visible').and('contain.text', 'Click me'); }); });
The test imports React and the mount function from @cypress/react to render the component inside Cypress.
The Button component is imported from the project source.
Inside the test, mount() renders the Button with a label prop.
Then, Cypress uses cy.get('button') to find the button element and asserts it is visible and contains the correct text.
This setup ensures the component renders correctly and is ready for further interaction tests.
Now add data-driven testing with 3 different button labels
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