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
53 changes: 53 additions & 0 deletions src/cachified.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1771,6 +1771,59 @@ describe('cachified', () => {
}),
);
});

it('rejects all values when batch loader returns wrong array length', async () => {
// Simulates an API that omits missing data (e.g., requesting posts 1, 2, 3
// but post 2 was deleted, so API returns only posts 1 and 3)
const getBatchLoader = () =>
createBatch<{ id: number; title: string }, number>((ids: number[]) => {
// Incorrect implementation: returns filtered results without mapping back
const allPosts = [
{ id: 1, title: 'Post 1' },
{ id: 3, title: 'Post 3' },
];
return allPosts.filter((post) => ids.includes(post.id));
});

// Requesting [1, 2, 3] fails because batch loader returns only 2 items
const cache1 = new Map<string, CacheEntry>();
const batch1 = getBatchLoader();
const values = [1, 2, 3].map((id) =>
cachified({
cache: cache1,
key: `post-${id}`,
getFreshValue: batch1.add(id),
}),
);

await expect(values[0]).rejects.toMatchInlineSnapshot(
`[Error: Batch loader must return an array with the same length as the input array (expected 3, got 2)]`,
);
await expect(values[1]).rejects.toMatchInlineSnapshot(
`[Error: Batch loader must return an array with the same length as the input array (expected 3, got 2)]`,
);
await expect(values[2]).rejects.toMatchInlineSnapshot(
`[Error: Batch loader must return an array with the same length as the input array (expected 3, got 2)]`,
);

// Requesting [1, 3] succeeds because batch loader returns exactly 2 items
const cache2 = new Map<string, CacheEntry>();
const batch2 = getBatchLoader();
const validValues = await Promise.all(
[1, 3].map((id) =>
cachified({
cache: cache2,
key: `post-${id}`,
getFreshValue: batch2.add(id),
}),
),
);

expect(validValues).toEqual([
{ id: 1, title: 'Post 1' },
{ id: 3, title: 'Post 3' },
]);
});
});

function createReporter() {
Expand Down
5 changes: 5 additions & 0 deletions src/createBatch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,11 @@ export function createBatch<Value, Param>(
requests.map((args) => args[3]),
),
);
if (results.length !== requests.length) {
throw new Error(
`Batch loader must return an array with the same length as the input array (expected ${requests.length}, got ${results.length})`,
);
}
results.forEach((value, index) => requests[index][1](value));
submission.resolve();
} catch (err) {
Expand Down