Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions src/__tests__/server.caching.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,32 @@ describe('memo', () => {
await expect(updateLog(logAsync)).resolves.toMatchSnapshot('async');
});

it('should return cached sync objects with isError: true without calling them', () => {
let callCount = 0;
const memoized = memo((value: string) => {
callCount += 1;

return { isError: true, value, callCount };
}, { cacheLimit: 1, cacheErrors: true });

expect(memoized('a')).toEqual({ isError: true, value: 'a', callCount: 1 });
expect(memoized('a')).toEqual({ isError: true, value: 'a', callCount: 1 });
expect(callCount).toBe(1);
});

it('should return sync objects with isError: true when cacheErrors is false', () => {
let callCount = 0;
const memoized = memo((value: string) => {
callCount += 1;

return { isError: true, value, callCount };
}, { cacheLimit: 1, cacheErrors: false });

expect(memoized('a')).toEqual({ isError: true, value: 'a', callCount: 1 });
expect(memoized('a')).toEqual({ isError: true, value: 'a', callCount: 2 });
expect(callCount).toBe(2);
});

it('should handle clear() without callback', () => {
const mockDebug = jest.fn();
const options = { cacheLimit: 1, debug: mockDebug };
Expand Down
2 changes: 1 addition & 1 deletion src/server.caching.ts
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ const memo = <TArgs extends unknown[], TReturn = unknown>(
cache: [...cache]
});

return cachedValue();
return typeof cachedValue === 'function' ? cachedValue() : cachedValue;
}

debug({
Expand Down
Loading