1
1
import type { TokenResource } from '@clerk/types' ;
2
- import { afterAll , beforeAll , describe , expect , it , vi } from 'vitest' ;
2
+ import { afterAll , afterEach , beforeAll , describe , expect , it , vi } from 'vitest' ;
3
3
4
4
import { Token } from '../resources/internal' ;
5
5
import { SessionTokenCache } from '../tokenCache' ;
@@ -189,12 +189,18 @@ describe('MemoryTokenCache', () => {
189
189
} ) ;
190
190
191
191
describe ( 'dynamic TTL calculation' , ( ) => {
192
+ let dateNowSpy : ReturnType < typeof vi . spyOn > ;
193
+
194
+ afterEach ( ( ) => {
195
+ dateNowSpy . mockRestore ( ) ;
196
+ } ) ;
197
+
192
198
it ( 'calculates expiresIn from JWT exp and iat claims and sets timeout based on calculated TTL' , async ( ) => {
193
199
const cache = SessionTokenCache ;
194
200
195
201
// Mock Date.now to return a fixed timestamp initially
196
202
const initialTime = 1675876730000 ; // Same as our JWT's iat in milliseconds
197
- vi . spyOn ( Date , 'now' ) . mockImplementation ( ( ) => initialTime ) ;
203
+ dateNowSpy = vi . spyOn ( Date , 'now' ) . mockImplementation ( ( ) => initialTime ) ;
198
204
199
205
// Test with a 30-second TTL
200
206
const shortTtlJwt = createJwtWithTtl ( 30 ) ;
@@ -215,7 +221,7 @@ describe('MemoryTokenCache', () => {
215
221
// Advance both the timer and the mocked current time
216
222
const advanceBy = 31 * 1000 ;
217
223
vi . advanceTimersByTime ( advanceBy ) ;
218
- vi . spyOn ( Date , 'now' ) . mockImplementation ( ( ) => initialTime + advanceBy ) ;
224
+ dateNowSpy . mockImplementation ( ( ) => initialTime + advanceBy ) ;
219
225
220
226
const cachedEntry2 = cache . get ( shortTtlKey ) ;
221
227
expect ( cachedEntry2 ) . toBeUndefined ( ) ;
@@ -226,7 +232,7 @@ describe('MemoryTokenCache', () => {
226
232
227
233
// Mock Date.now to return a fixed timestamp initially
228
234
const initialTime = 1675876730000 ; // Same as our JWT's iat in milliseconds
229
- vi . spyOn ( Date , 'now' ) . mockImplementation ( ( ) => initialTime ) ;
235
+ dateNowSpy = vi . spyOn ( Date , 'now' ) . mockImplementation ( ( ) => initialTime ) ;
230
236
231
237
// Test with a 120-second TTL
232
238
const longTtlJwt = createJwtWithTtl ( 120 ) ;
@@ -248,15 +254,15 @@ describe('MemoryTokenCache', () => {
248
254
// Advance 90 seconds - token should still be cached
249
255
const firstAdvance = 90 * 1000 ;
250
256
vi . advanceTimersByTime ( firstAdvance ) ;
251
- vi . spyOn ( Date , 'now' ) . mockImplementation ( ( ) => initialTime + firstAdvance ) ;
257
+ dateNowSpy . mockImplementation ( ( ) => initialTime + firstAdvance ) ;
252
258
253
259
const cachedEntryAfter90s = cache . get ( longTtlKey ) ;
254
260
expect ( cachedEntryAfter90s ) . toMatchObject ( longTtlKey ) ;
255
261
256
262
// Advance to 121 seconds - token should be removed
257
263
const secondAdvance = 31 * 1000 ;
258
264
vi . advanceTimersByTime ( secondAdvance ) ;
259
- vi . spyOn ( Date , 'now' ) . mockImplementation ( ( ) => initialTime + firstAdvance + secondAdvance ) ;
265
+ dateNowSpy . mockImplementation ( ( ) => initialTime + firstAdvance + secondAdvance ) ;
260
266
261
267
const cachedEntryAfter121s = cache . get ( longTtlKey ) ;
262
268
expect ( cachedEntryAfter121s ) . toBeUndefined ( ) ;
0 commit comments