Skip to content

Commit 05099e1

Browse files
fixed test cases and added types to auth.spec ,db.spec
1 parent 92050b9 commit 05099e1

File tree

4 files changed

+200
-330
lines changed

4 files changed

+200
-330
lines changed

src/lib/db.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ let client: postgres.Sql;
1010
try {
1111
client = postgres(DATABASE_URL, {
1212
prepare: false,
13-
max: 10, //Connection pooling
14-
idle_timeout: 30, //auto-close idle connections
13+
max: 10,
14+
idle_timeout: 30,
1515
ssl: process.env.API_POSTGRES_SSL_MODE === "true" ? "allow" : undefined,
1616
...(process.env.NODE_ENV === "development" && {
1717
debug: (connection, query, params) => {
@@ -27,10 +27,10 @@ try {
2727
process.exit(1);
2828
}
2929

30-
//Connect Drizzle ORM
30+
//Connect Drizzle ORM
3131
export const db = drizzle(client);
3232

33-
//Graceful Shutdown Handler
33+
//Graceful Shutdown Handler
3434
const shutdownHandler = async () => {
3535
console.log("Closing database connections...");
3636
try {
@@ -42,6 +42,6 @@ const shutdownHandler = async () => {
4242
process.exit(0);
4343
};
4444

45-
//Listen for termination signals
45+
//Listen for termination signals
4646
process.on("SIGINT", shutdownHandler);
4747
process.on("SIGTERM", shutdownHandler);

test/lib/auth.spec.ts

Lines changed: 18 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1+
import type { MiddlewareInputContext, MiddlewareOptions } from "better-auth";
12
import { beforeEach, describe, expect, test, vi } from "vitest";
23
import { auth } from "~/src/lib/auth";
34
import { db } from "~/src/lib/db";
45

5-
// Mock db.select for /sign-in
66
vi.mock("~/src/lib/db", async () => {
77
const actual =
88
await vi.importActual<typeof import("~/src/lib/db")>("~/src/lib/db");
@@ -23,36 +23,10 @@ vi.mock("~/src/lib/db", async () => {
2323
},
2424
};
2525
});
26-
interface AuthContext {
27-
path: string;
28-
context: {
29-
newSession?: {
30-
user: {
31-
id: string;
32-
email: string;
33-
name: string;
34-
};
35-
session: {
36-
token: string;
37-
};
38-
};
39-
};
40-
json: (data: unknown) => void;
41-
}
4226
describe("auth.hooks.after", () => {
43-
// let jsonMock: ReturnType<typeof vi.fn>;
44-
45-
// beforeEach(() => {
46-
// jsonMock = vi.fn();
47-
// });
48-
4927
test("returns success for /sign-up", async () => {
50-
// Create a spy for json method
5128
const jsonMock = vi.fn((data) => data);
52-
53-
// Create context object with the json spy method
54-
// biome-ignore lint/suspicious/noExplicitAny: We need flexibility for testing
55-
const ctx: any = {
29+
const ctx = {
5630
path: "/sign-up",
5731
context: {
5832
newSession: {
@@ -61,15 +35,9 @@ describe("auth.hooks.after", () => {
6135
},
6236
},
6337
json: jsonMock,
64-
};
65-
66-
// Get the handler function
38+
} as MiddlewareInputContext<MiddlewareOptions>;
6739
const handler = auth.options.hooks.after;
68-
69-
// Execute the handler with our mocked context
7040
const result = await handler(ctx);
71-
72-
// Instead of checking if the mock was called, verify the returned result
7341
expect(result).toEqual({
7442
statusCode: "10000",
7543
message: "Success",
@@ -81,10 +49,8 @@ describe("auth.hooks.after", () => {
8149
});
8250

8351
test("returns user details for /sign-in", async () => {
84-
// Create a spy for json method
8552
const jsonMock = vi.fn((data) => data);
86-
// biome-ignore lint/suspicious/noExplicitAny: We need flexibility for testing
87-
const ctx: any = {
53+
const ctx = {
8854
path: "/sign-in",
8955
context: {
9056
newSession: {
@@ -97,9 +63,7 @@ describe("auth.hooks.after", () => {
9763
},
9864
},
9965
json: jsonMock,
100-
};
101-
102-
// Execute the handler and check result
66+
} as MiddlewareInputContext<MiddlewareOptions>;
10367
const result = await auth.options.hooks.after(ctx);
10468

10569
expect(result).toEqual({
@@ -118,34 +82,26 @@ describe("auth.hooks.after", () => {
11882
});
11983

12084
test("returns error when no session", async () => {
121-
// Create a spy for json method
12285
const jsonMock = vi.fn((data) => data);
123-
// biome-ignore lint/suspicious/noExplicitAny: We need flexibility for testing
124-
const ctx: any = {
86+
const ctx = {
12587
path: "/sign-in",
12688
context: {},
12789
json: jsonMock,
128-
};
129-
130-
// Execute the handler and check result
90+
} as MiddlewareInputContext<MiddlewareOptions>;
13191
const result = await auth.options.hooks.after(ctx);
132-
13392
expect(result).toEqual({
13493
statusCode: "10001",
13594
message: "No active session",
13695
});
13796
});
13897

13998
test("returns failure on db error", async () => {
140-
// Create a spy for json method
14199
const jsonMock = vi.fn((data) => data);
142-
143-
// Mock DB error
144100
vi.mocked(db.select).mockImplementationOnce(() => {
145101
throw new Error("DB fail");
146102
});
147-
// biome-ignore lint/suspicious/noExplicitAny: We need flexibility for testing
148-
const ctx: any = {
103+
104+
const ctx = {
149105
path: "/sign-in",
150106
context: {
151107
newSession: {
@@ -158,11 +114,8 @@ describe("auth.hooks.after", () => {
158114
},
159115
},
160116
json: jsonMock,
161-
};
162-
163-
// Execute the handler and check result
117+
} as MiddlewareInputContext<MiddlewareOptions>;
164118
const result = await auth.options.hooks.after(ctx);
165-
166119
expect(result).toEqual({
167120
statusCode: "10001",
168121
message: "Failure",
@@ -172,23 +125,17 @@ describe("auth.hooks.after", () => {
172125
});
173126

174127
describe("auth.hooks.after error handling", () => {
175-
// Spy on console.error to verify it's called during error handling
176128
const consoleErrorSpy = vi
177129
.spyOn(console, "error")
178130
.mockImplementation(() => {});
179131

180132
beforeEach(() => {
181-
// Clear the mock before each test
182133
consoleErrorSpy.mockClear();
183134
});
184135

185136
test("handles Error object correctly", async () => {
186-
// Create a spy for json method
187137
const jsonMock = vi.fn((data) => data);
188-
189-
// Create context object that accesses newSession normally
190-
// biome-ignore lint/suspicious/noExplicitAny: We need flexibility for testing
191-
const ctx: any = {
138+
const ctx = {
192139
path: "/sign-in",
193140
context: {
194141
newSession: {
@@ -201,20 +148,12 @@ describe("auth.hooks.after error handling", () => {
201148
},
202149
},
203150
json: jsonMock,
204-
};
205-
206-
// Force db.select to throw an Error object with a specific message
151+
} as MiddlewareInputContext<MiddlewareOptions>;
207152
vi.mocked(db.select).mockImplementationOnce(() => {
208153
throw new Error("Database connection failed");
209154
});
210-
211-
// Execute the handler and check result
212155
const result = await auth.options.hooks.after(ctx);
213-
214-
// Verify console.error was called
215156
expect(consoleErrorSpy).toHaveBeenCalled();
216-
217-
// Verify the error response format with the specific error message
218157
expect(result).toEqual({
219158
statusCode: "10001",
220159
message: "Failure",
@@ -223,12 +162,8 @@ describe("auth.hooks.after error handling", () => {
223162
});
224163

225164
test("handles non-Error exceptions correctly", async () => {
226-
// Create a spy for json method
227165
const jsonMock = vi.fn((data) => data);
228-
229-
// Create context object
230-
// biome-ignore lint/suspicious/noExplicitAny: We need flexibility for testing
231-
const ctx: any = {
166+
const ctx = {
232167
path: "/sign-in",
233168
context: {
234169
newSession: {
@@ -241,34 +176,22 @@ describe("auth.hooks.after error handling", () => {
241176
},
242177
},
243178
json: jsonMock,
244-
};
245-
246-
// Force db.select to throw a string instead of an Error object
179+
} as MiddlewareInputContext<MiddlewareOptions>;
247180
vi.mocked(db.select).mockImplementationOnce(() => {
248181
throw "String exception";
249182
});
250-
251-
// Execute the handler and check result
252183
const result = await auth.options.hooks.after(ctx);
253-
254-
// Verify console.error was called
255184
expect(consoleErrorSpy).toHaveBeenCalled();
256-
257-
// Verify the error response format matches the actual implementation
258185
expect(result).toEqual({
259186
statusCode: "10001",
260187
message: "Failure",
261-
error: "Unknown error", // Updated to match actual implementation
188+
error: "Unknown error",
262189
});
263190
});
264191

265192
test("handles numeric exceptions correctly", async () => {
266-
// Create a spy for json method
267193
const jsonMock = vi.fn((data) => data);
268-
269-
// Create context object
270-
// biome-ignore lint/suspicious/noExplicitAny: We need flexibility for testing
271-
const ctx: any = {
194+
const ctx = {
272195
path: "/sign-in",
273196
context: {
274197
newSession: {
@@ -281,24 +204,16 @@ describe("auth.hooks.after error handling", () => {
281204
},
282205
},
283206
json: jsonMock,
284-
};
285-
286-
// Force db.select to throw a number instead of an Error object
207+
} as MiddlewareInputContext<MiddlewareOptions>;
287208
vi.mocked(db.select).mockImplementationOnce(() => {
288209
throw 404;
289210
});
290-
291-
// Execute the handler and check result
292211
const result = await auth.options.hooks.after(ctx);
293-
294-
// Verify console.error was called
295212
expect(consoleErrorSpy).toHaveBeenCalled();
296-
297-
// Verify the error response format - for non-Error objects, it will be "Unknown error"
298213
expect(result).toEqual({
299214
statusCode: "10001",
300215
message: "Failure",
301-
error: "Unknown error", // Updated to match actual implementation
216+
error: "Unknown error",
302217
});
303218
});
304219
});

0 commit comments

Comments
 (0)