test(auth): add test-only auth instance with testUtils plugin and initial tests

This commit is contained in:
2026-04-25 14:14:23 +08:00
parent a9cb8a328c
commit d1e2a8f281
4 changed files with 69 additions and 2 deletions
+27
View File
@@ -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)
})
})
+21
View File
@@ -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
}