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
|
||||||
|
}
|
||||||
+6
-2
@@ -11,7 +11,9 @@
|
|||||||
"db:migrate": "drizzle-kit migrate",
|
"db:migrate": "drizzle-kit migrate",
|
||||||
"db:studio": "drizzle-kit studio",
|
"db:studio": "drizzle-kit studio",
|
||||||
"db:push": "drizzle-kit push",
|
"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": {
|
"dependencies": {
|
||||||
"@aws-sdk/client-s3": "^3.1036.0",
|
"@aws-sdk/client-s3": "^3.1036.0",
|
||||||
@@ -41,12 +43,14 @@
|
|||||||
"@types/pg": "^8.20.0",
|
"@types/pg": "^8.20.0",
|
||||||
"@types/react": "^19",
|
"@types/react": "^19",
|
||||||
"@types/react-dom": "^19",
|
"@types/react-dom": "^19",
|
||||||
|
"@vitest/runner": "^4.1.5",
|
||||||
"drizzle-kit": "^0.31.10",
|
"drizzle-kit": "^0.31.10",
|
||||||
"eslint": "^9",
|
"eslint": "^9",
|
||||||
"eslint-config-next": "16.2.4",
|
"eslint-config-next": "16.2.4",
|
||||||
"tailwindcss": "^4",
|
"tailwindcss": "^4",
|
||||||
"tsx": "^4.21.0",
|
"tsx": "^4.21.0",
|
||||||
"typescript": "^5"
|
"typescript": "^5",
|
||||||
|
"vitest": "^4.1.5"
|
||||||
},
|
},
|
||||||
"ignoreScripts": [
|
"ignoreScripts": [
|
||||||
"sharp",
|
"sharp",
|
||||||
|
|||||||
@@ -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),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
})
|
||||||
Reference in New Issue
Block a user