Introduction
Component testing checks small parts of a user interface by themselves. This helps find problems early and makes fixing easier.
Component testing checks small parts of a user interface by themselves. This helps find problems early and makes fixing easier.
describe('ComponentName', () => { it('should behave as expected', () => { cy.mount(<ComponentName />) // test assertions here }) })
cy.mount() to load the UI component alone in Cypress.describe and it blocks to organize and run them.describe('Button', () => { it('click triggers action', () => { cy.mount(<Button />) cy.get('button').click() cy.get('.message').should('contain', 'Clicked') }) })
describe('InputField', () => { it('shows error on empty submit', () => { cy.mount(<InputField />) cy.get('input').clear() cy.get('form').submit() cy.get('.error').should('be.visible') }) })
This test mounts the Greeting component alone and checks if it shows the right text with the given name.
import React from 'react' function Greeting({ name }) { return <h1>Hello, {name}!</h1> } describe('Greeting component', () => { it('displays the correct greeting message', () => { cy.mount(<Greeting name="Alice" />) cy.get('h1').should('contain.text', 'Hello, Alice!') }) })
Component testing helps find bugs in small parts before they affect the whole app.
It makes tests faster because you don't load the entire page or app.
Use clear and simple selectors like roles or test IDs for stable tests.
Component testing isolates UI parts to test them clearly and quickly.
It helps catch problems early and makes fixing easier.
Cypress uses cy.mount() to load components alone for testing.
cy.mount() better than testing it inside the full app?Pass the test and this pattern moves unknown