agentleFS
golid-aigolid-ai/golid/.cursor/rules/write-tests-frontend.mdc

golid / rules

Cursor rule40 starsChanged 6 months agoMITView on GitHub

Patterns for frontend component tests — SolidJS Testing Library, mocking, jsdom limits

The file

We split this write-tests-frontend.mdc into the rules it gives an agent

6 rules. About 0 are common, 2 are distinctive, and 4 are very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈2

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 4

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Very specific:

    Before writing tests for a module,: read docs/modules/{module}/spec.md for the Critical Test Scenarios section — it's a verified test plan.

    line 12
  2. Distinctive:

    Don't just test happy paths.

    line 17
  3. Very specific:

    Use @solidjs/testing-library for component-level tests.

    line 30
  4. Very specific:

    Place test files next to the component: Button.test.tsx alongside Button.tsx.

    line 47
  5. Very specific:

    Always call afterEach(cleanup) in *.test.tsx files that use @solidjs/testing-library.

    line 51
  6. Distinctive:

    Every component test must assert at least one behavioral property, not just existence.

    line 77

≈ “Common” is an estimate: at least 1 in 5 of the 3,030 Cursor rule files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
write-tests-frontend.mdc
---
description: Patterns for frontend component tests — SolidJS Testing Library, mocking, jsdom limits
globs: frontend/**/*.test.ts,frontend/**/*.test.tsx
alwaysApply: false
---

# Writing Frontend Component Tests

> **Thesis:** Frontend tests verify behavior through props, callbacks, and
> rendered content — not rendering existence alone. Know jsdom's limits.

**Before writing tests for a module,** read `docs/modules/{module}/spec.md`
for the Critical Test Scenarios section — it's a verified test plan.

## Always Test Error Paths

Don't just test happy paths. Every service method should have tests for:

- **Permission denied** — wrong user type, non-owner, non-member → expect `FORBIDDEN`
- **Invalid status** — operation on wrong-status entity → expect `BAD_REQUEST`
- **Not found** — non-existent ID → expect `NOT_FOUND`
- **Duplicate/conflict** — re-creating existing entity → expect `CONFLICT`

These are the bugs that slip through manual testing and break in production.

> This section is intentionally duplicated in `write-tests.mdc` and `write-tests-frontend.mdc`. If updated, change both.

## Component Unit Tests (SolidJS)

Use `@solidjs/testing-library` for component-level tests. Test the public API: renders, accepts props, fires events.

```tsx
import { afterEach } from "vitest";
import { cleanup, render, screen, fireEvent } from "@solidjs/testing-library";
import { Button } from "./Button";

afterEach(cleanup);

test("calls onClick", async () => {
  const handler = vi.fn();
  render(() => <Button onClick={handler}>Click</Button>);
  await fireEvent.click(screen.getByText("Click"));
  expect(handler).toHaveBeenCalledTimes(1);
});
```

Place test files next to the component: `Button.test.tsx` alongside `Button.tsx`. Vitest discovers `src/**/*.test.{ts,tsx}` automatically.

**Important:** `vitest.config.ts` must have `resolve.conditions: ["development", "browser"]` to prevent Solid from resolving server-side bundles in jsdom.

Always call `afterEach(cleanup)` in `*.test.tsx` files that use
`@solidjs/testing-library`. Without cleanup, previous renders can leak Solid
computations into the next test and emit "computations created outside a root"
warnings.

### Mocking Route Dependencies

For route/component tests that mock `~/lib/api` or other modules imported at
component module scope, use `vi.hoisted` and import the route after `vi.mock`.

```tsx
const { mockListItems } = vi.hoisted(() => ({
  mockListItems: vi.fn(),
}));

vi.mock("~/lib/api", () => ({
  itemsApi: { list: (...args: unknown[]) => mockListItems(...args) },
  getErrorMessage: (err: unknown, fallback: string) =>
    err instanceof Error ? err.message : fallback,
}));

import ItemsPage from "./index";
```

### Test Quality Bar

Every component test must assert at least one **behavioral property**, not just existence. "Renders without crashing" is not a test.

```tsx
// BAD — tests nothing meaningful
test("renders", () => {
  const { container } = render(() => <Alert variant="success" message="Done" />);
  expect(container).toBeTruthy();
});

// GOOD — tests actual behavior: correct variant class, message rendered, dismiss works
test("renders success variant with message", () => {
  render(() => <Alert variant="success" message="Done" />);
  expect(screen.getByText("Done")).toBeInTheDocument();
  expect(screen.getByRole("alert")).toHaveClass("bg-green");
});

test("calls onDismiss when close button clicked", async () => {
  const onDismiss = vi.fn();
  render(() => <Alert message="Done" onDismiss={onDismiss} />);
  await fireEvent.click(screen.getByRole("button"));
  expect(onDismiss).toHaveBeenCalledTimes(1);
});
```

**Minimum assertions per component test file:**
- Renders with required props and shows expected content
- Variant/size/state props produce correct visual output (classes, attributes)
- Callbacks fire when expected (onClick, onDismiss, onChange, etc.)
- Accessibility: `axe(container)` passes with no violations (where feasible in jsdom)

**jsdom limitations** — skip or mark as smoke-test-only:
- SVG measurement APIs (`getBBox`, `getScreenCTM`) return zeros — Observable Plot/D3 chart tests can only verify render-without-crash
- `getUserMedia`, `MediaRecorder`, WebGL — not available in jsdom, need E2E
- Drag-and-drop (`pointermove` sequences) — unreliable in jsdom, need E2E

## Related Rules

For coverage thresholds, sync discipline, and pre-push checklist, see `write-tests-frontend-workflow` rule.
For backend test patterns (integration with TestDB, unit table-driven, handler mocks), see `write-tests` rule.
For browser/E2E tests, see `write-tests-e2e` rule.

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.