@variablesoftware/mock-kv
Version:
🎛️🏷️✨ Mock KV Namespace for testing Cloudflare Workers
36 lines (35 loc) • 1.1 kB
TypeScript
import type { MockKVNamespace, KVEntry, KVMap } from "./types/MockKVNamespace";
/**
* Creates an in-memory mock implementation of the Cloudflare KVNamespace interface for use in tests.
*
* Features:
* - get / put / delete operations
* - TTL and absolute expiration support
* - Typed JSON parsing (via `{ type: 'json' }`)
* - Key listing with limit
* - .dump() for test inspection
*
* All expiration values are normalized into milliseconds using the `expiresAt` field.
*
* @example
* ```ts
* const kv = mockKVNamespace();
* await kv.put('foo', 'bar', { expirationTtl: 60 });
* const val = await kv.get('foo'); // 'bar'
* ```
*
* @param data - Optional initial KV data to populate the store.
* @returns A mock KVNamespace instance with Cloudflare-like API.
*/
export declare const mockKVNamespace: (data?: KVMap) => MockKVNamespace & {
dump: () => Record<string, KVEntry>;
list: (_opts?: {
limit?: number;
}) => Promise<{
keys: {
name: string;
}[];
list_complete: boolean;
}>;
expireKeyNow: (_key: string) => void;
};