Skip to content

Commit 98b0735

Browse files
committed
resolve PR comments
1 parent e964fcd commit 98b0735

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

packages/clerk-js/src/core/__tests__/tokenCache.spec.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
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';
33

44
import { Token } from '../resources/internal';
55
import { SessionTokenCache } from '../tokenCache';
@@ -189,12 +189,18 @@ describe('MemoryTokenCache', () => {
189189
});
190190

191191
describe('dynamic TTL calculation', () => {
192+
let dateNowSpy: ReturnType<typeof vi.spyOn>;
193+
194+
afterEach(() => {
195+
dateNowSpy.mockRestore();
196+
});
197+
192198
it('calculates expiresIn from JWT exp and iat claims and sets timeout based on calculated TTL', async () => {
193199
const cache = SessionTokenCache;
194200

195201
// Mock Date.now to return a fixed timestamp initially
196202
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);
198204

199205
// Test with a 30-second TTL
200206
const shortTtlJwt = createJwtWithTtl(30);
@@ -215,7 +221,7 @@ describe('MemoryTokenCache', () => {
215221
// Advance both the timer and the mocked current time
216222
const advanceBy = 31 * 1000;
217223
vi.advanceTimersByTime(advanceBy);
218-
vi.spyOn(Date, 'now').mockImplementation(() => initialTime + advanceBy);
224+
dateNowSpy.mockImplementation(() => initialTime + advanceBy);
219225

220226
const cachedEntry2 = cache.get(shortTtlKey);
221227
expect(cachedEntry2).toBeUndefined();
@@ -226,7 +232,7 @@ describe('MemoryTokenCache', () => {
226232

227233
// Mock Date.now to return a fixed timestamp initially
228234
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);
230236

231237
// Test with a 120-second TTL
232238
const longTtlJwt = createJwtWithTtl(120);
@@ -248,15 +254,15 @@ describe('MemoryTokenCache', () => {
248254
// Advance 90 seconds - token should still be cached
249255
const firstAdvance = 90 * 1000;
250256
vi.advanceTimersByTime(firstAdvance);
251-
vi.spyOn(Date, 'now').mockImplementation(() => initialTime + firstAdvance);
257+
dateNowSpy.mockImplementation(() => initialTime + firstAdvance);
252258

253259
const cachedEntryAfter90s = cache.get(longTtlKey);
254260
expect(cachedEntryAfter90s).toMatchObject(longTtlKey);
255261

256262
// Advance to 121 seconds - token should be removed
257263
const secondAdvance = 31 * 1000;
258264
vi.advanceTimersByTime(secondAdvance);
259-
vi.spyOn(Date, 'now').mockImplementation(() => initialTime + firstAdvance + secondAdvance);
265+
dateNowSpy.mockImplementation(() => initialTime + firstAdvance + secondAdvance);
260266

261267
const cachedEntryAfter121s = cache.get(longTtlKey);
262268
expect(cachedEntryAfter121s).toBeUndefined();

0 commit comments

Comments
 (0)