Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

@variablesoftware/mock-kv

variablesoftware441MIT0.4.1TypeScript support: included

🎛️🏷️✨ Mock KV Namespace for testing Cloudflare Workers

cloudflare, workers, serverless, storage, kv-namespace, kv, local, mock, test, test-utils, devtools, miniflare, typescript

readme

@variablesoftware/mock-kv 🎛️🏷️✨

Test Suite NPM version License Coverage Bundle Size Downloads PRs Welcome

Mock Cloudflare KV Namespace for unit and integration testing

🎛️🏷️✨ @variablesoftware/mock-kv provides an in-memory simulation of Cloudflare Workers KV. It is designed for testing key-value storage logic with expiration, metadata, and batch operations — without any external dependencies.


🔧 Installation

yarn add --dev @variablesoftware/mock-kv

This package assumes a test environment with Vitest and support for ESM.


🚀 Usage

import { mockKVNamespace } from '@variablesoftware/mock-kv';

const kv = mockKVNamespace();
await kv.put('token-abc', 'value', { expirationTtl: 60 });

const result = await kv.get('token-abc');
console.log(result); // 'value'

🎯 Goals

  • ⚙ Match Cloudflare KV behavior closely for testing
  • 🧪 Support test-safe mocking of put/get/delete/list flows
  • 📦 No external storage dependencies; uses only in-memory JS objects
  • 📎 Logging via @variablesoftware/logface is required for test and runtime logging, but does not rely on any external services

✨ Features

Includes matching behavior for edge cases like:

  • Key expiration mid-test
  • list() with prefix collisions and limits
  • Metadata preservation across put/get calls

  • In-memory mock of Cloudflare KVNamespace

  • Supports put, get, delete, list, and metadata options
  • TTL-aware: honors expirationTtl and expiration
  • Returns values as string, ArrayBuffer, or null just like real KV
  • Simulates listing behavior including prefix + limit
  • Supports metadata in put() and getWithMetadata()
  • Compatible with Vitest and any Cloudflare Worker test setup
  • Logs via @variablesoftware/logface
  • Optional .dump() method for inspecting KV state during tests

🧪 Test Coverage

Tested using vitest run, with coverage for:

  • put() with TTL and metadata
  • get() and getWithMetadata() matching real behavior
  • delete() and list() consistency
  • Full .dump() snapshots for inspection and debugging

Run tests:

yarn test

🚧 Status

This package is under active development and not yet stable.

Once stable, it will be published as:

"@variablesoftware/mock-kv": "^0.5.0"

📄 License

MIT © Rob Friedman / Variable Software


Built with ❤️ by @variablesoftware
Thank you for downloading and using this project. Pull requests are warmly welcomed!


🌐 Inclusive & Accessible Design

  • Naming, logging, error messages, and tests avoid cultural or ableist bias
  • Avoids assumptions about input/output formats or encodings
  • Faithfully reflects user data — no coercion or silent transformations
  • Designed for clarity, predictability, and parity with underlying platforms (e.g., Cloudflare APIs)
  • Works well in diverse, multilingual, and inclusive developer environments

changelog

0.4.1 (2025-06-20)

0.4.0 (2025-06-20)

0.3.13 (2025-06-19)

0.3.12 (2025-06-13)

0.3.11 (2025-06-09)

0.3.10 (2025-06-07)

0.3.9 (2025-06-07)

0.3.8 (2025-06-07)

0.3.7 (2025-06-07)

0.3.6 (2025-06-07)

0.3.5 (2025-06-07)

0.3.4 (2025-06-07)

0.3.3 (2025-06-06)

0.3.2 (2025-06-06)

0.3.1 (2025-06-06)

0.3.0 (2025-06-06)

0.2.2 (2025-06-01)

Bug Fixes

  • lock file. Remove Rollup configuration file (rollup.config.js) as part of project restructuring. (398a3bc)

0.2.1 (2025-05-29)

Bug Fixes

  • trigger release for test (3c56f1d)

0.2.0 (2025-05-22)

Features

  • 100% test coverage. feat: enhance putHandler to always store metadata, even if null, and update test imports to use index.js (7aded5e)
  • add getWithMetadata functionality and enhance get/put methods with metadata support (dbbeabe)
  • refactor mockKVNamespace implementation and enhance get/put methods with improved handling for metadata and edge cases (1ee7c97)

0.1.0 (2025-05-09)

Bug Fixes

  • remove unused semantic-release dependencies from package.json and yarn.lock (64930cb)
  • reorder build:test and lint commands for improved workflow in CI (61a5de5)
  • reorder lint and build:test commands in pre-push hook for improved execution flow (e3fa9f0)

Features

  • add comprehensive expiration tests for mockKVNamespace, covering immediate expiration, TTL handling, and key deletion (cea06d7)
  • add comprehensive instruction files and update package dependencies for improved project structure and testing (ff8fd03)
  • add set -x to pre-commit and pre-push hooks for better debugging (42fa3de)
  • enhance mockKVNamespace with improved key/value handling, metadata support, and comprehensive tests (a64562b)
  • enhance stress tests for mockKVNamespace with analytics on key/value lengths and operation counts (2e7d9c1)
  • update .gitignore to include vim swap files and tarball builds (33e85cc)
  • update build scripts to use yarn for rollup commands and upgrade logface dependency to version 0.1.14 (df1dc18)
  • update husky hooks for linting, add rollup configuration, and enhance package dependencies (3f0462a)

Changelog

All notable changes to this project will be documented in this file. See Conventional Commits for commit guidelines.