golid / rules
Cursor rule40 starsChanged 6 months agoMITView on GitHub
Patterns for frontend component tests — SolidJS Testing Library, mocking, jsdom limits
Where files are headingNew Cursor rule files have more rules about planning and tracking tasks, and fewer about code style.
Of the Cursor rule files we read, the 1,533 first seen in the last six months, against the older ones: the kind of rule that gained most share, and the kind that lost most. About files like this in general, not this one, and an estimate, since a kind is a pattern in the words.
In this repository44 other files it gives its agents.
- .cursor/rules/audit-bugs.mdcCursor rule
- .cursor/rules/audit-codebase.mdcCursor rule
- .cursor/rules/ci-workflow.mdcCursor rule
- .cursor/rules/codebase-standards.mdcCursor rule
- .cursor/rules/common-commands.mdcCursor rule
- .cursor/rules/deploy-infra.mdcCursor rule
- .cursor/rules/document-module.mdcCursor rule
- .cursor/rules/dynamic-image-endpoints.mdcCursor rule
- .cursor/rules/dynamic-image-http.mdcCursor rule
- .cursor/rules/external-api.mdcCursor rule
- .cursor/rules/feature-flags.mdcCursor rule
- .cursor/rules/frontend-components-advanced.mdcCursor rule
- .cursor/rules/frontend-components.mdcCursor rule
- .cursor/rules/frontend-forms.mdcCursor rule
- .cursor/rules/frontend-lib.mdcCursor rule
- .cursor/rules/git-commits.mdcCursor rule
- .cursor/rules/go-handler.mdcCursor rule
- .cursor/rules/go-service-errors.mdcCursor rule
- .cursor/rules/go-service.mdcCursor rule
- .cursor/rules/iteration-surface.mdcCursor rule
- .cursor/rules/job-queue.mdcCursor rule
- .cursor/rules/observability.mdcCursor rule
- .cursor/rules/openapi.mdcCursor rule
- .cursor/rules/parallel-subagents.mdcCursor rule
- .cursor/rules/plan-execution-loop.mdcCursor rule
- .cursor/rules/plan-feature-execution.mdcCursor rule
- .cursor/rules/plan-feature.mdcCursor rule
- .cursor/rules/plan-infra.mdcCursor rule
- .cursor/rules/planning-standards.mdcCursor rule
- .cursor/rules/refactor-large-files.mdcCursor rule
- .cursor/rules/rename-tool.mdcCursor rule
- .cursor/rules/seed-data.mdcCursor rule
- .cursor/rules/slice-and-ship.mdcCursor rule
- .cursor/rules/solidjs-data-fetching.mdcCursor rule
- .cursor/rules/solidjs-pages.mdcCursor rule
- .cursor/rules/solidstart-routing.mdcCursor rule
- .cursor/rules/sql-migrations.mdcCursor rule
- .cursor/rules/sse-realtime.mdcCursor rule
- .cursor/rules/workflow-routing.mdcCursor rule
- .cursor/rules/write-rules.mdcCursor rule
- .cursor/rules/write-tests-e2e.mdcCursor rule
- .cursor/rules/write-tests-frontend-workflow.mdcCursor rule
- .cursor/rules/write-tests.mdcCursor rule
- .cursor/rules/write-tests-planning.mdcCursor rule
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
- Very specific: line 12
Before writing tests for a module,: read docs/modules/{module}/spec.md for the Critical Test Scenarios section — it's a verified test plan.
- Distinctive: line 17
Don't just test happy paths.
- Very specific: line 30
Use @solidjs/testing-library for component-level tests.
- Very specific: line 47
Place test files next to the component: Button.test.tsx alongside Button.tsx.
- Very specific: line 51
Always call afterEach(cleanup) in *.test.tsx files that use @solidjs/testing-library.
- Distinctive: line 77
Every component test must assert at least one behavioral property, not just existence.
≈ “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.
Files
---
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.
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
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.

