{
  "doc": {
    "id": "app/write-tests/component-testing/react/examples",
    "title": "React examples",
    "description": "Learn how to mount a React component, pass data to a React component, test event handlers, and customize cy.mount() for React Router and Redux.",
    "section": "app",
    "source_path": "/llm/markdown/app/write-tests/component-testing/react/examples.md",
    "version": "066c46e056f0f322a0670d2d3aa4e6adaebfe717",
    "updated_at": "2026-09-10T13:30:12.426Z",
    "headings": [
      {
        "id": "app/write-tests/component-testing/react/examples#react-examples",
        "text": "React Examples",
        "level": 1
      },
      {
        "id": "app/write-tests/component-testing/react/examples#passing-data-to-a-component",
        "text": "Passing Data to a Component",
        "level": 2
      },
      {
        "id": "app/write-tests/component-testing/react/examples#testing-event-handlers",
        "text": "Testing Event Handlers",
        "level": 2
      },
      {
        "id": "app/write-tests/component-testing/react/examples#testing-error-states",
        "text": "Testing Error States",
        "level": 2
      },
      {
        "id": "app/write-tests/component-testing/react/examples#testing-error-boundaries",
        "text": "Testing Error Boundaries",
        "level": 3
      },
      {
        "id": "app/write-tests/component-testing/react/examples#custom-mount-commands",
        "text": "Custom Mount Commands",
        "level": 2
      },
      {
        "id": "app/write-tests/component-testing/react/examples#customizing-cy-mount",
        "text": "Customizing cy.mount()",
        "level": 3
      },
      {
        "id": "app/write-tests/component-testing/react/examples#react-router",
        "text": "React Router",
        "level": 3
      },
      {
        "id": "app/write-tests/component-testing/react/examples#redux",
        "text": "Redux",
        "level": 3
      }
    ]
  },
  "content": {
    "type": "root",
    "children": [
      {
        "type": "heading",
        "depth": 1,
        "children": [
          {
            "type": "text",
            "value": "React Examples"
          }
        ]
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "The first step in testing a component is to mount it. This renders the component into a testbed and enable's the use of the Cypress API to select elements, interact with them, and run assertions."
          }
        ]
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "To mount a React component, import the component into your spec and pass the component to the `cy.mount` command:"
          }
        ]
      },
      {
        "type": "code",
        "lang": null,
        "meta": null,
        "value": "import { Stepper } from './stepper'\n\nit('mounts', () => {\n  cy.mount(<Stepper />)\n  //Stepper should have initial count of 0 (default)\n  cy.get('[data-cy=counter]').should('have.text', '0')\n})"
      },
      {
        "type": "heading",
        "depth": 2,
        "children": [
          {
            "type": "text",
            "value": "Passing Data to a Component"
          }
        ]
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "You can pass props to a component by setting them on the JSX passed into `cy.mount()`:"
          }
        ]
      },
      {
        "type": "code",
        "lang": null,
        "meta": null,
        "value": "it('mounts', () => {\n  cy.mount(<Stepper initial={100} />)\n  //Stepper should have initial count of 100\n  cy.get('[data-cy=counter]').should('have.text', '100')\n})"
      },
      {
        "type": "heading",
        "depth": 2,
        "children": [
          {
            "type": "text",
            "value": "Testing Event Handlers"
          }
        ]
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "Pass a Cypress "
          },
          {
            "type": "link",
            "title": null,
            "url": "/llm/markdown/app/guides/stubs-spies-and-clocks.md#Spies",
            "children": [
              {
                "type": "text",
                "value": "spy"
              }
            ]
          },
          {
            "type": "text",
            "value": " to an event prop and validate it was called:"
          }
        ]
      },
      {
        "type": "code",
        "lang": null,
        "meta": null,
        "value": "it('clicking + fires a change event with the incremented value', () => {\n  const onChangeSpy = cy.spy().as('onChangeSpy')\n  cy.mount(<Stepper onChange={onChangeSpy} />)\n  cy.get('[data-cy=increment]').click()\n  cy.get('@onChangeSpy').should('have.been.calledWith', 1)\n})"
      },
      {
        "type": "heading",
        "depth": 2,
        "children": [
          {
            "type": "text",
            "value": "Testing Error States"
          }
        ]
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "`cy.mount()` is an asynchronous Cypress command: it enqueues the mount and returns immediately, before the component ever renders. When a component throws during render, the error surfaces as an uncaught exception rather than as a synchronous throw."
          }
        ]
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "By default Cypress fails the test on any uncaught exception, so to assert on a render error you listen for it with "
          },
          {
            "type": "link",
            "title": null,
            "url": "/llm/markdown/api/cypress-api/catalog-of-events.md#Uncaught-Exceptions",
            "children": [
              {
                "type": "text",
                "value": "`cy.on('uncaught:exception')`"
              }
            ]
          },
          {
            "type": "text",
            "value": " and return `false` to prevent Cypress from failing the test:"
          }
        ]
      },
      {
        "type": "code",
        "lang": null,
        "meta": null,
        "value": "import { UserProfile } from './UserProfile'\n\nit('surfaces a render error', () => {\n  cy.on('uncaught:exception', (err) => {\n    // Assert on the error thrown during render...\n    expect(err.message).to.include('user is required')\n\n    // ...and return false so Cypress does not fail the test.\n    return false\n  })\n\n  cy.mount(<UserProfile />)\n})"
      },
      {
        "type": "heading",
        "depth": 3,
        "children": [
          {
            "type": "text",
            "value": "Testing Error Boundaries"
          }
        ]
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "The recommended way to handle render errors in React is an "
          },
          {
            "type": "link",
            "title": null,
            "url": "https://react.dev/reference/react/Component#catching-rendering-errors-with-an-error-boundary",
            "children": [
              {
                "type": "text",
                "value": "Error Boundary"
              }
            ]
          },
          {
            "type": "text",
            "value": ", which renders fallback UI via `getDerivedStateFromError()`. You can mount a component wrapped in your Error Boundary and assert that the fallback UI is displayed:"
          }
        ]
      },
      {
        "type": "code",
        "lang": null,
        "meta": null,
        "value": "// ErrorBoundary.cy.jsx\nimport { ErrorBoundary } from './ErrorBoundary'\n\nconst errorMessage = 'I crashed!'\nconst ChildWithError = () => {\n  throw new Error(errorMessage)\n}\n\nit('displays the fallback UI on error', () => {\n  // An Error Boundary renders fallback UI, but it does NOT stop the error\n  // from propagating to Cypress as an uncaught exception. Cypress fails on\n  // uncaught exceptions by default, so we must still suppress that behavior.\n  cy.on('uncaught:exception', (err) => {\n    // Only suppress the specific error we expect.\n    expect(err.message).to.include(errorMessage)\n\n    return false\n  })\n\n  cy.mount(\n    <ErrorBoundary name=\"ChildWithError\">\n      <ChildWithError />\n    </ErrorBoundary>\n  )\n\n  cy.get('header h1').should('contain', 'Something went wrong.')\n})"
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "Where `ErrorBoundary` renders fallback UI from `getDerivedStateFromError()`:"
          }
        ]
      },
      {
        "type": "code",
        "lang": null,
        "meta": null,
        "value": "// ErrorBoundary.jsx\nimport React from 'react'\n\nexport class ErrorBoundary extends React.Component {\n  constructor(props) {\n    super(props)\n    this.state = { error: null }\n  }\n\n  static getDerivedStateFromError(error) {\n    return { error }\n  }\n\n  render() {\n    const { name } = this.props\n    const { error } = this.state\n\n    if (error) {\n      return (\n        <header>\n          <h1>Something went wrong.</h1>\n          <h2>{`${name} failed to load`}</h2>\n        </header>\n      )\n    }\n\n    return this.props.children\n  }\n}"
      },
      {
        "type": "heading",
        "depth": 2,
        "children": [
          {
            "type": "text",
            "value": "Custom Mount Commands"
          }
        ]
      },
      {
        "type": "heading",
        "depth": 3,
        "children": [
          {
            "type": "text",
            "value": "Customizing `cy.mount()`"
          }
        ]
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "By default, `cy.mount()` is a simple passthrough to `mount()`, however, you can customize `cy.mount()` to fit your needs. For instance, if you are using providers or other global app-level setups in your React app, you can configure them here."
          }
        ]
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "Below are a few examples that demonstrate using a custom mount command. These examples can be adjusted for most other providers that you will need to support."
          }
        ]
      },
      {
        "type": "heading",
        "depth": 3,
        "children": [
          {
            "type": "text",
            "value": "React Router"
          }
        ]
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "If you have a component that consumes a hook or component from "
          },
          {
            "type": "link",
            "title": null,
            "url": "https://reactrouter.com/",
            "children": [
              {
                "type": "text",
                "value": "React Router"
              }
            ]
          },
          {
            "type": "text",
            "value": ", make sure the component has access to a React Router provider. Below is a sample mount command that uses `MemoryRouter` to wrap the component."
          }
        ]
      },
      {
        "type": "list",
        "ordered": false,
        "start": null,
        "spread": false,
        "children": [
          {
            "type": "listItem",
            "spread": false,
            "checked": null,
            "children": [
              {
                "type": "paragraph",
                "children": [
                  {
                    "type": "text",
                    "value": "cypress/support/component.jsx"
                  }
                ]
              }
            ]
          },
          {
            "type": "listItem",
            "spread": false,
            "checked": null,
            "children": [
              {
                "type": "paragraph",
                "children": [
                  {
                    "type": "text",
                    "value": "cypress/support/component.tsx"
                  }
                ]
              }
            ]
          }
        ]
      },
      {
        "type": "code",
        "lang": null,
        "meta": null,
        "value": "import { mount } from 'cypress/react'\nimport { MemoryRouter } from 'react-router-dom'\n\nCypress.Commands.add('mount', (component, options = {}) => {\n  const { routerProps = { initialEntries: ['/'] }, ...mountOptions } = options\n\n  const wrapped = <MemoryRouter {...routerProps}>{component}</MemoryRouter>\n\n  return mount(wrapped, mountOptions)\n})"
      },
      {
        "type": "code",
        "lang": null,
        "meta": null,
        "value": "import { mount, MountOptions, MountReturn } from 'cypress/react'\nimport { MemoryRouter, MemoryRouterProps } from 'react-router-dom'\n\ndeclare global {\n  namespace Cypress {\n    interface Chainable {\n      /**\n       * Mounts a React node\n       * @param component React Node to mount\n       * @param options Additional options to pass into mount\n       */\n      mount(\n        component: React.ReactNode,\n        options?: MountOptions & { routerProps?: MemoryRouterProps }\n      ): Cypress.Chainable<MountReturn>\n    }\n  }\n}\n\nCypress.Commands.add('mount', (component, options = {}) => {\n  const { routerProps = { initialEntries: ['/'] }, ...mountOptions } = options\n\n  const wrapped = <MemoryRouter {...routerProps}>{component}</MemoryRouter>\n\n  return mount(wrapped, mountOptions)\n})"
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "To set up certain scenarios, pass in props that will get passed to `MemoryRouter` in the options. Below is an example test that ensures an active link has the correct class applied to it by initializing the router with `initialEntries` pointed to a particular route:"
          }
        ]
      },
      {
        "type": "code",
        "lang": null,
        "meta": null,
        "value": "import { Navigation } from './Navigation'\n\nit('home link should be active when url is \"/\"', () => {\n  // No need to pass in custom initialEntries as default url is '/'\n  cy.mount(<Navigation />)\n\n  cy.get('a').contains('Home').should('have.class', 'active')\n})\n\nit('login link should be active when url is \"/login\"', () => {\n  cy.mount(<Navigation />, {\n    routerProps: {\n      initialEntries: ['/login'],\n    },\n  })\n\n  cy.get('a').contains('Login').should('have.class', 'active')\n})"
      },
      {
        "type": "heading",
        "depth": 3,
        "children": [
          {
            "type": "text",
            "value": "Redux"
          }
        ]
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "To use a component that consumes state or actions from a "
          },
          {
            "type": "link",
            "title": null,
            "url": "https://react-redux.js.org/",
            "children": [
              {
                "type": "text",
                "value": "Redux"
              }
            ]
          },
          {
            "type": "text",
            "value": " store, create a `mount` command that will wrap your component in a Redux Provider:"
          }
        ]
      },
      {
        "type": "list",
        "ordered": false,
        "start": null,
        "spread": false,
        "children": [
          {
            "type": "listItem",
            "spread": false,
            "checked": null,
            "children": [
              {
                "type": "paragraph",
                "children": [
                  {
                    "type": "text",
                    "value": "cypress/support/component.jsx"
                  }
                ]
              }
            ]
          },
          {
            "type": "listItem",
            "spread": false,
            "checked": null,
            "children": [
              {
                "type": "paragraph",
                "children": [
                  {
                    "type": "text",
                    "value": "cypress/support/component.tsx"
                  }
                ]
              }
            ]
          }
        ]
      },
      {
        "type": "code",
        "lang": null,
        "meta": null,
        "value": "import { mount } from 'cypress/react'\nimport { Provider } from 'react-redux'\nimport { getStore } from '../../src/store'\n\nCypress.Commands.add('mount', (component, options = {}) => {\n  // Use the default store if one is not provided\n  const { reduxStore = getStore(), ...mountOptions } = options\n\n  const wrapped = <Provider store={reduxStore}>{component}</Provider>\n\n  return mount(wrapped, mountOptions)\n})"
      },
      {
        "type": "code",
        "lang": null,
        "meta": null,
        "value": "import { mount, MountOptions, MountReturn } from 'cypress/react'\nimport { Provider } from 'react-redux'\nimport { EnhancedStore } from '@reduxjs/toolkit'\nimport { getStore } from '../../src/store'\nimport { RootState } from './src/StoreState'\n\ndeclare global {\n  namespace Cypress {\n    interface Chainable {\n      /**\n       * Mounts a React node\n       * @param component React Node to mount\n       * @param options Additional options to pass into mount\n       */\n      mount(\n        component: React.ReactNode,\n        options?: MountOptions & { reduxStore?: EnhancedStore<RootState> }\n      ): Cypress.Chainable<MountReturn>\n    }\n  }\n}\n\nCypress.Commands.add('mount', (component, options = {}) => {\n  const { reduxStore = getStore(), ...mountOptions } = options\n\n  const wrapped = <Provider store={reduxStore}>{component}</Provider>\n\n  return mount(wrapped, mountOptions)\n})"
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "The options param can have a store that is already initialized with data:"
          }
        ]
      },
      {
        "type": "code",
        "lang": null,
        "meta": null,
        "value": "import { getStore } from '../redux/store'\nimport { setUser } from '../redux/userSlice'\nimport { UserProfile } from './UserProfile'\n\nit('User profile should display user name', () => {\n  const user = { name: 'test person' }\n\n  // getStore is a factory method that creates a new store\n  const store = getStore()\n\n  // setUser is an action exported from the user slice\n  store.dispatch(setUser(user))\n\n  cy.mount(<UserProfile />, { reduxStore: store })\n\n  cy.get('div.name').should('have.text', user.name)\n})"
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "The `getStore` method is a factory method that initializes a new Redux store. It is important that the store be initialized with each new test to ensure changes to the store don't affect other tests."
          }
        ]
      }
    ]
  },
  "token_estimate": 1565
}