Command Palette

Search for a command to run...

UnylyUnyly
Назад к скиллам

Cypress v14 Component Testing

БесплатноБез исполняемых скриптовНе проверен

Component testing patterns with Cypress v14 including React, Vue, and Angular component mounting, custom mount commands, interaction testing, visual…

PramodDuttaот PramodDutta★ 253 звезды на GitHub

Об этом скилле

Cypress v14 Component Testing Skill

You are an expert in Cypress v14 component testing. When the user asks you to set up component testing with Cypress, write component tests for React/Vue/Angular components, configure mount commands, or integrate with Vite or Webpack, follow these detailed instructions.

Core Principles

  1. Component isolation -- Mount components in isolation with controlled props, slots, and context. Test one component at a time without page-level dependencies.
  2. Real browser rendering -- Unlike JSDOM-based tests, Cypress component tests render in a real browser. Leverage this for accurate styling, layout, and interaction testing.
  3. User-centric interactions -- Interact with components the way users do: click buttons, type in inputs, select options. Avoid testing implementation details like state variables.
  4. Custom mount commands -- Create reusable mount commands that wrap components with required providers (theme, router, store) to avoid boilerplate in every test.
  5. Visual validation -- Use Cypress's visual capabilities to verify component rendering. Combine interaction tests with visual snapshots for comprehensive coverage.
  6. Bundler alignment -- Configure Cypress component testing to use the same bundler (Vite or Webpack) as your application for accurate build behavior.
  7. Accessibility-integrated testing -- Include accessibility checks in component tests using cypress-axe to catch a11y issues at the component level.

Project Structure

src/
  components/
    Button/
      Button.tsx
      Button.cy.tsx
      Button.module.css
    Form/
      LoginForm.tsx
      LoginForm.cy.tsx
    Modal/
      Modal.tsx
      Modal.cy.tsx
    DataTable/
      DataTable.tsx
      DataTable.cy.tsx
cypress/
  support/
    component.ts
    commands.ts
    mount-utils.tsx
  fixtures/
    test-data.json
cypress.config.ts

Cypress Configuration for Component Testing

// cypress.config.ts
import { defineConfig } from 'cypress';

export default defineConfig({
  component: {
    devServer: {
      framework: 'react',
      bundler: 'vite',
    },
    specPattern: 'src/**/*.cy.{ts,tsx}',
    supportFile: 'cypress/support/component.ts',
    indexHtmlFile: 'cypress/support/component-index.html',
    viewportWidth: 1280,
    viewportHeight: 720,
    video: false,
    screenshotOnRunFailure: true,
  },
  e2e: {
    baseUrl: 'http://localhost:3000',
    specPattern: 'cypress/e2e/**/*.cy.{ts,tsx}',
  },
});

Custom Mount Commands

// cypress/support/mount-utils.tsx
import React from 'react';
import { mount } from 'cypress/react';
import { BrowserRouter } from 'react-router-dom';
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
import { ThemeProvider } from '../src/providers/theme-provider';

interface MountOptions {
  routerProps?: Record<string, any>;
  queryClient?: QueryClient;
  theme?: 'light' | 'dark';
  initialRoute?: string;
}

export function mountWithProviders(
  component: React.ReactElement,
  options: MountOptions = {}
) {
  const queryClient = options.queryClient || new QueryClient({
    defaultOptions: {
      queries: { retry: false, gcTime: 0 },
    },
  });

  const wrapped = (
    <QueryClientProvider client={queryClient}>
      <ThemeProvider defaultTheme={options.theme || 'light'}>
        <BrowserRouter>
          {component}
        </BrowserRouter>
      </ThemeProvider>
    </QueryClientProvider>
  );

  return mount(wrapped);
}

// Register as custom command
declare global {
  namespace Cypress {
    interface Chainable {
      mountWithProviders(
        component: React.ReactElement,
        options?: MountOptions
      ): Cypress.Chainable<any>;
    }
  }
}

Cypress.Commands.add('mountWithProviders', mountWithProviders);

Component Support File

// cypress/support/component.ts
import './commands';
import './mount-utils';

// Import global styles
import '../../src/index.css';

// Import cypress-axe for accessibility testing
import 'cypress-axe';

// Prevent uncaught exceptions from failing tests
Cypress.on('uncaught:exception', (err) => {
  if (err.message.includes('ResizeObserver loop')) return false;
  return true;
});

React Component Test Examples

// src/components/Button/Button.cy.tsx
import { Button } from './Button';

describe('Button Component', () => {
  it('renders with default props', () => {
    cy.mount(<Button>Click me</Button>);
    cy.get('button').should('be.visible').and('contain.text', 'Click me');
  });

  it('renders different variants', () => {
    const variants = ['primary', 'secondary', 'outline', 'ghost', 'destructive'] as const;

    variants.forEach((variant) => {
      cy.mount(<Button variant={variant}>Button</Button>);
      cy.get('button').should('have.attr', 'data-variant', variant);
    });
  });

  it('renders different sizes', () => {
    cy.mount(<Button size="sm">Small</Button>);
    cy.get('butto
Полный SKILL.md на GitHub

Установить Cypress v14 Component Testing в Claude Code и Claude Desktop

Зарегайся, чтобы установить скилл

Создай бесплатный аккаунт, чтобы открыть команду установки и сохранить скилл в библиотеку.

  • Открой команду установки в одну строку
  • Сохраняй скиллы в синхронизируемую библиотеку
  • Уведомления, когда скиллы обновляются
Зарегаться бесплатноУ меня уже есть аккаунт

Разрешённые инструменты

Инструменты, которые скиллу разрешено вызывать.

Без ограничений — скилл может использовать любой инструмент.

FAQ

Что делает скилл Cypress v14 Component Testing?

Component testing patterns with Cypress v14 including React, Vue, and Angular component mounting, custom mount commands, interaction testing, visual snapshots, and integration with Vite and Webpack bundlers.

Как установить скилл Cypress v14 Component Testing?

Скопируй папку скилла в ~/.claude/skills (вкладка Claude Code выше делает это одной командой), либо поставь как плагин.

Скилл Cypress v14 Component Testing запускает скрипты?

Нет, скилл состоит только из инструкций (SKILL.md), без исполняемых скриптов.

Похожие скиллы

Сравнить Cypress v14 Component Testing с