1
+ import type { MiddlewareInputContext , MiddlewareOptions } from "better-auth" ;
1
2
import { beforeEach , describe , expect , test , vi } from "vitest" ;
2
3
import { auth } from "~/src/lib/auth" ;
3
4
import { db } from "~/src/lib/db" ;
4
5
5
- // Mock db.select for /sign-in
6
6
vi . mock ( "~/src/lib/db" , async ( ) => {
7
7
const actual =
8
8
await vi . importActual < typeof import ( "~/src/lib/db" ) > ( "~/src/lib/db" ) ;
@@ -23,36 +23,10 @@ vi.mock("~/src/lib/db", async () => {
23
23
} ,
24
24
} ;
25
25
} ) ;
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
- }
42
26
describe ( "auth.hooks.after" , ( ) => {
43
- // let jsonMock: ReturnType<typeof vi.fn>;
44
-
45
- // beforeEach(() => {
46
- // jsonMock = vi.fn();
47
- // });
48
-
49
27
test ( "returns success for /sign-up" , async ( ) => {
50
- // Create a spy for json method
51
28
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 = {
56
30
path : "/sign-up" ,
57
31
context : {
58
32
newSession : {
@@ -61,15 +35,9 @@ describe("auth.hooks.after", () => {
61
35
} ,
62
36
} ,
63
37
json : jsonMock ,
64
- } ;
65
-
66
- // Get the handler function
38
+ } as MiddlewareInputContext < MiddlewareOptions > ;
67
39
const handler = auth . options . hooks . after ;
68
-
69
- // Execute the handler with our mocked context
70
40
const result = await handler ( ctx ) ;
71
-
72
- // Instead of checking if the mock was called, verify the returned result
73
41
expect ( result ) . toEqual ( {
74
42
statusCode : "10000" ,
75
43
message : "Success" ,
@@ -81,10 +49,8 @@ describe("auth.hooks.after", () => {
81
49
} ) ;
82
50
83
51
test ( "returns user details for /sign-in" , async ( ) => {
84
- // Create a spy for json method
85
52
const jsonMock = vi . fn ( ( data ) => data ) ;
86
- // biome-ignore lint/suspicious/noExplicitAny: We need flexibility for testing
87
- const ctx : any = {
53
+ const ctx = {
88
54
path : "/sign-in" ,
89
55
context : {
90
56
newSession : {
@@ -97,9 +63,7 @@ describe("auth.hooks.after", () => {
97
63
} ,
98
64
} ,
99
65
json : jsonMock ,
100
- } ;
101
-
102
- // Execute the handler and check result
66
+ } as MiddlewareInputContext < MiddlewareOptions > ;
103
67
const result = await auth . options . hooks . after ( ctx ) ;
104
68
105
69
expect ( result ) . toEqual ( {
@@ -118,34 +82,26 @@ describe("auth.hooks.after", () => {
118
82
} ) ;
119
83
120
84
test ( "returns error when no session" , async ( ) => {
121
- // Create a spy for json method
122
85
const jsonMock = vi . fn ( ( data ) => data ) ;
123
- // biome-ignore lint/suspicious/noExplicitAny: We need flexibility for testing
124
- const ctx : any = {
86
+ const ctx = {
125
87
path : "/sign-in" ,
126
88
context : { } ,
127
89
json : jsonMock ,
128
- } ;
129
-
130
- // Execute the handler and check result
90
+ } as MiddlewareInputContext < MiddlewareOptions > ;
131
91
const result = await auth . options . hooks . after ( ctx ) ;
132
-
133
92
expect ( result ) . toEqual ( {
134
93
statusCode : "10001" ,
135
94
message : "No active session" ,
136
95
} ) ;
137
96
} ) ;
138
97
139
98
test ( "returns failure on db error" , async ( ) => {
140
- // Create a spy for json method
141
99
const jsonMock = vi . fn ( ( data ) => data ) ;
142
-
143
- // Mock DB error
144
100
vi . mocked ( db . select ) . mockImplementationOnce ( ( ) => {
145
101
throw new Error ( "DB fail" ) ;
146
102
} ) ;
147
- // biome-ignore lint/suspicious/noExplicitAny: We need flexibility for testing
148
- const ctx : any = {
103
+
104
+ const ctx = {
149
105
path : "/sign-in" ,
150
106
context : {
151
107
newSession : {
@@ -158,11 +114,8 @@ describe("auth.hooks.after", () => {
158
114
} ,
159
115
} ,
160
116
json : jsonMock ,
161
- } ;
162
-
163
- // Execute the handler and check result
117
+ } as MiddlewareInputContext < MiddlewareOptions > ;
164
118
const result = await auth . options . hooks . after ( ctx ) ;
165
-
166
119
expect ( result ) . toEqual ( {
167
120
statusCode : "10001" ,
168
121
message : "Failure" ,
@@ -172,23 +125,17 @@ describe("auth.hooks.after", () => {
172
125
} ) ;
173
126
174
127
describe ( "auth.hooks.after error handling" , ( ) => {
175
- // Spy on console.error to verify it's called during error handling
176
128
const consoleErrorSpy = vi
177
129
. spyOn ( console , "error" )
178
130
. mockImplementation ( ( ) => { } ) ;
179
131
180
132
beforeEach ( ( ) => {
181
- // Clear the mock before each test
182
133
consoleErrorSpy . mockClear ( ) ;
183
134
} ) ;
184
135
185
136
test ( "handles Error object correctly" , async ( ) => {
186
- // Create a spy for json method
187
137
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 = {
192
139
path : "/sign-in" ,
193
140
context : {
194
141
newSession : {
@@ -201,20 +148,12 @@ describe("auth.hooks.after error handling", () => {
201
148
} ,
202
149
} ,
203
150
json : jsonMock ,
204
- } ;
205
-
206
- // Force db.select to throw an Error object with a specific message
151
+ } as MiddlewareInputContext < MiddlewareOptions > ;
207
152
vi . mocked ( db . select ) . mockImplementationOnce ( ( ) => {
208
153
throw new Error ( "Database connection failed" ) ;
209
154
} ) ;
210
-
211
- // Execute the handler and check result
212
155
const result = await auth . options . hooks . after ( ctx ) ;
213
-
214
- // Verify console.error was called
215
156
expect ( consoleErrorSpy ) . toHaveBeenCalled ( ) ;
216
-
217
- // Verify the error response format with the specific error message
218
157
expect ( result ) . toEqual ( {
219
158
statusCode : "10001" ,
220
159
message : "Failure" ,
@@ -223,12 +162,8 @@ describe("auth.hooks.after error handling", () => {
223
162
} ) ;
224
163
225
164
test ( "handles non-Error exceptions correctly" , async ( ) => {
226
- // Create a spy for json method
227
165
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 = {
232
167
path : "/sign-in" ,
233
168
context : {
234
169
newSession : {
@@ -241,34 +176,22 @@ describe("auth.hooks.after error handling", () => {
241
176
} ,
242
177
} ,
243
178
json : jsonMock ,
244
- } ;
245
-
246
- // Force db.select to throw a string instead of an Error object
179
+ } as MiddlewareInputContext < MiddlewareOptions > ;
247
180
vi . mocked ( db . select ) . mockImplementationOnce ( ( ) => {
248
181
throw "String exception" ;
249
182
} ) ;
250
-
251
- // Execute the handler and check result
252
183
const result = await auth . options . hooks . after ( ctx ) ;
253
-
254
- // Verify console.error was called
255
184
expect ( consoleErrorSpy ) . toHaveBeenCalled ( ) ;
256
-
257
- // Verify the error response format matches the actual implementation
258
185
expect ( result ) . toEqual ( {
259
186
statusCode : "10001" ,
260
187
message : "Failure" ,
261
- error : "Unknown error" , // Updated to match actual implementation
188
+ error : "Unknown error" ,
262
189
} ) ;
263
190
} ) ;
264
191
265
192
test ( "handles numeric exceptions correctly" , async ( ) => {
266
- // Create a spy for json method
267
193
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 = {
272
195
path : "/sign-in" ,
273
196
context : {
274
197
newSession : {
@@ -281,24 +204,16 @@ describe("auth.hooks.after error handling", () => {
281
204
} ,
282
205
} ,
283
206
json : jsonMock ,
284
- } ;
285
-
286
- // Force db.select to throw a number instead of an Error object
207
+ } as MiddlewareInputContext < MiddlewareOptions > ;
287
208
vi . mocked ( db . select ) . mockImplementationOnce ( ( ) => {
288
209
throw 404 ;
289
210
} ) ;
290
-
291
- // Execute the handler and check result
292
211
const result = await auth . options . hooks . after ( ctx ) ;
293
-
294
- // Verify console.error was called
295
212
expect ( consoleErrorSpy ) . toHaveBeenCalled ( ) ;
296
-
297
- // Verify the error response format - for non-Error objects, it will be "Unknown error"
298
213
expect ( result ) . toEqual ( {
299
214
statusCode : "10001" ,
300
215
message : "Failure" ,
301
- error : "Unknown error" , // Updated to match actual implementation
216
+ error : "Unknown error" ,
302
217
} ) ;
303
218
} ) ;
304
219
} ) ;
0 commit comments