agentleFS
JoviDeCroockJoviDeCroock/pracht/skills/scaffold-tests/SKILL.md

scaffold-tests

Skill106 starsChanged 7 months agoMITView on GitHub

Set up Vitest for pracht routes, loaders, and middleware in browser mode or JSDOM, with `@pracht/test` helpers and runnable example tests. Use for "scaffold tests", "set up Vitest", "add unit tests", "test this loader", "test this route".

  • Installs packages

1 other project uses a version of this.

Adopted by

Most recently taken up by

The file

We split this skill into the rules it gives an agent

11 rules. About 0 are common, 5 are distinctive, and 6 are very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈5

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 6

    Very specific

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

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

    Use AskUserQuestion to choose between:

    line 28
  2. Distinctive:

    If the project already has one configured, default to it and confirm.

    line 39
  3. Distinctive:

    If the project only needs loader/middleware tests (no component rendering), skip the extras entirely and use the plugin-less config in Step …

    line 72
  4. Very specific:

    If vitest.config.ts already exists, merge — never clobber.

    line 130
  5. Very specific:

    Use pracht inspect routes --json to find targets.

    line 140
  6. Distinctive:

    Add (or merge) scripts:

    line 227
  7. Very specific:

    If pnpm test already exists, do not overwrite.

    line 238
  8. Distinctive:

    If anything fails on first run, report the failure and the fix.

    line 247
  9. Very specific:

    Ask the rendering-strategy question once per project.

    line 252
  10. Distinctive:

    Only test exports that exist — read the route file before generating.

    line 254
  11. Very specific:

    Use @pracht/test's factories for BaseRouteArgs/LoaderArgs construction instead of hand-building args objects.

    line 255

≈ “Common” is an estimate: at least 1 in 5 of the 350,495 skill 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
SKILL.md

Pracht Scaffold Tests

Generate Vitest tests aligned with pracht's testing recipe (https://pracht.resynapse.dev/docs/recipes/testing). Loaders and API handlers are plain async functions — they test directly with no framework bootstrap. Component tests need a renderer; the user picks the flavor.

Step 1: Pick the rendering strategy

Use AskUserQuestion to choose between:

  1. Browser mode — vitest with @vitest/browser and vitest-browser-preact.
    • Pros: real browser, real events, fewer hydration false positives, screenshots, works for SPA-mode interaction tests.
    • Cons: slower, heavier setup, requires a browser binary on CI.
  2. JSDOM — vitest with @testing-library/preact.
    • Pros: fast, lightweight, runs anywhere.
    • Cons: JSDOM lacks layout, certain DOM APIs; brittle for complex UIs.

If the project already has one configured, default to it and confirm.

Step 2: Install dependencies

Detect the package manager from the lockfile.

Common (both modes):

pnpm add -D vitest @types/node @pracht/test

@pracht/test provides the typed args factories (createLoaderArgs, createApiArgs, createMiddlewareArgs, createApiMiddlewareArgs), the runMiddleware() chain runner, submitForm(), and the readJson()/readRedirect() response readers used in the templates below.

Component-test extras — @preact/preset-vite must be an explicit dev dependency: the configs in Step 3 import it, and a transitive-only copy fails under pnpm's strict node_modules.

Browser mode:

pnpm add -D @vitest/browser playwright vitest-browser-preact @preact/preset-vite

JSDOM mode:

pnpm add -D jsdom @testing-library/preact @testing-library/jest-dom @preact/preset-vite

If the project only needs loader/middleware tests (no component rendering), skip the extras entirely and use the plugin-less config in Step 3.

Step 3: Wire vitest.config.ts

Browser mode:

import { defineConfig } from "vitest/config";
import preact from "@preact/preset-vite";

export default defineConfig({
  plugins: [preact()],
  test: {
    browser: {
      enabled: true,
      provider: "playwright",
      instances: [{ browser: "chromium" }],
    },
  },
});

JSDOM mode:

import { defineConfig } from "vitest/config";
import preact from "@preact/preset-vite";

export default defineConfig({
  plugins: [preact()],
  test: {
    environment: "jsdom",
    setupFiles: ["./test/setup.ts"],
  },
});

test/setup.ts (JSDOM only):

import "@testing-library/jest-dom/vitest";

Loader/middleware-only mode (no components) — matches the minimal config in https://pracht.resynapse.dev/docs/recipes/testing.md; no preset, no extra deps:

import { defineConfig } from "vitest/config";

export default defineConfig({
  test: {
    // Exclude E2E tests (run those with Playwright)
    exclude: ["e2e/**", "node_modules/**"],
  },
});

If vitest.config.ts already exists, merge — never clobber.

Step 4: Generate the tests

MCP: if pracht dev-mcp is registered, prefer its inspect_*, generate_*, doctor, and verify tools to the CLI.

This skill covers loaders, middleware, and components. For API handlers (src/api/**), delegate to /pracht-test-api — it owns handler enumeration and per-method test generation; don't duplicate a weaker version here.

Use pracht inspect routes --json to find targets. Ask the user which subset to scaffold, or pass paths via $ARGUMENTS.

Loader test template

import { describe, it, expect } from "vitest";
import { createLoaderArgs } from "@pracht/test";
import { loader } from "./<route-file>";

describe("<route> loader", () => {
  it("returns the expected shape", async () => {
    const data = await loader(createLoaderArgs({ url: "/<path>" }));
    expect(data).toBeDefined();
  });
});

Pass params, headers, a partial context, or a JSON-encoding body as needed; the factory defaults everything else and exposes controller (the AbortController behind args.signal) for cancellation tests.

Middleware test template

import { describe, it, expect } from "vitest";
import { createMiddlewareArgs, readRedirect, runMiddleware } from "@pracht/test";
import { middleware } from "./<middleware-file>";

describe("<name> middleware", () => {
  it("redirects unauthenticated requests", async () => {
    const response = await runMiddleware(middleware, createMiddlewareArgs({ url: "/dashboard" }));
    expect(readRedirect(response).location).toMatch(/^\/login/);
  });

  it("calls through when authenticated", async () => {
    const response = await runMiddleware(
      middleware,
      createMiddlewareArgs({ url: "/dashboard", headers: { cookie: "session=valid" } }),
      async () => new Response("handler ran"),
    );
    expect(await response.text()).toBe("handler ran");
  });
});

runMiddleware() accepts an array to test a chain in manifest order, including context mutations flowing downstream and returned-response short-circuits. A thrown Response resolves by default, matching page/API outer normalization. For raw capability-chain behavior, pass undefined, { thrownResponse: "reject" } after the args/final-handler slots; prefer createCapabilityTestHost() so the test asserts the real typed internal_error envelope. For middleware attached through defineApp({ api }), use createApiMiddlewareArgs() so route has the API metadata shape.

Component test template (browser mode)

import { describe, it, expect } from "vitest";
import { render } from "vitest-browser-preact";
import { Component } from "./<route-file>";

describe("<route> component", () => {
  it("renders the heading", async () => {
    const screen = render(<Component data={{ /* mock loader data */ }} params={{}} />);
    await expect.element(screen.getByRole("heading")).toBeVisible();
  });
});

Component test template (JSDOM)

import { describe, it, expect } from "vitest";
import { render, screen } from "@testing-library/preact";
import { Component } from "./<route-file>";

describe("<route> component", () => {
  it("renders the heading", () => {
    render(<Component data={{ /* mock loader data */ }} params={{}} />);
    expect(screen.getByRole("heading")).toBeInTheDocument();
  });
});

Step 5: Wire package.json

Add (or merge) scripts:

{
  "scripts": {
    "test": "vitest run",
    "test:watch": "vitest"
  }
}

If pnpm test already exists, do not overwrite.

Step 6: Verify

pnpm test
pracht verify --json

If anything fails on first run, report the failure and the fix. Do not commit broken scaffolding.

Rules

  1. Ask the rendering-strategy question once per project; persist by inspecting vitest.config.ts on subsequent runs.
  2. Only test exports that exist — read the route file before generating.
  3. Use @pracht/test's factories for BaseRouteArgs/LoaderArgs construction instead of hand-building args objects.
  4. For routes with getStaticPaths, scaffold a separate test that calls it.
  5. Generated tests should pass on first run with a placeholder assertion; the user fills in real expectations.

$ARGUMENTS

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.