Skip to content

Commit ee13f13

Browse files
committed
refactor: cover last 3 branches with tests
1 parent b0558a5 commit ee13f13

File tree

2 files changed

+19
-10
lines changed

2 files changed

+19
-10
lines changed

src/assertCacheEntry.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
import type { CacheMetadata } from './common';
22

3+
export function logKey(key?: string) {
4+
return key ? `for ${key} ` : '';
5+
}
6+
37
export function assertCacheEntry(
48
entry: unknown,
59
key?: string,
@@ -9,9 +13,9 @@ export function assertCacheEntry(
913
} {
1014
if (!isRecord(entry)) {
1115
throw new Error(
12-
`Cache entry ${
13-
key ? `for ${key} ` : ''
14-
}is not a cache entry object, it's a ${typeof entry}`,
16+
`Cache entry ${logKey(
17+
key,
18+
)}is not a cache entry object, it's a ${typeof entry}`,
1519
);
1620
}
1721
if (
@@ -21,17 +25,13 @@ export function assertCacheEntry(
2125
(entry.metadata.swr != null && typeof entry.metadata.swr !== 'number')
2226
) {
2327
throw new Error(
24-
`Cache entry ${
25-
key ? `for ${key} ` : ''
26-
}does not have valid metadata property`,
28+
`Cache entry ${logKey(key)}does not have valid metadata property`,
2729
);
2830
}
2931

3032
if (!('value' in entry)) {
3133
throw new Error(
32-
`Cache entry for ${
33-
key ? `for ${key} ` : ''
34-
}does not have a value property`,
34+
`Cache entry for ${logKey(key)}does not have a value property`,
3535
);
3636
}
3737
}

src/cachified.spec.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import {
1818
totalTtl,
1919
} from './index';
2020
import { Deferred } from './createBatch';
21+
import { logKey } from './assertCacheEntry';
2122

2223
jest.mock('./index', () => {
2324
if (process.version.startsWith('v18')) {
@@ -1249,7 +1250,9 @@ describe('cachified', () => {
12491250
(cb as Function)(new Error('Nope2'), null);
12501251
return false;
12511252
});
1252-
expect(cache.delete('test-0')).rejects.toThrowErrorMatchingInlineSnapshot(`"Nope2"`);
1253+
expect(cache.delete('test-0')).rejects.toThrowErrorMatchingInlineSnapshot(
1254+
`"Nope2"`,
1255+
);
12531256

12541257
// handle corrupt cache
12551258
await new Promise((res) => redis.set('test-3', '{{{', res));
@@ -1589,3 +1592,9 @@ describe('totalTtl helper', () => {
15891592
expect(totalTtl({ createdTime: 0, swr: 5 })).toBe(5);
15901593
});
15911594
});
1595+
1596+
describe('internal logKey helper', () => {
1597+
it('falls back to empty string, when no key given', () => {
1598+
expect(logKey()).toBe('');
1599+
});
1600+
});

0 commit comments

Comments
 (0)