test(auth): add test-only auth instance with testUtils plugin and initial tests
This commit is contained in:
@@ -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)
|
||||
})
|
||||
})
|
||||
@@ -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
|
||||
}
|
||||
Reference in New Issue
Block a user