npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

cf-bun-mocks

v0.3.0

Published

Cloudflare Workers mocks and helpers for Bun testing

Readme

cf-bun-mocks

Cloudflare Workers mocks and helpers for Bun testing.

Installation

bun add -d cf-bun-mocks

D1 Mock

The D1 mock provides a drop-in replacement for Cloudflare's D1 database, backed by Bun's native SQLite.

Basic Usage

import { D1Mock } from "cf-bun-mocks";

// In-memory database (recommended for tests)
const db = new D1Mock(":memory:");

// Or persist to a file
const db = new D1Mock("./test.db");

With Migrations

Use createD1Mock to create an in-memory database and run your migrations:

import { createD1Mock } from "cf-bun-mocks";

const db = await createD1Mock("./migrations");

This reads all .sql files from the migrations directory in sorted order and executes them.

Example Test

import { describe, test, expect, beforeEach } from "bun:test";
import { D1Mock } from "cf-bun-mocks";
import type { D1Database } from "@cloudflare/workers-types";

describe("my worker", () => {
  let db: D1Database;

  beforeEach(async () => {
    db = new D1Mock(":memory:");
    await db.exec(`
      CREATE TABLE users (
        id INTEGER PRIMARY KEY,
        name TEXT NOT NULL,
        email TEXT UNIQUE
      )
    `);
  });

  test("insert and query", async () => {
    await db
      .prepare("INSERT INTO users (name, email) VALUES (?, ?)")
      .bind("Alice", "[email protected]")
      .run();

    const user = await db
      .prepare("SELECT * FROM users WHERE email = ?")
      .bind("[email protected]")
      .first();

    expect(user).toEqual({
      id: 1,
      name: "Alice",
      email: "[email protected]",
    });
  });

  test("batch operations", async () => {
    const results = await db.batch([
      db
        .prepare("INSERT INTO users (name, email) VALUES (?, ?)")
        .bind("Bob", "[email protected]"),
      db
        .prepare("INSERT INTO users (name, email) VALUES (?, ?)")
        .bind("Carol", "[email protected]"),
      db.prepare("SELECT * FROM users"),
    ]);

    expect(results[2].results).toHaveLength(2);
  });
});

Injecting into Workers

Pass the mock as your D1 binding when testing your worker:

import { D1Mock } from "cf-bun-mocks";
import type { Env } from "./worker";

const env: Env = {
  DB: new D1Mock(":memory:"),
};

// Test your worker with the mock env
const response = await worker.fetch(request, env);

Workers Environment

For testing code that imports from cloudflare:workers, use useWorkersMock() to mock the module and set up test environments:

import { describe, test, expect } from "bun:test";
import { useWorkersMock, D1Mock } from "cf-bun-mocks";

describe("worker with module imports", () => {
  const { env } = useWorkersMock(() => ({
    env: {
      DB: new D1Mock(":memory:"),
      API_KEY: "test-key",
    },
  }));

  test("uses mocked environment", async () => {
    const { env } = await import("cloudflare:workers");
    expect(env.DB).toBeDefined();
    expect(env.API_KEY).toBe("test-key");
  });
});

The mock environment is reset before each test with the values from your factory function.

Manual Injection (Alternative)

If you're not using cloudflare:workers imports, you can pass the environment directly to your worker:

import { describe, test, expect } from "bun:test";
import { D1Mock } from "cf-bun-mocks";
import type { Env } from "./worker";

describe("my worker", () => {
  test("uses test environment", async () => {
    const env: Env = {
      DB: new D1Mock(":memory:"),
      MY_SECRET: "test-secret",
    };

    const response = await worker.fetch(request, env);
    expect(response.status).toBe(200);
  });
});

API

D1Mock

Implements the full D1Database interface from @cloudflare/workers-types:

  • prepare(query: string) - Create a prepared statement
  • batch(statements: D1PreparedStatement[]) - Execute multiple statements
  • exec(query: string) - Execute raw SQL (supports multiple statements)
  • dump() - Serialize the database to an ArrayBuffer
  • withSession(constraint?) - Get a session (bookmark tracking is stubbed)

createD1Mock(migrationsPath: string)

Creates an in-memory D1Mock and runs all .sql migration files from the specified directory in sorted order.

useWorkersMock<TEnv>(createMock: () => Partial<{ env: TEnv }> | Promise<Partial<{ env: TEnv }>>)

Mocks the cloudflare:workers module and sets up test environments. Returns a mock object with an env property that gets updated before each test with values from the factory function. Uses Bun's mock.module() internally.

Requirements

  • Bun v1.0+
  • TypeScript 5+

License

MIT