Skip to main content

Overview

Playwright Component Testing allows you to test UI components in isolation within a real browser environment. Test React, Vue, Svelte, and other framework components with the same Playwright APIs you use for end-to-end testing.

Getting Started

Installation

Install Playwright Component Testing for your framework:
This creates a playwright-ct.config.ts file and installs the necessary dependencies.

Project Structure

React Component Testing

Basic Component Test

Test Component Props

Test Component State

Vue Component Testing

Basic Vue Component Test

Test Vue Events

Advanced Testing Patterns

Test with Context

Provide context/store to components:

Test Hooks and Lifecycle

Test Component Interactions

Configuration

Component Testing Config

playwright-ct.config.ts

Run Component Tests

1

Run all component tests

2

Run in UI mode

3

Run specific test file

4

Debug component tests

Best Practices

Test User Behavior: Focus on testing how users interact with components rather than implementation details.
  • Test in isolation: Component tests should focus on a single component
  • Mock external dependencies: Use route handlers to mock API calls
  • Test accessibility: Verify components are accessible with screen readers
  • Test edge cases: Verify components handle empty states and errors
  • Keep tests fast: Component tests should run quickly
  • Use realistic data: Test with data that matches production scenarios
Component testing runs in a real browser, providing higher confidence than unit tests while remaining faster than full E2E tests.

Comparison with E2E Testing

CI/CD Integration

Run component tests in CI:

Troubleshooting

Component not rendering

Verify the component import and check console for errors:

Vite configuration issues

Customize Vite config in playwright-ct.config.ts:
Component testing is experimental. The API may change in future versions.