From d1e2a8f281b30ebba038873c2af2d46e4a7efb2a Mon Sep 17 00:00:00 2001 From: Adrian Bonpin Date: Sat, 25 Apr 2026 14:10:04 +0800 Subject: [PATCH] test(auth): add test-only auth instance with testUtils plugin and initial tests --- lib/auth/__tests__/auth.test.ts | 27 +++++++++++++++++++++++++++ lib/auth/test.ts | 21 +++++++++++++++++++++ package.json | 8 ++++++-- vitest.config.ts | 15 +++++++++++++++ 4 files changed, 69 insertions(+), 2 deletions(-) create mode 100644 lib/auth/__tests__/auth.test.ts create mode 100644 lib/auth/test.ts create mode 100644 vitest.config.ts diff --git a/lib/auth/__tests__/auth.test.ts b/lib/auth/__tests__/auth.test.ts new file mode 100644 index 0000000..c06c4fe --- /dev/null +++ b/lib/auth/__tests__/auth.test.ts @@ -0,0 +1,27 @@ +import { describe, it, expect, beforeAll } from "vitest" +import { testAuth, getTestHelpers } from "@/lib/auth/test" +import type { TestHelpers } from "better-auth/plugins" + +describe("Better-Auth integration", () => { + let test: TestHelpers + + beforeAll(async () => { + test = await getTestHelpers() + }) + + it("should create a user", async () => { + const user = test.createUser({ email: "test@example.com" }) + expect(user.email).toBe("test@example.com") + }) + + it("should create a session for a user", async () => { + const user = test.createUser({ email: "session-test@example.com" }) + await test.saveUser(user) + + const { session, headers } = await test.login({ userId: user.id }) + expect(session.userId).toBe(user.id) + expect(headers.get("cookie")).toBeTruthy() + + await test.deleteUser(user.id) + }) +}) diff --git a/lib/auth/test.ts b/lib/auth/test.ts new file mode 100644 index 0000000..437ea53 --- /dev/null +++ b/lib/auth/test.ts @@ -0,0 +1,21 @@ +import { betterAuth } from "better-auth" +import { testUtils } from "better-auth/plugins" +import { drizzleAdapter } from "@better-auth/drizzle-adapter" +import { db } from "@/lib/db/index" + +export const testAuth = betterAuth({ + database: drizzleAdapter(db, { + provider: "pg", + }), + plugins: [ + testUtils({ captureOTP: true }), + ], + emailAndPassword: { + enabled: true, + }, +}) + +export async function getTestHelpers() { + const ctx = await testAuth.$context + return ctx.test +} diff --git a/package.json b/package.json index 535f989..0078fc5 100644 --- a/package.json +++ b/package.json @@ -11,7 +11,9 @@ "db:migrate": "drizzle-kit migrate", "db:studio": "drizzle-kit studio", "db:push": "drizzle-kit push", - "db:seed": "bun run lib/db/seed.ts" + "db:seed": "bun run lib/db/seed.ts", + "test": "vitest run", + "test:watch": "vitest" }, "dependencies": { "@aws-sdk/client-s3": "^3.1036.0", @@ -41,12 +43,14 @@ "@types/pg": "^8.20.0", "@types/react": "^19", "@types/react-dom": "^19", + "@vitest/runner": "^4.1.5", "drizzle-kit": "^0.31.10", "eslint": "^9", "eslint-config-next": "16.2.4", "tailwindcss": "^4", "tsx": "^4.21.0", - "typescript": "^5" + "typescript": "^5", + "vitest": "^4.1.5" }, "ignoreScripts": [ "sharp", diff --git a/vitest.config.ts b/vitest.config.ts new file mode 100644 index 0000000..9920ec7 --- /dev/null +++ b/vitest.config.ts @@ -0,0 +1,15 @@ +import { defineConfig } from "vitest/config" +import path from "path" + +export default defineConfig({ + test: { + environment: "node", + globals: true, + setupFiles: [], + }, + resolve: { + alias: { + "@": path.resolve(__dirname), + }, + }, +})