{
  "doc": {
    "id": "app/component-testing/get-started",
    "title": "Component Testing in Cypress | Cypress Documentation",
    "description": "Write Cypress component tests in React, Angular, Vue, or Svelte. Learn how to set up, write, and run your first component test.",
    "section": "app",
    "source_path": "/llm/markdown/app/component-testing/get-started.md",
    "version": "ce02913654e2655ee63448bdc92bb92c7b46a619",
    "updated_at": "2026-04-22T19:37:51.587Z",
    "headings": [
      {
        "id": "app/component-testing/get-started#cypress-component-testing",
        "text": "Cypress Component Testing",
        "level": 1
      },
      {
        "id": "app/component-testing/get-started#what-youll-learn",
        "text": " What you'll learn",
        "level": 5
      },
      {
        "id": "app/component-testing/get-started#configuring-component-testing",
        "text": "Configuring Component Testing",
        "level": 2
      },
      {
        "id": "app/component-testing/get-started#supported-frameworks",
        "text": "Supported Frameworks",
        "level": 3
      },
      {
        "id": "app/component-testing/get-started#select-testing-type",
        "text": "Select Testing Type",
        "level": 3
      },
      {
        "id": "app/component-testing/get-started#project-setup",
        "text": "Project Setup",
        "level": 3
      },
      {
        "id": "app/component-testing/get-started#install-dependencies",
        "text": "Install Dependencies",
        "level": 3
      },
      {
        "id": "app/component-testing/get-started#config-files",
        "text": "Config Files",
        "level": 3
      },
      {
        "id": "app/component-testing/get-started#choose-a-browser",
        "text": "Choose A Browser",
        "level": 3
      },
      {
        "id": "app/component-testing/get-started#writing-your-first-test",
        "text": "Writing Your First Test",
        "level": 2
      },
      {
        "id": "app/component-testing/get-started#frameworks",
        "text": "Frameworks",
        "level": 3
      },
      {
        "id": "app/component-testing/get-started#your-first-component-test",
        "text": "Your First Component Test",
        "level": 3
      },
      {
        "id": "app/component-testing/get-started#test-explanation",
        "text": "Test Explanation",
        "level": 3
      },
      {
        "id": "app/component-testing/get-started#running-the-test",
        "text": "Running the Test",
        "level": 2
      },
      {
        "id": "app/component-testing/get-started#selectors-assertions",
        "text": "Selectors & Assertions",
        "level": 3
      },
      {
        "id": "app/component-testing/get-started#passing-props-to-components",
        "text": "Passing Props to Components",
        "level": 3
      },
      {
        "id": "app/component-testing/get-started#testing-interactions",
        "text": "Testing Interactions",
        "level": 3
      },
      {
        "id": "app/component-testing/get-started#testing-components-with-events",
        "text": "Testing Components with Events",
        "level": 2
      },
      {
        "id": "app/component-testing/get-started#using-spies",
        "text": "Using Spies",
        "level": 3
      },
      {
        "id": "app/component-testing/get-started#whats-next",
        "text": "What's Next?",
        "level": 2
      }
    ]
  },
  "content": {
    "type": "root",
    "children": [
      {
        "type": "heading",
        "depth": 1,
        "children": [
          {
            "type": "text",
            "value": "Cypress Component Testing"
          }
        ]
      },
      {
        "type": "heading",
        "depth": 5,
        "children": [
          {
            "type": "text",
            "value": " What you'll learn"
          }
        ]
      },
      {
        "type": "list",
        "ordered": false,
        "start": null,
        "spread": false,
        "children": [
          {
            "type": "listItem",
            "spread": false,
            "checked": null,
            "children": [
              {
                "type": "paragraph",
                "children": [
                  {
                    "type": "text",
                    "value": "How to set up Component Testing for React, Angular, Vue, or Svelte"
                  }
                ]
              }
            ]
          },
          {
            "type": "listItem",
            "spread": false,
            "checked": null,
            "children": [
              {
                "type": "paragraph",
                "children": [
                  {
                    "type": "text",
                    "value": "How to write your first component test"
                  }
                ]
              }
            ]
          },
          {
            "type": "listItem",
            "spread": false,
            "checked": null,
            "children": [
              {
                "type": "paragraph",
                "children": [
                  {
                    "type": "text",
                    "value": "How to run your component test\n:::"
                  }
                ]
              }
            ]
          }
        ]
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "Assuming you've successfully\n"
          },
          {
            "type": "link",
            "title": null,
            "url": "/llm/markdown/app/get-started/install-cypress.md",
            "children": [
              {
                "type": "text",
                "value": "installed Cypress"
              }
            ]
          },
          {
            "type": "text",
            "value": " and\n"
          },
          {
            "type": "link",
            "title": null,
            "url": "/llm/markdown/app/get-started/open-the-app.md",
            "children": [
              {
                "type": "text",
                "value": "opened Cypress"
              }
            ]
          },
          {
            "type": "text",
            "value": ", now it's time to setup component testing."
          }
        ]
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "The Cypress App will guide you through configuring your project."
          }
        ]
      },
      {
        "type": "heading",
        "depth": 2,
        "children": [
          {
            "type": "text",
            "value": "Configuring Component Testing"
          }
        ]
      },
      {
        "type": "heading",
        "depth": 3,
        "children": [
          {
            "type": "text",
            "value": "Supported Frameworks"
          }
        ]
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "Cypress currently has official mounting libraries for\n"
          },
          {
            "type": "link",
            "title": null,
            "url": "/llm/markdown/app/component-testing/react/overview.md",
            "children": [
              {
                "type": "text",
                "value": "React"
              }
            ]
          },
          {
            "type": "text",
            "value": ",\n"
          },
          {
            "type": "link",
            "title": null,
            "url": "/llm/markdown/app/component-testing/angular/overview.md",
            "children": [
              {
                "type": "text",
                "value": "Angular"
              }
            ]
          },
          {
            "type": "text",
            "value": ",\n"
          },
          {
            "type": "link",
            "title": null,
            "url": "/llm/markdown/app/component-testing/vue/overview.md",
            "children": [
              {
                "type": "text",
                "value": "Vue"
              }
            ]
          },
          {
            "type": "text",
            "value": ", and\n"
          },
          {
            "type": "link",
            "title": null,
            "url": "/llm/markdown/app/component-testing/svelte/overview.md",
            "children": [
              {
                "type": "text",
                "value": "Svelte"
              }
            ]
          },
          {
            "type": "text",
            "value": " and support for the\nfollowing development servers and frameworks:"
          }
        ]
      },
      {
        "type": "table",
        "align": [
          null,
          null,
          null
        ],
        "children": [
          {
            "type": "tableRow",
            "children": [
              {
                "type": "tableCell",
                "children": [
                  {
                    "type": "text",
                    "value": "Framework"
                  }
                ]
              },
              {
                "type": "tableCell",
                "children": [
                  {
                    "type": "text",
                    "value": "UI Library"
                  }
                ]
              },
              {
                "type": "tableCell",
                "children": [
                  {
                    "type": "text",
                    "value": "Bundler"
                  }
                ]
              }
            ]
          },
          {
            "type": "tableRow",
            "children": [
              {
                "type": "tableCell",
                "children": [
                  {
                    "type": "link",
                    "title": null,
                    "url": "/llm/markdown/app/component-testing/react/overview.md#React-with-Vite",
                    "children": [
                      {
                        "type": "text",
                        "value": "React with Vite"
                      }
                    ]
                  }
                ]
              },
              {
                "type": "tableCell",
                "children": [
                  {
                    "type": "text",
                    "value": "React 18-19"
                  }
                ]
              },
              {
                "type": "tableCell",
                "children": [
                  {
                    "type": "text",
                    "value": "Vite 5-8"
                  }
                ]
              }
            ]
          },
          {
            "type": "tableRow",
            "children": [
              {
                "type": "tableCell",
                "children": [
                  {
                    "type": "link",
                    "title": null,
                    "url": "/llm/markdown/app/component-testing/react/overview.md#React-with-Webpack",
                    "children": [
                      {
                        "type": "text",
                        "value": "React with Webpack"
                      }
                    ]
                  }
                ]
              },
              {
                "type": "tableCell",
                "children": [
                  {
                    "type": "text",
                    "value": "React 18-19"
                  }
                ]
              },
              {
                "type": "tableCell",
                "children": [
                  {
                    "type": "text",
                    "value": "Webpack 5"
                  }
                ]
              }
            ]
          },
          {
            "type": "tableRow",
            "children": [
              {
                "type": "tableCell",
                "children": [
                  {
                    "type": "link",
                    "title": null,
                    "url": "/llm/markdown/app/component-testing/react/overview.md#Nextjs",
                    "children": [
                      {
                        "type": "text",
                        "value": "Next.js 14-16"
                      }
                    ]
                  }
                ]
              },
              {
                "type": "tableCell",
                "children": [
                  {
                    "type": "text",
                    "value": "React 18-19"
                  }
                ]
              },
              {
                "type": "tableCell",
                "children": [
                  {
                    "type": "text",
                    "value": "Webpack 5"
                  }
                ]
              }
            ]
          },
          {
            "type": "tableRow",
            "children": [
              {
                "type": "tableCell",
                "children": [
                  {
                    "type": "link",
                    "title": null,
                    "url": "/llm/markdown/app/component-testing/vue/overview.md#Vue-with-Vite",
                    "children": [
                      {
                        "type": "text",
                        "value": "Vue with Vite"
                      }
                    ]
                  }
                ]
              },
              {
                "type": "tableCell",
                "children": [
                  {
                    "type": "text",
                    "value": "Vue 3"
                  }
                ]
              },
              {
                "type": "tableCell",
                "children": [
                  {
                    "type": "text",
                    "value": "Vite 5-8"
                  }
                ]
              }
            ]
          },
          {
            "type": "tableRow",
            "children": [
              {
                "type": "tableCell",
                "children": [
                  {
                    "type": "link",
                    "title": null,
                    "url": "/llm/markdown/app/component-testing/vue/overview.md#Vue-with-Webpack",
                    "children": [
                      {
                        "type": "text",
                        "value": "Vue with Webpack"
                      }
                    ]
                  }
                ]
              },
              {
                "type": "tableCell",
                "children": [
                  {
                    "type": "text",
                    "value": "Vue 3"
                  }
                ]
              },
              {
                "type": "tableCell",
                "children": [
                  {
                    "type": "text",
                    "value": "Webpack 5"
                  }
                ]
              }
            ]
          },
          {
            "type": "tableRow",
            "children": [
              {
                "type": "tableCell",
                "children": [
                  {
                    "type": "link",
                    "title": null,
                    "url": "/llm/markdown/app/component-testing/angular/overview.md#Framework-Configuration",
                    "children": [
                      {
                        "type": "text",
                        "value": "Angular"
                      }
                    ]
                  }
                ]
              },
              {
                "type": "tableCell",
                "children": [
                  {
                    "type": "text",
                    "value": "Angular 18-21"
                  }
                ]
              },
              {
                "type": "tableCell",
                "children": [
                  {
                    "type": "text",
                    "value": "Webpack 5"
                  }
                ]
              }
            ]
          },
          {
            "type": "tableRow",
            "children": [
              {
                "type": "tableCell",
                "children": [
                  {
                    "type": "link",
                    "title": null,
                    "url": "/llm/markdown/app/component-testing/svelte/overview.md#Svelte-with-Vite",
                    "children": [
                      {
                        "type": "text",
                        "value": "Svelte with Vite"
                      }
                    ]
                  }
                ]
              },
              {
                "type": "tableCell",
                "children": [
                  {
                    "type": "text",
                    "value": "Svelte 5"
                  }
                ]
              },
              {
                "type": "tableCell",
                "children": [
                  {
                    "type": "text",
                    "value": "Vite 5-8"
                  }
                ]
              }
            ]
          },
          {
            "type": "tableRow",
            "children": [
              {
                "type": "tableCell",
                "children": [
                  {
                    "type": "link",
                    "title": null,
                    "url": "/llm/markdown/app/component-testing/svelte/overview.md#Svelte-with-Webpack",
                    "children": [
                      {
                        "type": "text",
                        "value": "Svelte with Webpack"
                      }
                    ]
                  }
                ]
              },
              {
                "type": "tableCell",
                "children": [
                  {
                    "type": "text",
                    "value": "Svelte 5"
                  }
                ]
              },
              {
                "type": "tableCell",
                "children": [
                  {
                    "type": "text",
                    "value": "Webpack 5"
                  }
                ]
              }
            ]
          }
        ]
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "The following integrations are built and maintained by Cypress community members."
          }
        ]
      },
      {
        "type": "table",
        "align": [
          null,
          null,
          null
        ],
        "children": [
          {
            "type": "tableRow",
            "children": [
              {
                "type": "tableCell",
                "children": [
                  {
                    "type": "text",
                    "value": "Framework"
                  }
                ]
              },
              {
                "type": "tableCell",
                "children": [
                  {
                    "type": "text",
                    "value": "UI Library"
                  }
                ]
              },
              {
                "type": "tableCell",
                "children": [
                  {
                    "type": "text",
                    "value": "Bundler"
                  }
                ]
              }
            ]
          },
          {
            "type": "tableRow",
            "children": [
              {
                "type": "tableCell",
                "children": [
                  {
                    "type": "link",
                    "title": null,
                    "url": "https://github.com/qwikifiers/cypress-qwik",
                    "children": [
                      {
                        "type": "text",
                        "value": "Qwik"
                      }
                    ]
                  }
                ]
              },
              {
                "type": "tableCell",
                "children": [
                  {
                    "type": "text",
                    "value": "Qwik"
                  }
                ]
              },
              {
                "type": "tableCell",
                "children": [
                  {
                    "type": "text",
                    "value": "Vite"
                  }
                ]
              }
            ]
          },
          {
            "type": "tableRow",
            "children": [
              {
                "type": "tableCell",
                "children": [
                  {
                    "type": "link",
                    "title": null,
                    "url": "https://github.com/redfox-mx/cypress-lit",
                    "children": [
                      {
                        "type": "text",
                        "value": "Lit"
                      }
                    ]
                  }
                ]
              },
              {
                "type": "tableCell",
                "children": [
                  {
                    "type": "text",
                    "value": "Lit"
                  }
                ]
              },
              {
                "type": "tableCell",
                "children": [
                  {
                    "type": "text",
                    "value": "Vite"
                  }
                ]
              }
            ]
          }
        ]
      },
      {
        "type": "heading",
        "depth": 3,
        "children": [
          {
            "type": "text",
            "value": "Select Testing Type"
          }
        ]
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "Whenever you run Cypress for the first time, the app will prompt you to set up\neither E2E Testing or Component Testing. Click on \"Component Testing\" to start\nthe configuration wizard."
          }
        ]
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "For more information on how to choose a testing type, we recommend this "
          },
          {
            "type": "link",
            "title": null,
            "url": "/llm/markdown/app/core-concepts/testing-types.md",
            "children": [
              {
                "type": "text",
                "value": "Testing Types Guide"
              }
            ]
          }
        ]
      },
      {
        "type": "heading",
        "depth": 3,
        "children": [
          {
            "type": "text",
            "value": "Project Setup"
          }
        ]
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "The Project Setup screen will automatically detect your framework and bundler. In this example we will use "
          },
          {
            "type": "link",
            "title": null,
            "url": "https://react.dev/",
            "children": [
              {
                "type": "text",
                "value": "React"
              }
            ]
          },
          {
            "type": "text",
            "value": " and "
          },
          {
            "type": "link",
            "title": null,
            "url": "https://vitejs.dev/",
            "children": [
              {
                "type": "text",
                "value": "Vite"
              }
            ]
          },
          {
            "type": "text",
            "value": ".\nClick \"Next Step\" to continue."
          }
        ]
      },
      {
        "type": "heading",
        "depth": 3,
        "children": [
          {
            "type": "text",
            "value": "Install Dependencies"
          }
        ]
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "The next screen checks that all the required dependencies are installed. All the\nitems should have green checkboxes on them, indicating everything is good, so\nclick \"Continue\"."
          }
        ]
      },
      {
        "type": "heading",
        "depth": 3,
        "children": [
          {
            "type": "text",
            "value": "Config Files"
          }
        ]
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "Next, Cypress generates all the necessary configuration files and gives you a\nlist of all the changes it made to your project. Click \"Continue\"."
          }
        ]
      },
      {
        "type": "heading",
        "depth": 3,
        "children": [
          {
            "type": "text",
            "value": "Choose A Browser"
          }
        ]
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "After setting up component testing, you will be at the browser selection screen."
          }
        ]
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "Pick the browser of your choice and click the \"Start Component Testing\" button\nto open the Cypress App."
          }
        ]
      },
      {
        "type": "heading",
        "depth": 2,
        "children": [
          {
            "type": "text",
            "value": "Writing Your First Test"
          }
        ]
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "At this point, your project is configured to use Cypress Component Testing. However, when the app appears, it won't find any specs because we haven't created\nany yet. Now we need write our first Component Test."
          }
        ]
      },
      {
        "type": "heading",
        "depth": 3,
        "children": [
          {
            "type": "text",
            "value": "Frameworks"
          }
        ]
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "Cypress currently has official mounting libraries for\n"
          },
          {
            "type": "link",
            "title": null,
            "url": "/llm/markdown/app/component-testing/react/overview.md",
            "children": [
              {
                "type": "text",
                "value": "React"
              }
            ]
          },
          {
            "type": "text",
            "value": ",\n"
          },
          {
            "type": "link",
            "title": null,
            "url": "/llm/markdown/app/component-testing/angular/overview.md",
            "children": [
              {
                "type": "text",
                "value": "Angular"
              }
            ]
          },
          {
            "type": "text",
            "value": ",\n"
          },
          {
            "type": "link",
            "title": null,
            "url": "/llm/markdown/app/component-testing/vue/overview.md",
            "children": [
              {
                "type": "text",
                "value": "Vue"
              }
            ]
          },
          {
            "type": "text",
            "value": ", and\n"
          },
          {
            "type": "link",
            "title": null,
            "url": "/llm/markdown/app/component-testing/svelte/overview.md",
            "children": [
              {
                "type": "text",
                "value": "Svelte"
              }
            ]
          },
          {
            "type": "text",
            "value": ".\nIn addition to our official framework support there are many community plugins such as\n"
          },
          {
            "type": "link",
            "title": null,
            "url": "https://github.com/qwikifiers/cypress-qwik",
            "children": [
              {
                "type": "text",
                "value": "Qwik"
              }
            ]
          },
          {
            "type": "text",
            "value": " and "
          },
          {
            "type": "link",
            "title": null,
            "url": "https://github.com/redfox-mx/cypress-lit",
            "children": [
              {
                "type": "text",
                "value": "Lit"
              }
            ]
          },
          {
            "type": "text",
            "value": "."
          }
        ]
      },
      {
        "type": "heading",
        "depth": 3,
        "children": [
          {
            "type": "text",
            "value": "Your First Component Test"
          }
        ]
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "Though every framework is different and has its own unique nuances, in general, writing tests is very similar.\nLet's look at how a basic test is written for a `StepperComponent`."
          }
        ]
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "Let's assume the Stepper Component consists of two `buttons`, one used to decrement the counter and one\nto increment it as well as a `span` tag that sits in the middle of the buttons to show the\ncurrent value of the counter."
          }
        ]
      },
      {
        "type": "heading",
        "depth": 3,
        "children": [
          {
            "type": "text",
            "value": "Test Explanation"
          }
        ]
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "Let's break down the spec. First, we import the `Stepper` component. Then, we\norganize our tests using the functions `describe` and `it`, which allows us to\ngroup tests into sections by using method blocks. These are global functions\nprovided by Cypress, which means you don't have to import them directly to use\nthem. The top-level `describe` block will be the container for all our tests in\na file, and each `it` represents an individual test. The `describe` function\ntakes two parameters, the first of which is the name of the test suite, and the\nsecond is a function that will execute the tests."
          }
        ]
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "We defined a test using the `it` function inside `describe`. The first parameter\nto `it` is a brief description of the spec, and the second parameter is a\nfunction that contains the test code. In our example above, we only have one\ntest, but soon we'll see how we can add multiple `it` blocks inside of a\n`describe` for a series of tests."
          }
        ]
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "The "
          },
          {
            "type": "link",
            "title": null,
            "url": "/llm/markdown/api/commands/mount.md",
            "children": [
              {
                "type": "text",
                "value": "cy.mount()"
              }
            ]
          },
          {
            "type": "text",
            "value": " method will mount our component into the test\napp so we can begin running tests against it."
          }
        ]
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "Now it's time to see the test in action."
          }
        ]
      },
      {
        "type": "heading",
        "depth": 2,
        "children": [
          {
            "type": "text",
            "value": "Running the Test"
          }
        ]
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "Switch back to the browser you opened for testing, and you should now see the\n`Stepper` Component in the spec list. Click it to see the spec execute."
          }
        ]
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "Our first test verifies the component can mount in its default state without\nany errors. If there is a runtime error during test execution, the test will\nfail, and you will see a stack trace pointing to the source of the problem."
          }
        ]
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "A basic test like the one above is an excellent way to start testing a\ncomponent. Cypress renders your component in a real browser, and you can use all\nthe techniques/tools you would normally during development, such as interacting\nwith the component in the test runner, and using the browser dev tools to\ninspect and debug both your tests and the component's code."
          }
        ]
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "Feel free to play around with the `Stepper` component by interacting with the\nincrement and decrement buttons."
          }
        ]
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "Now that the component is mounted, our next step is to test that the behavior of\nthe component is correct."
          }
        ]
      },
      {
        "type": "heading",
        "depth": 3,
        "children": [
          {
            "type": "text",
            "value": "Selectors & Assertions"
          }
        ]
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "By default, the Stepper's counter is initialized to \"0\". We can override that\nvalue by specifying an initial count. Let's write a couple of tests that will\nverify both these states."
          }
        ]
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "To do so, we will use a selector to access the `span` element that contains the\ncounter, and then assert that the text value of the element is what we expect it\nto be."
          }
        ]
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "There are various ways to select items from the DOM using Cypress. We will use\n"
          },
          {
            "type": "link",
            "title": null,
            "url": "/llm/markdown/api/commands/get.md",
            "children": [
              {
                "type": "text",
                "value": "cy.get()"
              }
            ]
          },
          {
            "type": "text",
            "value": ", which allows us to pass in a CSS-like selector."
          }
        ]
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "After we \"get\" the element, we use the "
          },
          {
            "type": "link",
            "title": null,
            "url": "/llm/markdown/api/commands/should.md",
            "children": [
              {
                "type": "text",
                "value": "should"
              }
            ]
          },
          {
            "type": "text",
            "value": " assertion\nmethod to verify it has the correct text value."
          }
        ]
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "Add the following test inside the `describe` block, right below the first test:"
          }
        ]
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "When you go back to the test runner, you should see the test pass."
          }
        ]
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "In the above test, we select the element by passing in \"span\" to `cy.get()`,\nwhich will select all `span` tags in our component. We only have one `span`\ncurrently, so this works. However, if our component evolves and we add another\n`span`, then this test could start to fail. We should use a selector that will\nbe less brittle to future changes."
          }
        ]
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "In the `Stepper` component, the `span` tag has a `data-cy` attribute on it:"
          }
        ]
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "We assign a unique id to the `data-cy` attribute that we can use for testing\npurposes. Update the test to use a CSS attribute selector to `cy.get()`:"
          }
        ]
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "The test will still pass as expected, and our selector is now future-proof. For\nmore info on writing good selectors, see our guide\n"
          },
          {
            "type": "link",
            "title": null,
            "url": "/llm/markdown/app/core-concepts/best-practices.md#Selecting-Elements",
            "children": [
              {
                "type": "text",
                "value": "Selector Best Practices"
              }
            ]
          },
          {
            "type": "text",
            "value": "."
          }
        ]
      },
      {
        "type": "heading",
        "depth": 3,
        "children": [
          {
            "type": "text",
            "value": "Passing Props to Components"
          }
        ]
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "We should also have a test to ensure the `count` prop sets the count to\nsomething else besides its default value of \"0\". We can pass in props to the\n`Stepper` component like so:"
          }
        ]
      },
      {
        "type": "heading",
        "depth": 3,
        "children": [
          {
            "type": "text",
            "value": "Testing Interactions"
          }
        ]
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "We mounted and selected the element in the above tests but didn't interact with\nit. We should also test that the value of the counter changes when a user clicks\nthe \"increment\" and \"decrement\" buttons."
          }
        ]
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "To do so, we will interact with the component by using the\n"
          },
          {
            "type": "link",
            "title": null,
            "url": "/llm/markdown/api/commands/click.md",
            "children": [
              {
                "type": "text",
                "value": "click()"
              }
            ]
          },
          {
            "type": "text",
            "value": " command, which clicks a DOM element just like a\nreal user would."
          }
        ]
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "Add the following tests:"
          }
        ]
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "When you view the results of each of the tests, you will see that the counter is\nnow \"1\" in the increment test, and \"-1\" in the decrement test. Not only did our\ntests pass, but we can visually see the results in a browser!"
          }
        ]
      },
      {
        "type": "heading",
        "depth": 2,
        "children": [
          {
            "type": "text",
            "value": "Testing Components with Events"
          }
        ]
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "All the state of the `Stepper` Component (ie: the count) is handled internally in the\ncomponent and then consumers are then alerted to changes to the state."
          }
        ]
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "As the developer of the `Stepper` Component, you want to make sure when the\nend-user clicks the increment and decrement buttons, that the appropriate event is\ntriggered with the proper values in the consuming component."
          }
        ]
      },
      {
        "type": "heading",
        "depth": 3,
        "children": [
          {
            "type": "text",
            "value": "Using Spies"
          }
        ]
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "We can use "
          },
          {
            "type": "link",
            "title": null,
            "url": "/llm/markdown/app/guides/stubs-spies-and-clocks.md#Spies",
            "children": [
              {
                "type": "text",
                "value": "Cypress Spies"
              }
            ]
          },
          {
            "type": "text",
            "value": " to\nvalidate these events are being called correctly. A spy is a special function that\nkeeps track of how many times it was called and any parameters that it was\ncalled with. We can then assign a spy to our event, interact with the\ncomponent, and then query the spy to validate it was called with the parameters\nwe expect."
          }
        ]
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "Let's set up the spies and bind them to the component:"
          }
        ]
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "First, we create a new spy by calling the `cy.spy()` method. We pass in a string\nthat gives the spy an "
          },
          {
            "type": "link",
            "title": null,
            "url": "/llm/markdown/app/core-concepts/variables-and-aliases.md",
            "children": [
              {
                "type": "text",
                "value": "alias"
              }
            ]
          },
          {
            "type": "text",
            "value": ",\nwhich assigns the spy a name by which we can reference it later. In\n`cy.mount()`, we initialize the component and pass the spy into it. After that,\nwe click the increment button."
          }
        ]
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "The next line is a bit different. We've seen how we can use the `cy.get()`\nmethod to select elements, but we can also use it to grab any aliases we've set\nup previously. We use `cy.get()` to grab the alias to the spy (by prepending an\n\"@\" to the alias name). We assert that the method was called with the expected\nvalue."
          }
        ]
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "With that, the `Stepper` component is well tested. Nice job!"
          }
        ]
      },
      {
        "type": "heading",
        "depth": 2,
        "children": [
          {
            "type": "text",
            "value": "What's Next?"
          }
        ]
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "Congratulations, you covered the basics for Component Testing\nwith Cypress!"
          }
        ]
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "To learn more about testing with Cypress, check out the\n"
          },
          {
            "type": "link",
            "title": null,
            "url": "/llm/markdown/app/core-concepts/introduction-to-cypress.md",
            "children": [
              {
                "type": "text",
                "value": "Introduction to Cypress"
              }
            ]
          },
          {
            "type": "text",
            "value": " guide."
          }
        ]
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "Writing component tests with an AI coding tool? The `/cypress-author` skill applies Cypress best practices automatically. See "
          },
          {
            "type": "link",
            "title": null,
            "url": "/llm/markdown/app/tooling/ai-skills.md",
            "children": [
              {
                "type": "text",
                "value": "Cypress AI Skills"
              }
            ]
          },
          {
            "type": "text",
            "value": "."
          }
        ]
      }
    ]
  },
  "token_estimate": 2212
}