diff --git a/packages/better-auth/src/db/internal-adapter.test.ts b/packages/better-auth/src/db/internal-adapter.test.ts index c705d258c5..0811779a02 100644 --- a/packages/better-auth/src/db/internal-adapter.test.ts +++ b/packages/better-auth/src/db/internal-adapter.test.ts @@ -2,8 +2,9 @@ import { beforeAll, expect, it, describe, vi, afterEach } from "vitest"; import type { BetterAuthOptions, BetterAuthPlugin } from "../types"; import Database from "better-sqlite3"; import { init } from "../init"; +import { betterAuth } from "../auth"; import { getMigrations } from "./get-migration"; -import { SqliteDialect } from "kysely"; +import { Kysely, SqliteDialect } from "kysely"; import { getTestInstance } from "../test-utils/test-instance"; describe("adapter test", async () => { @@ -191,4 +192,73 @@ describe("adapter test", async () => { identifier: "test-id-1", }); }); + + it("runs the after hook after adding user to db", async () => { + const sampleUser = { + name: "sample", + email: "sample@sampling.com", + password: "sampliiiiiing", + }; + const hookUserCreateAfter = vi.fn(); + + const dialect = new SqliteDialect({ + database: new Database(":memory:"), + }); + + const db = new Kysely({ + dialect, + }); + + const opts: BetterAuthOptions = { + database: { + dialect, + type: "sqlite", + }, + databaseHooks: { + user: { + create: { + async after(user, context) { + hookUserCreateAfter(user, context); + + const userFromDb: any = await db + .selectFrom("user") + .selectAll() + .where("id", "=", user.id) + .executeTakeFirst(); + + expect(user.id).toBe(userFromDb.id); + expect(user.name).toBe(userFromDb.name); + expect(user.email).toBe(userFromDb.email); + expect(user.image).toBe(userFromDb.image); + expect(user.emailVerified).toBe( + Boolean(userFromDb.emailVerified), + ); + expect(user.createdAt).toStrictEqual( + new Date(userFromDb.createdAt), + ); + expect(user.updatedAt).toStrictEqual( + new Date(userFromDb.updatedAt), + ); + }, + }, + }, + }, + emailAndPassword: { enabled: true }, + } satisfies BetterAuthOptions; + + const migrations = await getMigrations(opts); + await migrations.runMigrations(); + + const auth = betterAuth(opts); + + await auth.api.signUpEmail({ + body: { + name: sampleUser.name, + email: sampleUser.email, + password: sampleUser.password, + }, + }); + + expect(hookUserCreateAfter).toHaveBeenCalledOnce(); + }); });