diff --git a/src/cachified.spec.ts b/src/cachified.spec.ts index 0dedacf..e70b0c3 100644 --- a/src/cachified.spec.ts +++ b/src/cachified.spec.ts @@ -105,6 +105,52 @@ describe('cachified', () => { expect(value2).toBe('TWO'); }); + it('caches undefined values', async () => { + const cache = new Map>(); + + const value = await cachified({ + cache, + key: 'test', + getFreshValue() { + return undefined; + }, + }); + + const value2 = await cachified({ + cache, + key: 'test', + getFreshValue() { + throw new Error('🛸'); + }, + }); + + expect(value).toBe(undefined); + expect(value2).toBe(undefined); + }); + + it('caches null values', async () => { + const cache = new Map>(); + + const value = await cachified({ + cache, + key: 'test', + getFreshValue() { + return null; + }, + }); + + const value2 = await cachified({ + cache, + key: 'test', + getFreshValue() { + throw new Error('🛸'); + }, + }); + + expect(value).toBe(null); + expect(value2).toBe(null); + }); + it('throws when no fresh value can be received for empty cache', async () => { const cache = new Map>(); const reporter = createReporter(); diff --git a/src/cachified.ts b/src/cachified.ts index 44fcb80..df0436a 100644 --- a/src/cachified.ts +++ b/src/cachified.ts @@ -32,9 +32,9 @@ export async function cachified( const hasPendingValue = () => { return pendingValues.has(key); }; - const cachedValue = - (!forceFresh && (await getCachedValue(context, report, hasPendingValue))) || - CACHE_EMPTY; + const cachedValue = !forceFresh + ? await getCachedValue(context, report, hasPendingValue) + : CACHE_EMPTY; if (cachedValue !== CACHE_EMPTY) { return cachedValue; }