Skip to content

Fix Playwright Component Test – Component Not Rendering

DodaTech Updated 2026-06-24 3 min read

In this tutorial, you'll learn about Fix Playwright Component Test. We cover key concepts, practical examples, and best practices.

You write a Playwright component test for a React or Vue component. The test mounts the component, but the browser shows a blank page — or the component renders without styles and interactions fail.

Wrong ❌

// Counter.test.jsx
import { test, expect } from '@playwright/experimental-ct-react';
import Counter from './Counter';

test('increment', async ({ mount }) => {
  // ❌ No props, no event handlers
  const component = await mount(<Counter />);

  await component.click();
  // Component doesn't update because the click handler isn't wired
});

```jsx
// Counter.jsx — no initial props
export default function Counter({ initialCount = 0, onIncrement }) {
  const [count, setCount] = useState(initialCount);
  return <button onClick={() => { setCount(c => c + 1); onIncrement?.(c + 1); }}>{count}</button>;
}

The component mounts but clicking doesn't trigger the expected behaviour because `onIncrement` is undefined.

## Right ✅

```javascript
// Counter.test.jsx
import { test, expect } from '@playwright/experimental-ct-react';
import Counter from './Counter';

test('increments on click', async ({ mount }) => {
  const messages: number[] = [];

  const component = await mount(
    <Counter
      initialCount={5}
      onIncrement={(count) => messages.push(count)}
    />
  );

  // Interact with the component
  await component.click();

  // Assert on the DOM
  await expect(component).toContainText('6');
  expect(messages).toEqual([6]);
});

**For Vue components:**

```<a href="/programming-languages/javascript/">javascript</a>
import { test, expect } from '@<a href="/testing-qa/playwright/">playwright</a>/experimental-ct-vue';
import Counter from './Counter.vue';

test('increments', async ({ mount }) => {
  const component = await mount(Counter, {
    props: { initialCount: 5 },
  });

  await component.click();
  await expect(component).toContainText('6');
});

**Register component plugins (Vue):**

```javascript
// playwright/index.js
import { beforeMount } from '@playwright/experimental-ct-vue';
import { createPinia } from 'pinia';

beforeMount(async ({ app }) => {
  app.use(createPinia());
});

**Add CSS/global styles:**

```<a href="/programming-languages/javascript/">javascript</a>
// <a href="/testing-qa/playwright/">playwright</a>/index.js
import '../src/index.css';   // global styles

**Debug component tests:**

```bash
npx <a href="/testing-qa/playwright/">playwright</a> test --ui
# Opens <a href="/testing-qa/playwright/">Playwright</a> UI — inspect the mounted component

## Root Cause

Component tests mount components in an isolated iframe without the app's full context (routing, state management, global CSS). Missing providers, incorrect props, or missing styles cause the component to render incorrectly.

## Prevention

- Always pass all required props and callbacks.
- Use `<a href="/testing-qa/playwright/">playwright</a>/index.{js,ts}` to register global providers (Pinia, Router, Theme).
- Import global CSS in the `<a href="/testing-qa/playwright/">playwright</a>/index.ts` file.
- Use `await component.click()` — not `page.click()` — for component interactions.


## Common Mistakes with component test

1. **Using `foldl` instead of `foldl'` causing stack overflow on large lists**
2. **Forgetting `deriving (Show, Eq)` on custom data types needed for debugging**
3. **Placing the wildcard pattern first in case expressions, making all subsequent patterns unreachable**

These mistakes appear frequently in real-world <a href="/testing-qa/playwright/">PLAYWRIGHT</a> code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.

## Practice Exercise

**Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.**

This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.

## FAQ

<details style="margin-bottom:12px;border:1px solid #e2e8f0;border-radius:10px;overflow:hidden"><summary style="cursor:pointer;padding:14px 18px;font-weight:600;font-size:1.05rem;background:#f8fafc;border-bottom:1px solid #e2e8f0;color:#1e293b">**Q: Can I test component interactions with a store?**</summary><div style="padding:14px 18px;color:#475569;line-height:1.7;background:#fff"><p>A: Yes — wrap the component with the provider in the mount call or in <code>beforeMount</code>.</p>
</div></details><details style="margin-bottom:12px;border:1px solid #e2e8f0;border-radius:10px;overflow:hidden"><summary style="cursor:pointer;padding:14px 18px;font-weight:600;font-size:1.05rem;background:#f8fafc;border-bottom:1px solid #e2e8f0;color:#1e293b">**Q: Why are my component's fonts/styles missing?**</summary><div style="padding:14px 18px;color:#475569;line-height:1.7;background:#fff"><p>A: Import your main CSS file in <code>&lt;a href=&quot;/testing-qa/playwright/&quot;&gt;playwright&lt;/a&gt;/index.ts</code>. Component tests don't load the app's HTML.</p>
</div></details><details style="margin-bottom:12px;border:1px solid #e2e8f0;border-radius:10px;overflow:hidden"><summary style="cursor:pointer;padding:14px 18px;font-weight:600;font-size:1.05rem;background:#f8fafc;border-bottom:1px solid #e2e8f0;color:#1e293b">**Q: How do I test async behaviour (loading, error states)?**</summary><div style="padding:14px 18px;color:#475569;line-height:1.7;background:#fff"><p>A: Mock the API with <code>page.route()</code> before mounting — component tests share the same page context.</p>
</div></details><details style="margin-bottom:12px;border:1px solid #e2e8f0;border-radius:10px;overflow:hidden"><summary style="cursor:pointer;padding:14px 18px;font-weight:600;font-size:1.05rem;background:#f8fafc;border-bottom:1px solid #e2e8f0;color:#1e293b">**Q: Can I use component tests with <a href="/tools/storybook/">Storybook</a>?**</summary><div style="padding:14px 18px;color:#475569;line-height:1.7;background:#fff"><p>A: Yes — <a href="/testing-qa/playwright/">Playwright</a> can test <a href="/tools/storybook/">Storybook</a> stories using <code>@&lt;a href=&quot;/testing-qa/playwright/&quot;&gt;playwright&lt;/a&gt;/experimental-ct-react</code> with the story as the component.</p>
</div></details>

---

*Component testing is covered in the [DodaTech <a href="/testing-qa/playwright/">Playwright</a> Component Tests course](https://dodatech.com/courses/<a href="/testing-qa/playwright/">playwright</a>).*

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro