1
1
import { createAuthClient } from "better-auth/client" ;
2
2
import Database from "better-sqlite3" ;
3
- import { beforeAll , describe , expect , it , vi } from "vitest" ;
3
+ import { beforeAll , afterAll , describe , expect , it , vi } from "vitest" ;
4
4
import { expo } from "." ;
5
5
import { expoClient } from "./client" ;
6
6
import { betterAuth } from "better-auth" ;
@@ -44,7 +44,7 @@ vi.mock("expo-linking", async () => {
44
44
45
45
const fn = vi . fn ( ) ;
46
46
47
- describe ( "expo" , async ( ) => {
47
+ function testUtils ( extraOpts ?: Parameters < typeof betterAuth > [ 0 ] ) {
48
48
const storage = new Map < string , string > ( ) ;
49
49
50
50
const auth = betterAuth ( {
@@ -61,6 +61,7 @@ describe("expo", async () => {
61
61
} ,
62
62
plugins : [ expo ( ) ] ,
63
63
trustedOrigins : [ "better-auth://" ] ,
64
+ ...extraOpts ,
64
65
} ) ;
65
66
66
67
const client = createAuthClient ( {
@@ -80,9 +81,20 @@ describe("expo", async () => {
80
81
} ) ,
81
82
] ,
82
83
} ) ;
84
+
85
+ return { storage, auth, client } ;
86
+ }
87
+
88
+ describe ( "expo" , async ( ) => {
89
+ const { auth, client, storage } = testUtils ( ) ;
90
+
83
91
beforeAll ( async ( ) => {
84
92
const { runMigrations } = await getMigrations ( auth . options ) ;
85
93
await runMigrations ( ) ;
94
+ vi . useFakeTimers ( ) ;
95
+ } ) ;
96
+ afterAll ( ( ) => {
97
+ vi . useRealTimers ( ) ;
86
98
} ) ;
87
99
88
100
it ( "should store cookie with expires date" , async ( ) => {
@@ -96,7 +108,7 @@ describe("expo", async () => {
96
108
expect ( storedCookie ) . toBeDefined ( ) ;
97
109
const parsedCookie = JSON . parse ( storedCookie || "" ) ;
98
110
expect ( parsedCookie [ "better-auth.session_token" ] ) . toMatchObject ( {
99
- value : expect . any ( String ) ,
111
+ value : expect . stringMatching ( / . + / ) ,
100
112
expires : expect . any ( String ) ,
101
113
} ) ;
102
114
} ) ;
@@ -128,3 +140,79 @@ describe("expo", async () => {
128
140
expect ( c ) . includes ( "better-auth.session_token" ) ;
129
141
} ) ;
130
142
} ) ;
143
+
144
+ describe ( "expo with cookieCache" , async ( ) => {
145
+ const { auth, client, storage } = testUtils ( {
146
+ session : {
147
+ expiresIn : 5 ,
148
+ cookieCache : {
149
+ enabled : true ,
150
+ maxAge : 1 ,
151
+ } ,
152
+ } ,
153
+ } ) ;
154
+ beforeAll ( async ( ) => {
155
+ const { runMigrations } = await getMigrations ( auth . options ) ;
156
+ await runMigrations ( ) ;
157
+ vi . useFakeTimers ( ) ;
158
+ } ) ;
159
+ afterAll ( ( ) => {
160
+ vi . useRealTimers ( ) ;
161
+ } ) ;
162
+
163
+ it ( "should store cookie with expires date" , async ( ) => {
164
+ const testUser = {
165
+
166
+ password : "password" ,
167
+ name : "Test User" ,
168
+ } ;
169
+ await client . signUp . email ( testUser ) ;
170
+ const storedCookie = storage . get ( "better-auth_cookie" ) ;
171
+ expect ( storedCookie ) . toBeDefined ( ) ;
172
+ const parsedCookie = JSON . parse ( storedCookie || "" ) ;
173
+ expect ( parsedCookie [ "better-auth.session_token" ] ) . toMatchObject ( {
174
+ value : expect . stringMatching ( / .+ / ) ,
175
+ expires : expect . any ( String ) ,
176
+ } ) ;
177
+ expect ( parsedCookie [ "better-auth.session_data" ] ) . toMatchObject ( {
178
+ value : expect . stringMatching ( / .+ / ) ,
179
+ expires : expect . any ( String ) ,
180
+ } ) ;
181
+ } ) ;
182
+ it ( "should refresh session_data when it expired without erasing session_token" , async ( ) => {
183
+ vi . advanceTimersByTime ( 1000 ) ;
184
+ const { data } = await client . getSession ( ) ;
185
+ expect ( data ) . toMatchObject ( {
186
+ session : expect . any ( Object ) ,
187
+ user : expect . any ( Object ) ,
188
+ } ) ;
189
+ const storedCookie = storage . get ( "better-auth_cookie" ) ;
190
+ expect ( storedCookie ) . toBeDefined ( ) ;
191
+ const parsedCookie = JSON . parse ( storedCookie || "" ) ;
192
+ expect ( parsedCookie [ "better-auth.session_token" ] ) . toMatchObject ( {
193
+ value : expect . stringMatching ( / .+ / ) ,
194
+ expires : expect . any ( String ) ,
195
+ } ) ;
196
+ expect ( parsedCookie [ "better-auth.session_data" ] ) . toMatchObject ( {
197
+ value : expect . stringMatching ( / .+ / ) ,
198
+ expires : expect . any ( String ) ,
199
+ } ) ;
200
+ } ) ;
201
+
202
+ it ( "should erase both session_data and session_token when token expired" , async ( ) => {
203
+ vi . advanceTimersByTime ( 5000 ) ;
204
+ const { data } = await client . getSession ( ) ;
205
+ expect ( data ) . toBeNull ( ) ;
206
+ const storedCookie = storage . get ( "better-auth_cookie" ) ;
207
+ expect ( storedCookie ) . toBeDefined ( ) ;
208
+ const parsedCookie = JSON . parse ( storedCookie || "" ) ;
209
+ expect ( parsedCookie [ "better-auth.session_token" ] ) . toMatchObject ( {
210
+ value : expect . stringMatching ( / ^ $ / ) ,
211
+ expires : expect . any ( String ) ,
212
+ } ) ;
213
+ expect ( parsedCookie [ "better-auth.session_data" ] ) . toMatchObject ( {
214
+ value : expect . stringMatching ( / ^ $ / ) ,
215
+ expires : expect . any ( String ) ,
216
+ } ) ;
217
+ } ) ;
218
+ } ) ;
0 commit comments