Tests use Vitest 4 across every app and package, with Testing Library for React. The Python service uses pytest.
Running tests
pnpm test # every workspace, through Turborepo
pnpm test:coverage # with V8 coverage
pnpm --dir apps/web test # one workspace
pnpm --dir apps/web exec vitest # watch mode
pnpm --dir apps/web exec vitest run trpc/routers/__tests__/apiRegistry.test.ts # one file
Run pnpm generate first on a fresh clone: tests import the generated Prisma types.
Where tests live
| Location | What |
|---|---|
apps/web/tests/integration/ | Route handlers, routers and the middleware, with mocked auth and database. Includes routePolicy.test.ts |
apps/web/trpc/routers/__tests__/ | Router tests and the API registry check |
apps/web/lib/**/__tests__/, apps/web/server/__tests__/ | Services and server helpers, including env validation |
packages/*/src/**/*.test.ts(x) | Next to the code they test |
apps/desktop, apps/mobile | Main-process and helper tests |
apps/backend/tests/ | pytest unit and integration tests |
Configuration
apps/web/vitest.config.mts:
environment: "happy-dom"for component tests;globals: true, sodescribe,it,expectandvineed no import (importing them explicitly is fine);@resolves toapps/web;vitest.setup.tsloads@testing-library/jest-dommatchers.
The root vitest.workspace.ts lists apps/* and packages/* as projects.
Testing a tRPC router
Call procedures directly with createCaller and a fake context:
import { vi } from "vitest";
import { homeRouter } from "@/trpc/routers/homeProcedures";
vi.mock("@workspace/auth/better-auth/auth", () => ({
auth: { api: { getSession: vi.fn(), setPassword: vi.fn().mockResolvedValue({ status: true }) } },
}));
it("sets a password", async () => {
const caller = homeRouter.createCaller({
session: { user: { id: "user_1", role: "user" } } as any,
headers: new Headers(),
});
await expect(
caller.setPassword({ newPassword: "password123", confirmPassword: "password123" }),
).resolves.toBeDefined();
});
Test the access rules too: a null session must be rejected, a guest must not run mutations, a user must not reach admin procedures.
Testing a route handler
Import the handler and call it with a Request:
import { vi } from "vitest";
import { POST } from "@/app/api/settings/modifyAvatar/route";
vi.mock("@/server/routeGuard", () => ({
guardRoute: vi.fn().mockResolvedValue({ ok: false, status: 401, error: "Unauthorized" }),
}));
it("rejects signed-out uploads", async () => {
const res = await POST(new Request("http://localhost/api/settings/modifyAvatar", { method: "POST" }));
expect(res.status).toBe(401);
});
Mocking
Mock workspace packages by their import path:
vi.mock("@workspace/database/client", () => ({
default: { user: { findUnique: vi.fn(), update: vi.fn() } },
}));
vi.mock("@workspace/email/resend/index", () => ({
sendSupportEmail: vi.fn(),
}));
Mock external services (Stripe, Dodo, Resend, Upstash, AI providers) in tests; never call them. Set env vars per test with vi.stubEnv("NEXT_PUBLIC_CMS", "postgres") and restore with vi.unstubAllEnvs().
Component tests
// packages/ui/src/components/misc/GuestBanner.test.tsx
import { render, screen } from "@testing-library/react";
import { vi } from "vitest";
import { GuestBanner } from "./GuestBanner";
const isGuest = vi.fn();
vi.mock("../../hooks/useIsGuest", () => ({ useIsGuest: () => isGuest() }));
it("shows the read-only notice to guests", () => {
isGuest.mockReturnValue(true);
render(<GuestBanner />);
expect(screen.getByText(/read-only demo/i)).toBeInTheDocument();
});
it("renders nothing for other users", () => {
isGuest.mockReturnValue(false);
const { container } = render(<GuestBanner />);
expect(container).toBeEmptyDOMElement();
});
Tests that guard conventions
Some tests fail on purpose when you forget a step:
| Test | Fails when |
|---|---|
tests/integration/routePolicy.test.ts | An API route has no route-policy row, a session route skips guardRoute, or a route does not validate input as declared |
trpc/routers/__tests__/apiRegistry.test.ts | apiRegistry.ts does not match the router |
CI
.github/workflows/ci.yml runs on pushes and pull requests:
| Job | Command |
|---|---|
| Lint | pnpm turbo run lint |
| Architecture | pnpm arch:check |
| Typecheck | pnpm turbo run typecheck |
| Test | pnpm turbo run test |
| Web Build | pnpm --dir apps/web build |
| Docker Web Build | pnpm docker:build:web |
| Backend Lint, Backend Typecheck, Backend Test | ruff, pyright and pytest for apps/backend (AI Agents module) |
The open-source repository also runs Template Sync and the Scaffold Variant Matrix. Make the jobs you rely on required checks in your branch protection.
