Playwright Component Testing | React, Vue, Svelte, MSW & CI

Playwright Component Testing | React, Vue, Svelte, MSW & CI

이 글의 핵심

Playwright Component Testing renders isolated components in a real browser using the same locator and assertion model as E2E. This English guide follows the Korean deep-dive: framework setup, mount vs jsdom, MSW integration, visual regression, CI—and explains the CT bundling pipeline, serialization limits on props, and when to keep Vitest for speed.

Role in the testing pyramid

LayerToolingStrength
Unit / integration (Node)Vitest, Jest + RTLSpeed, logic
Component (browser)Playwright CT, Cypress CTLayout, real events
E2EPlaywright, CypressFull user journeys

Component Testing validates a widget-sized surface—modal, table row, form section—without navigating the whole app.


Architecture: mount pipeline

  1. npm init playwright@latest -- --ct scaffolds playwright/index.html, playwright/index.tsx, and playwright-ct.config.ts.
  2. The CT runner bundles each test plus component entry with Vite (configurable via ctViteConfig).
  3. The mount fixture renders your component into the test shell; the return value behaves like a locator to the mounted root—reuse familiar click, screenshot, and getByRole APIs.

Props must stay serializable; complex live objects from Node do not cross cleanly—wrap with test doubles or wrapper components.


Mocking mechanisms

  • MSW: Share handlers with your app. The Korean guide shows router.use(...) with http handlers—keeps API contracts aligned with Storybook or frontend mocks.
  • page.route: Quick one-off stubs; good for spikes, easy to fragment conventions if overused.
  • beforeMount hooks: Inject routers, themes, i18n—mirror production providers in playwright/index.tsx to avoid style-less or locale-missing renders.

Coverage instrumentation

CT runs a browser bundle; collecting Istanbul/V8 coverage requires enabling coverage in the CT toolchain (version-specific). Many teams skip line coverage for CT and rely on screenshots + behavioral assertions, while keeping Vitest coverage for business logic.

If you must merge coverage, plan report merging in CI and watch for path mapping mismatches between CT and unit runs.


Visual regression

toHaveScreenshot() on the mounted root catches unintended style changes. Mitigate OS/font drift with consistent CI images, document.fonts.ready, and tolerance options.


E2E vs CT boundaries

  • CT: Single component, fast feedback compared to full E2E, slower than jsdom.
  • E2E: Validates routing, real backends, auth cookies across pages.

Use both: CT for design system regressions; E2E for checkout and permissions.


Cypress CT comparison

TopicCypress CTPlaywright CT
APICypress chainsPlaywright locators/assertions
BrowsersOften Chromium-focused setupsMulti-browser projects
MaturityMore historical examplesNewer, experimental flag
DebuggingTime-travel styleTrace Viewer shared with E2E

Teams already on Playwright E2E usually consolidate tooling; Cypress-heavy teams may stay on Cypress CT.


CI

npx playwright install --with-deps chromium
npx playwright test -c playwright-ct.config.ts

Shard large suites; store HTML reports and traces as artifacts.


Production testing patterns

CT runs in CI and optional pre-publish jobs—not in production. Combine with:

  • Storybook visual tests or Chromatic for design review.
  • E2E smoke on staging after deploy.
  • Monitoring separate from test suites.

References