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

Package detail

@variablesoftware/mock-d1

variablesoftware746MIT0.6.3TypeScript support: included

🎛️🗂️🧠 Mock D1 Database implementation for testing Cloudflare Workers

cloudflare, workers, serverless, storage, d1, database, emulator, mock, test, miniflare, sqlite, testing, typescript

readme

@variablesoftware/mock-d1 🎛️🗂️🧠

Test Suite NPM version License Coverage Bundle Size Downloads PRs Welcome

Mock Cloudflare D1 Database for unit and integration testing

🎛️🗂️🧠 @variablesoftware/mock-d1 provides an in-memory simulation of Cloudflare's D1 SQLite-compatible database. It enables fast, isolated, and predictable testing of SQL-backed applications without relying on external services.


🔧 Installation

pnpm add --dev @variablesoftware/mock-d1

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


🚀 Usage

import { mockD1Database } from "@variablesoftware/mock-d1";

const db = mockD1Database({
  sessions: [{ sub: "user-123", jti: "token-abc", created: Date.now() }],
});

const stmt = db.prepare("SELECT * FROM sessions WHERE sub = ?");
stmt.bind("user-123");
const result = await stmt.all();

console.log(result.results); // [{ sub: 'user-123', ... }]

🎯 Goals

  • ⚙ Match Cloudflare's behavior for testing real query flows
  • 📐 Explicit mock factories preferred over static snapshots
  • 📦 Eventually compatible with service bindings

✨ Features

  • Fully in-memory, no persistent writes
  • SQL-style .prepare().bind().all() and .run() flow
  • Supports mock row injection
  • Isolated per test run
  • Compatible with Vitest and Hono-based Cloudflare Workers
  • Logs via @variablesoftware/logface
  • Supports simple SELECT, INSERT, UPDATE, and DELETE statements
  • Optional .dump() method for snapshot inspection
  • Returns results shaped like real Cloudflare D1Result
  • Does not coerce types or values — faithfully returns your stored inputs
  • Strives for parity with Cloudflare D1 behavior while keeping mocks debuggable

🧪 Test Coverage

Tested using vitest run, with coverage for:

  • The "butter churn" suite stress-tests mockD1Database() with randomized insert/select/delete operations to simulate real query volume
  • Basic SELECT queries
  • Parameter binding
  • Return shape matching Cloudflare's D1Result

Run tests:

pnpm test

🚧 Status

This package is under active development and not yet stable.

Once stable, it will be published as:

"@variablesoftware/mock-d1": "^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.6.3 (2025-06-09)

0.6.2 (2025-06-09)

0.6.1 (2025-06-07)

0.6.0 (2025-06-07)

0.5.3 (2025-06-07)

0.5.2 (2025-06-07)

0.5.1 (2025-06-07)

0.5.0 (2025-06-07)

0.4.0 (2025-06-06)

0.3.4 (2025-06-01)

Bug Fixes

  • 🛠️ Empty commit message (a239a01)

0.3.3 (2025-05-26)

Bug Fixes

  • add write permissions for contents in coverage-markdown job (3aaf36a)

0.3.2 (2025-05-26)

Bug Fixes

  • add git authentication setup for markdown summary commit in CI workflow (aea3b93)

0.3.1 (2025-05-26)

Bug Fixes

  • coverage: force add markdown summary to ensure commit (64fb9b7)

0.3.0 (2025-05-26)

Features

  • add coverage reporting and markdown summary generation to CI workflow (01593af)

0.2.2 (2025-05-26)

Bug Fixes

  • reorder commit message guidelines for clarity (c426de7)
  • reorganize emoji usage explanations for clarity (ac2720b)
  • uncomment diagnostic commands in pre-push hook (ead3970)
  • uncomment linting commands in pre-commit and pre-push hooks (2d7548c)

0.2.1 (2025-05-26)

Bug Fixes

  • making a markdown list, checking it twice. (5e48594)

0.2.0 (2025-05-26)

Features

  • release: v0.2.1 kitchen sink, unclogged? (3552cdd)

0.1.0 (2025-05-09)

Bug Fixes

  • correct logger parameter naming in createPreparedStatement function (31a84c4)
  • ensure linting fails the pre-commit hook if linting errors are present; update package version to 0.1.2 (f544413)
  • reorder lint and build:test commands in pre-push hook for better execution flow (cb35135)

Features

  • add comprehensive instruction files for project setup and usage; enhance logging in mockD1Database; update package dependencies (6dafdbc)
  • add smoke tests for npm package installability and importability from registry (8b1aa2e)
  • add smoke tests for npm package installability and importability; implement tests for registry and local package installation (8d0421c)
  • add support for DDL and DML operations; implement handlers for DROP, TRUNCATE, and ALTER TABLE, and enhance SELECT handling (6380606)
  • implement modular SQL statement handlers; enhance mockD1Database functionality and improve error handling (53afea4)
  • refactor workflow and tests; update SQL handling and add stress tests for mockD1Database (c878fb0)
  • split concerns, tests passing. pass the lint. 🧶 (6593cab)
  • tests passing (4a7ad80)
  • update build process to use Rollup and add new plugins (26ee2d3)
  • update test framework to include Mocks; bump logface dependency to version 0.1.16 (6739677)