Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to mount a React component for testing.
Cypress
import { mount } from 'cypress/react'; import MyComponent from './MyComponent'; describe('MyComponent', () => { it('renders correctly', () => { [1](<MyComponent />); }); });
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'render' instead of 'mount' which is not imported from 'cypress/react'.
Using 'shallow' which is from other testing libraries, not Cypress.
✗ Incorrect
In Cypress component testing, the 'mount' function is used to render the component in the test environment.
2fill in blank
mediumComplete the code to import the component before mounting it.
Cypress
import { mount } from 'cypress/react'; [1] MyComponent from './MyComponent'; describe('MyComponent', () => { it('renders', () => { mount(<MyComponent />); }); });
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'export' which is for sending code out, not bringing it in.
Using 'require' which is CommonJS syntax, not standard in Cypress ES modules.
✗ Incorrect
To use a component in the test file, you must import it using the 'import' keyword.
3fill in blank
hardFix the error in the test setup by completing the missing import statement.
Cypress
import { [1] } from 'cypress/react'; import Button from './Button'; describe('Button', () => { it('should render', () => { mount(<Button />); }); });
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Importing 'render' which is not provided by 'cypress/react'.
Forgetting to import 'mount' causing runtime errors.
✗ Incorrect
The 'mount' function must be imported from 'cypress/react' to render the component in the test.
4fill in blank
hardFill both blanks to correctly set up a component test with a beforeEach hook.
Cypress
import { [1] } from 'cypress/react'; import Header from './Header'; describe('Header', () => { beforeEach(() => { [2](<Header />); }); it('displays title', () => { cy.contains('Welcome').should('be.visible'); }); });
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using different functions for import and call causing errors.
Using 'render' which is not from 'cypress/react'.
✗ Incorrect
The 'mount' function is imported and used to render the component before each test.
5fill in blank
hardFill all three blanks to correctly import, mount, and assert a component renders a button.
Cypress
import { [1] } from 'cypress/react'; import Button from './Button'; describe('Button', () => { it('renders button', () => { [2](<Button />); cy.get('button').should([3]); }); });
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'render' instead of 'mount' for import or call.
Using 'exist' which checks presence but not visibility.
✗ Incorrect
Import and mount use 'mount', and the assertion checks the button is visible with 'be.visible'.