0
0
Cypresstesting~20 mins

Component test setup in Cypress - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Component Test Setup Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this Cypress component test setup code?
Consider this Cypress test setup for a React component. What will be the test result after running this code?
Cypress
import { mount } from 'cypress/react';

describe('Button component', () => {
  it('renders with correct label', () => {
    mount(<button>Click me</button>);
    cy.get('button').should('have.text', 'Click me');
  });
});
ATest fails because the button text is empty.
BTest passes because the button text matches 'Click me'.
CTest fails due to a syntax error in the mount function.
DTest fails because the selector 'button' does not find any element.
Attempts:
2 left
💡 Hint
Think about what the mount function does and how Cypress queries elements.
locator
intermediate
1:30remaining
Which locator is best for selecting a button in a Cypress component test?
You want to select a button in a component test for Cypress. Which locator is the best practice to use?
Acy.get('button')
Bcy.get('#submit')
Ccy.get('.btn-primary')
Dcy.get('[data-cy="submit-button"]')
Attempts:
2 left
💡 Hint
Consider maintainability and avoiding brittle selectors.
assertion
advanced
2:00remaining
Which assertion correctly verifies a component's input value in Cypress?
You have an input field rendered in a component test. Which assertion correctly checks that the input's value is 'hello'?
Cypress
import { mount } from 'cypress/react';

mount(<input value="hello" />);
Acy.get('input').should('have.value', 'hello')
Bcy.get('input').should('contain.text', 'hello')
Ccy.get('input').should('have.attr', 'value', 'hello')
Dcy.get('input').should('have.text', 'hello')
Attempts:
2 left
💡 Hint
Think about how input values are accessed in Cypress assertions.
🔧 Debug
advanced
2:30remaining
Why does this Cypress component test fail to find the button?
Look at this test code. Why does the test fail with 'Timed out retrying: Expected to find element: button, but never found it'?
Cypress
import { mount } from 'cypress/react';

describe('MyComponent', () => {
  it('should find the button', () => {
    cy.get('button').should('exist');
    mount(<button>Press</button>);
  });
});
AThe test is missing a wait command before cy.get.
BThe selector 'button' is incorrect and does not match the button element.
CThe mount call happens after the cy.get, so the button is not rendered when searched.
DThe mount function is not imported, causing the test to fail.
Attempts:
2 left
💡 Hint
Think about the order of commands and when elements appear in the DOM.
framework
expert
3:00remaining
Which Cypress configuration is required to enable component testing with React?
You want to run Cypress component tests for React components. Which configuration setting is necessary in cypress.config.js?
Acomponent: { devServer: { framework: 'react', bundler: 'webpack' } }
Bcomponent: { supportFile: false }
Ce2e: { baseUrl: 'http://localhost:3000' }
Dcomponent: { testFiles: '**/*.spec.js' }
Attempts:
2 left
💡 Hint
Think about how Cypress knows which framework and bundler to use for component tests.