Challenge - 5 Problems
React Mounting Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of Mounting a Simple React Component
What will be the output in the Cypress test runner when this React component is mounted and checked for text content?
Cypress
import React from 'react'; import { mount } from 'cypress/react'; function Greeting() { return <h1>Hello, Cypress!</h1>; } describe('Greeting component', () => { it('renders correct text', () => { mount(<Greeting />); cy.contains('Hello, Cypress!').should('exist'); }); });
Attempts:
2 left
💡 Hint
Check what text the component renders and what text the test looks for.
✗ Incorrect
The component renders 'Hello, Cypress!' inside an h1 tag. The test mounts the component and checks for that exact text, so it passes.
❓ locator
intermediate2:00remaining
Best Locator for a Button in Mounted React Component
You have mounted a React component with a button: . Which Cypress selector is the best practice to locate this button for testing?
Attempts:
2 left
💡 Hint
Use selectors that improve accessibility and are less likely to break.
✗ Incorrect
Using the aria-label attribute is best practice for accessibility and stable selectors. It is more reliable than text or position-based selectors.
❓ assertion
advanced2:00remaining
Correct Assertion for React Component State Change
After mounting a React component with a button that toggles text from 'Off' to 'On', which assertion correctly verifies the text changes after clicking?
Cypress
import React, { useState } from 'react'; import { mount } from 'cypress/react'; function Toggle() { const [on, setOn] = useState(false); return <button onClick={() => setOn(!on)}>{on ? 'On' : 'Off'}</button>; } describe('Toggle component', () => { it('toggles text on click', () => { mount(<Toggle />); cy.get('button').click(); // Which assertion is correct here? }); });
Attempts:
2 left
💡 Hint
Check the correct Cypress assertion for button text content.
✗ Incorrect
The 'have.text' assertion checks the exact text content of the button. 'contain' is less strict but also works; however, 'have.value' and 'have.attr' are incorrect for button text.
🔧 Debug
advanced2:00remaining
Debugging Mount Failure in Cypress React Test
You try to mount a React component in Cypress but get the error: "TypeError: mount is not a function". What is the most likely cause?
Cypress
import React from 'react'; import { mount } from 'cypress/react'; function Sample() { return <div>Sample</div>; } describe('Sample test', () => { it('mounts component', () => { mount(<Sample />); }); });
Attempts:
2 left
💡 Hint
Check if the testing environment has the right packages installed.
✗ Incorrect
The error means Cypress cannot find the mount function. This usually happens if the cypress/react package is missing or not set up properly.
❓ framework
expert3:00remaining
Handling Asynchronous Effects in Mounted React Components
You mount a React component that fetches data asynchronously on mount and displays it. Which Cypress approach correctly waits for the data to appear before asserting?
Cypress
import React, { useEffect, useState } from 'react'; import { mount } from 'cypress/react'; function DataFetcher() { const [data, setData] = useState(null); useEffect(() => { setTimeout(() => setData('Loaded'), 1000); }, []); return <div>{data ?? 'Loading...'}</div>; } describe('DataFetcher component', () => { it('shows loaded data', () => { mount(<DataFetcher />); // Which option waits correctly for data? }); });
Attempts:
2 left
💡 Hint
Use Cypress commands that automatically retry until the condition is met.
✗ Incorrect
cy.contains('Loaded').should('exist') retries until the text appears, handling async updates gracefully. cy.wait(1000) is brittle and not recommended. Other options may fail if text changes timing varies.