All skills
sergiodxa avatar

/frontend-testing-best-practices

@4d1d816

Testing best practices for the frontend. Emphasizes E2E tests over unit tests, minimal mocking, and testing behavior over implementation details. Use when writing tests or reviewing test code.

Use this Skill: https://skilld.dev/gh/sergiodxa/agent-skills/frontend-testing-best-practices

This session only. Nothing lands on disk.

rulesprefer-e2e-tests.md

≈844 tokens on demand. Your agent reads this file only when SKILL.md points to it.

Prefer E2E Tests Over Unit Tests

Write E2E tests as your default testing strategy. Only write unit tests for pure functions and utilities.

Why

  • E2E tests provide real confidence - they test what users actually do
  • E2E tests catch integration issues that unit tests miss
  • E2E tests don't require mocking, so they're more maintainable
  • Refactoring doesn't break E2E tests (implementation changes, behavior stays)

Decision Flow

Is it a pure function with no dependencies?
  → Yes: Unit test (Vitest)
  → No: Continue...

Is it a loader/action with simple API calls?
  → Yes: Integration test with MSW (Vitest)
  → No: Continue...

Does it involve user interaction, routing, or complex state?
  → Yes: E2E test (Playwright)

Would testing it require complex mocking?
  → Yes: E2E test (Playwright)

Examples

E2E Test (Preferred for User Flows)

// e2e/orders.spec.ts
import { test, expect } from "@playwright/test";

test("user can place an order", async ({ page }) => {
  await page.goto("/catalog");

  await page.getByLabel("Quantity").fill("1");
  await page.getByLabel("Item").selectOption("item-123");
  await page.getByRole("button", { name: "Buy" }).click();

  await expect(page.getByText("Thanks for your order")).toBeVisible();
});

Unit Test (For Pure Functions Only)

// app/utils/format.test.ts
import { describe, test, expect } from "vitest";
import { formatCurrency } from "./format";

describe("formatCurrency", () => {
  test("formats dollars with two decimals", () => {
    expect(formatCurrency(1234.5)).toBe("$1,234.50");
  });

  test("handles zero", () => {
    expect(formatCurrency(0)).toBe("$0.00");
  });
});

Integration Test (For Loaders with Simple Mocking)

// app/routes/profile/route.test.ts
import { describe, test, expect, beforeEach } from "vitest";
import { loader } from "./route";
import { mockServer, http, HttpResponse } from "~/lib/test-utils";

beforeEach(() => {
  mockServer.use(
    http.get("/api/user", () => HttpResponse.json({ name: "John" })),
  );
});

describe("profile loader", () => {
  test("returns user data", async () => {
    let response = await loader({
      request: new Request("http://test"),
      params: {},
      context: {},
    });
    let data = await response.json();
    expect(data.user.name).toBe("John");
  });
});

What NOT to Unit Test

// BAD: Don't unit test React components
describe("OrderForm", () => {
  test("renders form fields", () => {
    render(<OrderForm />);
    // This doesn't provide real confidence
  });
});

// BAD: Don't unit test with complex mocks
describe("CheckoutFlow", () => {
  test("processes checkout", async () => {
    vi.mock("~/lib/transactions");
    vi.mock("~/lib/analytics");
    vi.mock("~/hooks/useCart");
    // If you need this many mocks, write an E2E test
  });
});

Rules

  1. Default to E2E tests for anything involving user interaction
  2. Use unit tests only for pure functions with no dependencies
  3. If a unit test requires more than one mock, consider E2E instead
  4. Don't unit test React components - test them via E2E
  5. Integration tests (Vitest + MSW) are acceptable for loaders/actions
  6. Measure confidence, not coverage

Source: SKILL.md on GitHub

No alerts16d5 checks · Risk SAFE
  • Gen Agent Trust Hub16d

    This skill provides a set of best practices and guidelines for frontend testing, focusing on E2E tests with Playwright and unit tests with Vitest. No security issues were detected.

  • Socket16d

    No alerts

  • Snyk16d

    Risk: LOW · No issues

  • Runlayer7mo

    7 files scanned · No issues

  • ZeroLeaks5mo

    Score: 93/100 · 2 sections analyzed

Signed by skilld at 4d1d816. This ties the file your Agent reads to that commit on GitHub. It does not review the instructions.

Last checked against GitHub 2 months ago.

Dormantupdated 8 months ago
  • Testing
  • React
  • e2e-testing
  • playwright
  • vitest
  • behavior-testing
  • mocking
  • unit-tests
  • test-strategy

README badge

README badge for sergiodxa/agent-skills/frontend-testing-best-practices

Provides guidelines for frontend testing that prioritize end-to-end tests over unit tests, minimize mocking, and test user behavior rather than implementation details. Use when writing or reviewing test code for web applications built with Playwright and Vitest.

Generated from the current SKILL.md.

When should I write unit tests vs E2E tests?
Default to E2E tests for user workflows and features. Only write unit tests for pure functions. If you need 3 or more mocks for a unit test, write an E2E test instead.
Should I test React components directly?
No. Test components through E2E tests that exercise them in a real user flow, rather than isolated component unit tests.
What selectors should I use in E2E tests?
Prefer accessible selectors in this order: role-based (getByRole), label-based (getByLabel), text-based, then test IDs. Avoid CSS selectors.
What testing tools does this skill assume?
E2E tests use Playwright, unit tests use Vitest. MSW (Mock Service Worker) is used for simple API mocking.

Generated from the current SKILL.md. These answers refresh after source changes.