Exit course
Chapter 8 of 11 Beginner 5 min read 2 quick checks Updated

Cypress Component Testing vs End-to-End Testing

Watch this chapter Starts at 4:50 in the 9:03 course video.
On this page
  1. What changes
  2. A React example
  3. Which one to write
  4. The limitation that matters here

Everything so far has been end-to-end testing: visit a URL, drive the whole application. Cypress has a second mode, component testing, that mounts a single UI component in the browser without the rest of the app. This chapter is deliberately short. It shows what component tests look like, when they are the better choice, and the one limitation that matters for the rest of the course.

What changes#

End-to-end Component
Starts with cy.visit(url) cy.mount(<Component />)
Needs a running app (local or deployed) your framework's dev server, which Cypress starts
Tests user flows across pages, real backend one component's behaviour, props and events
Speed seconds per test milliseconds per test
Spec location (default) cypress/e2e/ next to the component, *.cy.jsx

The commands, assertions, cy.intercept and the command log are the same in both modes, so everything from chapters 2 to 5 carries over.

A React example#

This example runs in its own small project, because component testing needs a framework and a bundler. It uses React 18 with Vite 6:

npm install --save-dev cypress@14.2.0 react@18 react-dom@18 vite@6 @vitejs/plugin-react@4

with a minimal vite.config.js:

import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'

export default defineConfig({ plugins: [react()] })

When you run npx cypress open and choose Component Testing, Cypress detects the framework and bundler and writes the configuration for you. It comes down to this cypress.config.js (the scaffolded file may differ in quoting and comments):

const { defineConfig } = require('cypress')

module.exports = defineConfig({
  component: {
    devServer: {
      framework: 'react',
      bundler: 'vite',
    },
  },
})

plus a cypress/support/component.js that registers cy.mount:

import { mount } from 'cypress/react'

Cypress.Commands.add('mount', mount)

and a cypress/support/component-index.html with the element components are mounted into.

The component under test, src/Counter.jsx:

import { useState } from 'react'

export default function Counter({ start = 0, onChange = () => {} }) {
  const [count, setCount] = useState(start)
  const increment = () => {
    setCount(count + 1)
    onChange(count + 1)
  }
  return (
    <div>
      <span data-cy="count">{count}</span>
      <button data-cy="increment" onClick={increment}>+1</button>
    </div>
  )
}

and its test, src/Counter.cy.jsx:

import Counter from './Counter'

describe('<Counter />', () => {
  it('starts at the value it is given', () => {
    cy.mount(<Counter start={5} />)
    cy.get('[data-cy=count]').should('have.text', '5')
  })

  it('calls onChange with the new value', () => {
    const onChange = cy.stub().as('onChange')
    cy.mount(<Counter onChange={onChange} />)

    cy.get('[data-cy=increment]').click()

    cy.get('[data-cy=count]').should('have.text', '1')
    cy.get('@onChange').should('have.been.calledOnceWith', 1)
  })
})

Run it headlessly with:

npx cypress run --component

Both tests pass in a fraction of a second each. The second one shows what component tests are especially good at: cy.stub() replaces a callback prop, and the alias lets you assert how the component called it, something an end-to-end test can only observe indirectly.

A date picker has 14 edge cases (leap years, month boundaries, disabled dates). Where do they belong?

Component tests exercise the widget's own behaviour quickly and in isolation. One end-to-end test then proves the widget is wired into the real page and backend. Fourteen full flows would be slow and would fail for reasons unrelated to the date picker.

Which one to write#

A practical split many teams use:

  • Component tests for anything whose behaviour depends mostly on props and user input: form widgets, tables with sorting, conditional rendering, error states.
  • End-to-end tests for the flows that make money or lose it: sign-up, login, checkout, the main path through the product. Fewer of them, each covering more.

The limitation that matters here#

Component tests run against a dev server on your machine. The TestingBot Cypress documentation lists Cypress Component Testing as not supported on the grid, so in chapter 8 only the end-to-end specs go to TestingBot. Run component tests locally or in your CI job with cypress run --component, where a Linux runner's Chrome or Electron is a good fit for them anyway: component behaviour rarely differs between operating systems the way full pages do.

Where does cy.mount come from in a component test?

Cypress ships a mount function per framework, and the scaffolded support file registers it as cy.mount. That is why the same pattern works for React, Vue, Angular and Svelte, each with its own import.