mirror of
https://github.com/better-auth/better-auth.git
synced 2026-05-23 15:42:09 -05:00
test: use node:sqlite by default
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import { memoryAdapter } from "@better-auth/memory-adapter";
|
||||
import Database from "better-sqlite3";
|
||||
import { DatabaseSync } from "node:sqlite";
|
||||
import { describe, expect, expectTypeOf, it } from "vitest";
|
||||
import type { Auth } from "../types";
|
||||
import { betterAuth } from "./minimal";
|
||||
@@ -55,7 +55,7 @@ describe("auth-minimal", () => {
|
||||
});
|
||||
|
||||
it("should throw error with direct database connection (Kysely required)", async () => {
|
||||
const sqliteDB = new Database(":memory:");
|
||||
const sqliteDB = new DatabaseSync(":memory:");
|
||||
|
||||
const auth = betterAuth({
|
||||
baseURL: "http://localhost:3000",
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import Database from "better-sqlite3";
|
||||
import { DatabaseSync } from "node:sqlite";
|
||||
import { describe, expect, it, vi } from "vitest";
|
||||
import { createAuthEndpoint } from "../api";
|
||||
import { getAdapter } from "../db/adapter-kysely";
|
||||
@@ -286,7 +286,7 @@ describe("base context creation", () => {
|
||||
|
||||
it("should return false for cookieRefreshCache when undefined", async () => {
|
||||
const res = await initBase({
|
||||
database: new Database(":memory:"),
|
||||
database: new DatabaseSync(":memory:"),
|
||||
});
|
||||
expect(res.sessionConfig.cookieRefreshCache).toBe(false);
|
||||
});
|
||||
@@ -329,7 +329,7 @@ describe("base context creation", () => {
|
||||
level: "warn",
|
||||
log,
|
||||
} as any,
|
||||
database: new Database(":memory:"),
|
||||
database: new DatabaseSync(":memory:"),
|
||||
session: {
|
||||
cookieCache: {
|
||||
refreshCache: true,
|
||||
@@ -1298,7 +1298,7 @@ describe("base context creation", () => {
|
||||
level: "warn",
|
||||
log,
|
||||
} as any,
|
||||
database: new Database(":memory:"),
|
||||
database: new DatabaseSync(":memory:"),
|
||||
session: {
|
||||
cookieCache: {
|
||||
refreshCache: true,
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
import Database from "better-sqlite3";
|
||||
import { DatabaseSync } from "node:sqlite";
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { init } from "./init";
|
||||
|
||||
describe("init (with Kysely)", () => {
|
||||
const database = new Database(":memory:");
|
||||
const database = new DatabaseSync(":memory:");
|
||||
|
||||
it("should initialize with Kysely adapter", async () => {
|
||||
const res = await init({
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
import type { GenericEndpointContext } from "@better-auth/core";
|
||||
import { safeJSONParse } from "@better-auth/core/utils/json";
|
||||
import Database from "better-sqlite3";
|
||||
import { Kysely, SqliteDialect } from "kysely";
|
||||
import { afterEach, beforeAll, describe, expect, it, vi } from "vitest";
|
||||
import { betterAuth } from "../auth/full";
|
||||
import { init } from "../context/init";
|
||||
@@ -13,11 +11,9 @@ import type {
|
||||
User,
|
||||
} from "../types";
|
||||
import { getMigrations } from "./get-migration";
|
||||
import { DatabaseSync } from 'node:sqlite'
|
||||
|
||||
describe("internal adapter test", async () => {
|
||||
const sqliteDialect = new SqliteDialect({
|
||||
database: new Database(":memory:"),
|
||||
});
|
||||
const map = new Map();
|
||||
const expirationMap = new Map();
|
||||
let id = 1;
|
||||
@@ -30,10 +26,7 @@ describe("internal adapter test", async () => {
|
||||
const pluginHookUserCreateBefore = vi.fn();
|
||||
const pluginHookUserCreateAfter = vi.fn();
|
||||
const opts = {
|
||||
database: {
|
||||
dialect: sqliteDialect,
|
||||
type: "sqlite",
|
||||
},
|
||||
database: new DatabaseSync(":memory:"),
|
||||
user: {
|
||||
fields: {
|
||||
email: "email_address",
|
||||
@@ -270,30 +263,19 @@ describe("internal adapter test", async () => {
|
||||
};
|
||||
const hookUserCreateAfter = vi.fn();
|
||||
|
||||
const dialect = new SqliteDialect({
|
||||
database: new Database(":memory:"),
|
||||
});
|
||||
const database = new DatabaseSync(":memory:")
|
||||
|
||||
const db = new Kysely<any>({
|
||||
dialect,
|
||||
});
|
||||
|
||||
const opts: BetterAuthOptions = {
|
||||
database: {
|
||||
dialect,
|
||||
type: "sqlite",
|
||||
},
|
||||
const opts = {
|
||||
database,
|
||||
databaseHooks: {
|
||||
user: {
|
||||
create: {
|
||||
async after(user, context) {
|
||||
hookUserCreateAfter(user, context);
|
||||
|
||||
const userFromDb: any = await db
|
||||
.selectFrom("user")
|
||||
.selectAll()
|
||||
.where("id", "=", user.id)
|
||||
.executeTakeFirst();
|
||||
const userFromDb = database
|
||||
.prepare('SELECT * FROM user WHERE id = ?')
|
||||
.get(user.id)!;
|
||||
|
||||
expect(user.id).toBe(userFromDb.id);
|
||||
expect(user.name).toBe(userFromDb.name);
|
||||
@@ -303,10 +285,10 @@ describe("internal adapter test", async () => {
|
||||
Boolean(userFromDb.emailVerified),
|
||||
);
|
||||
expect(user.createdAt).toStrictEqual(
|
||||
new Date(userFromDb.createdAt),
|
||||
new Date(userFromDb.createdAt as string),
|
||||
);
|
||||
expect(user.updatedAt).toStrictEqual(
|
||||
new Date(userFromDb.updatedAt),
|
||||
new Date(userFromDb.updatedAt as string),
|
||||
);
|
||||
},
|
||||
},
|
||||
@@ -339,12 +321,7 @@ describe("internal adapter test", async () => {
|
||||
const capturedTTLs: number[] = [];
|
||||
|
||||
const testOpts = {
|
||||
database: {
|
||||
dialect: new SqliteDialect({
|
||||
database: new Database(":memory:"),
|
||||
}),
|
||||
type: "sqlite",
|
||||
},
|
||||
database: new DatabaseSync(":memory:"),
|
||||
secondaryStorage: {
|
||||
set(key: string, value: string, ttl?: number | undefined) {
|
||||
if (ttl !== undefined) {
|
||||
@@ -645,16 +622,8 @@ describe("internal adapter test", async () => {
|
||||
const testMap = new Map<string, string>();
|
||||
const testExpirationMap = new Map<string, number>();
|
||||
|
||||
const testDb = new Database(":memory:");
|
||||
const testSqliteDialect = new SqliteDialect({
|
||||
database: testDb,
|
||||
});
|
||||
|
||||
const testOpts = {
|
||||
database: {
|
||||
dialect: testSqliteDialect,
|
||||
type: "sqlite",
|
||||
},
|
||||
database: new DatabaseSync(":memory:"),
|
||||
secondaryStorage: {
|
||||
set(key: string, value: string, ttl?: number) {
|
||||
testMap.set(key, value);
|
||||
@@ -752,23 +721,14 @@ describe("internal adapter test", async () => {
|
||||
expect(updatedTTL).toBeDefined();
|
||||
expect(updatedTTL! - expectedTTL).toBeLessThanOrEqual(1);
|
||||
expect(updatedTTL! - expectedTTL).toBeGreaterThanOrEqual(0);
|
||||
|
||||
// Clean up DB
|
||||
testDb.close();
|
||||
});
|
||||
|
||||
it("should deduplicate sessions when active-sessions list contains duplicates", async () => {
|
||||
const testDb = new Database(":memory:");
|
||||
const testDialect = new SqliteDialect({ database: testDb });
|
||||
|
||||
const testMap = new Map<string, string>();
|
||||
const testExpirationMap = new Map<string, number>();
|
||||
|
||||
const testOpts = {
|
||||
database: {
|
||||
dialect: testDialect,
|
||||
type: "sqlite",
|
||||
},
|
||||
database: new DatabaseSync(":memory:"),
|
||||
secondaryStorage: {
|
||||
set(key: string, value: string, ttl?: number) {
|
||||
testMap.set(key, value);
|
||||
@@ -823,8 +783,5 @@ describe("internal adapter test", async () => {
|
||||
// listSessions should deduplicate and return only unique sessions
|
||||
const sessions = await testInternalAdapter.listSessions(user.id);
|
||||
expect(sessions.length).toBe(1);
|
||||
|
||||
// Clean up DB
|
||||
testDb.close();
|
||||
});
|
||||
});
|
||||
|
||||
@@ -6,7 +6,7 @@ import type {
|
||||
VercelProfile,
|
||||
} from "@better-auth/core/social-providers";
|
||||
import { betterFetch } from "@better-fetch/fetch";
|
||||
import Database from "better-sqlite3";
|
||||
import { DatabaseSync } from "node:sqlite";
|
||||
import { HttpResponse, http } from "msw";
|
||||
import { setupServer } from "msw/node";
|
||||
import { OAuth2Server } from "oauth2-mock-server";
|
||||
@@ -614,7 +614,7 @@ describe("Disable signup", async () => {
|
||||
});
|
||||
|
||||
describe("signin", async () => {
|
||||
const database = new Database(":memory:");
|
||||
const database = new DatabaseSync(":memory:");
|
||||
|
||||
beforeAll(async () => {
|
||||
const migrations = await getMigrations({
|
||||
|
||||
@@ -61,8 +61,8 @@ export async function getTestInstance<
|
||||
}
|
||||
|
||||
async function getSqlite() {
|
||||
const { default: Database } = await import("better-sqlite3");
|
||||
return new Database(":memory:");
|
||||
const { DatabaseSync } = await import("node:sqlite");
|
||||
return new DatabaseSync(":memory:");
|
||||
}
|
||||
|
||||
async function getMysql() {
|
||||
|
||||
Reference in New Issue
Block a user