diff --git a/__tests__/cache.test.ts b/__tests__/cache.test.ts index e0b5d5b13..7948100c7 100644 --- a/__tests__/cache.test.ts +++ b/__tests__/cache.test.ts @@ -54,7 +54,9 @@ describe('cache', () => { user: { name: 'Test' } } }); - jest.advanceTimersByTime(1001); + jest.advanceTimersByTime(799); + expect(Object.keys(cache.cache).length).toBe(1); + jest.advanceTimersByTime(1); expect(Object.keys(cache.cache).length).toBe(0); }); it('expires after `user.exp` when `user.exp` < `expires_in`', () => { @@ -73,7 +75,9 @@ describe('cache', () => { user: { name: 'Test' } } }); - jest.advanceTimersByTime(1001); + jest.advanceTimersByTime(799); + expect(Object.keys(cache.cache).length).toBe(1); + jest.advanceTimersByTime(1); expect(Object.keys(cache.cache).length).toBe(0); }); }); diff --git a/src/cache.ts b/src/cache.ts index 957222185..337e772e6 100644 --- a/src/cache.ts +++ b/src/cache.ts @@ -24,7 +24,7 @@ const createKey = (e: CacheKeyData) => `${e.audience}::${e.scope}`; const getExpirationTimeoutInMilliseconds = (expiresIn: number, exp: number) => { const expTime = (new Date(exp * 1000).getTime() - new Date().getTime()) / 1000; - return Math.min(expiresIn, expTime) * 1000; + return Math.min(expiresIn, expTime) * 1000 * 0.8; }; export default class Cache {