Testing

Vitest covers server loads, form actions, and authorization. Playwright exercises the application in Chromium. The current release has 58 unit tests and 42 browser tests; there is no numeric coverage threshold.

Commands

pnpm test
pnpm test:watch
pnpm exec vitest run "src/routes/(app)/users/users.test.ts"
pnpm test:e2e
pnpm check
pnpm lint
pnpm format:check

Isolated unit tests

Name tests *.test.ts beside the route they cover. createTestDb() creates an in-memory SQLite database for each test. Mock the database module with mockDbModule() before dynamically importing a route.

import { beforeEach, describe, expect, it, vi } from "vitest";
import {
  createTestDb,
  createTestUser,
  createAuthHeaders,
  createFormData,
  createMockRequest,
  type TestDb,
} from "#lib/server/db/test-utils.js";

let testDb: TestDb;
vi.mock("#lib/server/db/index.js", async () =>
  (await import("#lib/server/db/test-utils.js")).mockDbModule(() => testDb)
);
const { actions } = await import("./+page.server.js");

describe("Change password", () => {
  beforeEach(() => { testDb = createTestDb(); });

  it("changes the password for the signed-in user", async () => {
    const userId = await createTestUser(testDb, { role: "viewer" });
    const headers = await createAuthHeaders(userId);
    const request = createMockRequest(
      createFormData({
        currentPassword: "password123",
        newPassword: "new-password-1",
        confirmPassword: "new-password-1",
      }),
      headers
    );
    const result = await actions.changePassword({ request } as Parameters<typeof actions.changePassword>[0]);
    expect(result).toMatchObject({ success: true });
  });
});

Better Auth captures the db object once, when auth.ts is first imported, but each test creates a fresh database. mockDbModule(() => testDb) returns a proxy that forwards every property access to whichever database testDb points at when the call happens, so the shared auth instance always talks to the current test database.

src/test-setup.ts is registered in setupFiles in vite.config.ts. It mocks $app/server so getRequestEvent() returns undefined; outside a real request, the sveltekitCookies plugin then has nothing to forward and auth.api.* calls work in plain Vitest.

Fixture helpers

  • await createTestUser(db, overrides) inserts a user plus a credential account and returns the user ID. The Argon2id password hash is the same as production; the password is password123 and the default role is viewer.
  • await createAuthHeaders(userId) creates a real Better Auth session and returns Headers carrying its signed session cookie. Pass them to createMockRequest for actions that call auth.api.* with request.headers.
  • createMockLocals(userId, role) builds locals for load functions and role checks; role defaults to admin, so pass the intended role explicitly in permission tests.
  • createFormData(entries) accepts a record of string values.
  • createMockRequest(formData, headers?) creates a POST request, optionally with the headers from createAuthHeaders.

Actions that guard on locals need createMockLocals; actions that call Better Auth need real session headers. Many actions need both. Because Better Auth also enforces rules itself (last-admin protection, admin-only endpoints), you can test them directly with auth.api.* and createAuthHeaders; see src/lib/server/auth.test.ts.

When changing schema.ts, also update SCHEMA_SQL in test-utils.ts. Test successful operations, invalid input, unauthenticated requests, and every relevant role.

Browser tests

Name browser tests e2e/*.spec.ts. Install Chromium once:

pnpm exec playwright install chromium
pnpm test:e2e

Playwright automatically creates and seeds .svelte-kit/e2e.db, builds the application, and starts a fresh preview server on port 4173. It ignores your normal database path and does not reuse another running server. Local runs use four workers; CI uses one worker and retries failed tests twice.

Reuse login and heading helpers from e2e/helpers.ts. Failed-run reports appear in playwright-report/; traces are recorded on the first retry. Both report and result directories are ignored by Git.

CI and deployment

The reusable .github/workflows/ci.yml workflow runs frozen installation, type checking, lint, formatting, unit tests, and browser tests. Deployment waits for those checks, builds for the production origin, and verifies the served commit at /api/health.