Setting up component tests helps you check small parts of your app work right before combining them. It saves time and finds bugs early.
Component test setup in Cypress
Introduction
Syntax
Cypress
import { mount } from 'cypress/react18'; describe('ComponentName', () => { beforeEach(() => { mount(<ComponentName />); }); it('does something', () => { // test code here }); });
Use mount() to load the component inside Cypress for testing.
beforeEach() runs before every test to set up the component fresh.
Examples
Cypress
import { mount } from 'cypress/react18'; import Button from './Button'; describe('Button component', () => { beforeEach(() => { mount(<Button label="Click me" />); }); it('shows the correct label', () => { cy.contains('Click me').should('exist'); }); });
Cypress
import { mount } from 'cypress/react18'; import Input from './Input'; describe('Input component', () => { beforeEach(() => { mount(<Input placeholder="Enter name" />); }); it('accepts user input', () => { cy.get('input').type('Alice').should('have.value', 'Alice'); }); });
Sample Program
This test mounts a Greeting component with a name prop and checks if the greeting text shows correctly.
Cypress
import { mount } from 'cypress/react18'; import Greeting from './Greeting'; describe('Greeting component', () => { beforeEach(() => { mount(<Greeting name="Bob" />); }); it('displays the greeting message', () => { cy.contains('Hello, Bob!').should('exist'); }); });
Important Notes
Always mount your component fresh in beforeEach() to avoid test interference.
Use clear and simple selectors like text or role for better test reliability.
Component tests run faster than full app tests because they load less code.
Summary
Component test setup uses mount() to load a component inside Cypress.
Use beforeEach() to prepare the component before each test.
This helps test small parts quickly and catch bugs early.
Practice
1. What is the main purpose of using
mount() in Cypress component testing?easy
Solution
Step 1: Understand the role of
mount()mount()is used to load a UI component inside Cypress so you can test it in isolation.Step 2: Differentiate from other test types
End-to-end tests run the whole app, butmount()focuses on small parts (components).Final Answer:
To load a component inside Cypress for testing -> Option CQuick Check:
mount() loads component [OK]
Hint: Remember: mount() loads components, not full apps [OK]
Common Mistakes:
- Confusing mount() with full app testing
- Thinking mount() deploys code
- Mixing component and API testing
2. Which of the following is the correct syntax to prepare a component before each test in Cypress?
easy
Solution
Step 1: Identify the correct hook for setup before each test
beforeEach()runs before every test, ideal for fresh component setup.Step 2: Confirm syntax correctness
The syntaxbeforeEach(() => { mount(<MyComponent />) })correctly mounts the component before each test.Final Answer:
beforeEach(() => { mount(<MyComponent />) }) -> Option AQuick Check:
Use beforeEach() for setup [OK]
Hint: Use beforeEach() to reset component before every test [OK]
Common Mistakes:
- Using beforeAll() which runs only once
- Using afterEach() which runs after tests
- Using before() which is deprecated in Cypress
3. Given this Cypress component test code, what will be the output in the test runner?
describe('Button component', () => {
beforeEach(() => {
mount(Click me)
})
it('checks button text', () => {
cy.get('button').should('have.text', 'Click me')
})
})medium
Solution
Step 1: Analyze the mount and selector
Themount()loads a button with text 'Click me'. The selectorcy.get('button')correctly targets it.Step 2: Check the assertion
The assertionshould('have.text', 'Click me')matches the button's text exactly, so it passes.Final Answer:
Test passes because button text matches -> Option AQuick Check:
Correct selector and text = pass [OK]
Hint: Match selector text exactly for passing assertion [OK]
Common Mistakes:
- Using wrong selector causing no element found
- Mismatching text in assertion
- Forgetting to mount component before test
4. Identify the error in this Cypress component test setup:
describe('Input component', () => {
beforeEach(() => {
mount('')
})
it('checks placeholder', () => {
cy.get('input').should('have.attr', 'placeholder', 'Enter name')
})
})medium
Solution
Step 1: Check the argument passed to mount()
The code passes a string '<input placeholder="Enter name" />' instead of JSX element. mount() expects JSX, not a string.Step 2: Confirm other parts are correct
The selectorcy.get('input')and attribute check are correct, so no error there.Final Answer:
mount() should receive JSX, not a string -> Option BQuick Check:
mount() needs JSX, not string [OK]
Hint: Pass JSX, not strings, to mount() [OK]
Common Mistakes:
- Passing HTML strings instead of JSX to mount()
- Confusing beforeEach() with afterEach()
- Using wrong selectors
5. You want to test a component that renders a list of items passed as props. Which setup correctly mounts the component with props before each test?
hard
Solution
Step 1: Understand correct JSX with props
Passing props in JSX uses syntax like<List items={["a", "b"]} />. This is correct in beforeEach(() => { mount(<List items={["a", "b"]} />) }).Step 2: Identify incorrect options
beforeEach(() => { mount('<List items=["a", "b"] />') }) passes a string, not JSX. beforeEach(() => { mount(List, { props: { items: ["a", "b"] } }) }) uses wrong mount signature. beforeEach(() => { mount(<List items="a,b" />) }) passes a string instead of array.Final Answer:
beforeEach(() => { mount(<List items={["a", "b"]} />) }) -> Option DQuick Check:
Use JSX with array props [OK]
Hint: Pass props as JSX arrays, not strings [OK]
Common Mistakes:
- Passing component as string instead of JSX
- Using wrong mount function signature
- Passing props as comma-separated string
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
