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

mock-drizzle-orm

v1.0.0

Published

Mock Drizzle ORM for testing - never hit the database again.

Readme

mock-drizzle-orm

Never hit the database again while testing Drizzle ORM.

Mock for unit tests against PostgreSQL, MySQL, and SQLite (async + sync). Works with any Node test runner (Vitest, Jest, Mocha, Node test, …) — the package has no Jest/Vitest/Sinon runtime dependency.

Zero refactoring required: new MockDrizzle() intercepts await and .execute() on your existing singleton db, including db.transaction().

See a full Vitest dogfood app in examples/vitest-pg-app (npm run test:example).

Install

npm install --save-dev mock-drizzle-orm
# peer
npm install drizzle-orm

Ships both ESM and CJS (import / require). Requires Node >= 18.

Lifecycle

describe("suite", () => {
  let mock: MockDrizzle;

  beforeEach(() => {
    mock = new MockDrizzle();
  });

  afterEach(() => {
    mock.restore(); // remove global intercept
  });
});

Use resetAll() between cases if you keep one instance for the whole file; prefer restore() when tearing down.

Quick start (zero-refactor)

Keep your production db module as-is:

// db.ts
import { drizzle } from "drizzle-orm/node-postgres";
export const db = drizzle(process.env.DATABASE_URL!, { schema });

In tests:

import { MockDrizzle } from "mock-drizzle-orm";
import { db } from "./db";
import { accounts } from "./schema";

const mock = new MockDrizzle();

mock
  .onMock(accounts)
  .select()
  .toReturn([{ id: 1, userId: 10, balance: 100 }])
  .toReturn([{ id: 2, userId: 20, balance: 50 }]);
mock.onMock(accounts).update().toReturn([]).toReturn([]);

const result = await db.transaction(async (tx) => {
  // your real service logic using tx / db
});

mock.onMock(accounts).update().assertCalledTimes(2);
mock.restore();

MySQL / SQLite singletons work the same way (drizzle-orm/mysql-proxy, drizzle-orm/sqlite-proxy, drizzle-orm/better-sqlite3, etc.).

For sync SQLite (better-sqlite3), use .all() / .get() / .run() and sync transaction — no await required:

mock
  .onMock(users)
  .select()
  .toReturn([{ id: 1, name: "Alice" }]);
const rows = db.select().from(users).all();
db.transaction((tx) => tx.select().from(users).all());

Method-first mocking

mock.onMock(users).select().toReturn(data);
mock.onMock(users).insert().toReturn(data);
mock.onMock(users).update().toReturn(data);
mock.onMock(users).delete().toReturn(data);

// or by table name
mock.onMock("users").select().toReturn(data);

Queue multiple returns (FIFO):

mock
  .onMock(users)
  .select()
  .toReturn([{ id: 1 }])
  .toReturn([{ id: 2 }]);

Param matching

Optionally require toSQL().params to match:

mock
  .onMock(users)
  .select()
  .withParams([1])
  .toReturn([{ id: 1, name: "Alice" }]);
mock
  .onMock(users)
  .select()
  .toReturn([{ id: 2 }]); // fallback when params do not match

Direct .execute()

await db.select().from(users).execute();

is intercepted the same as await db.select().from(users).

Relational queries

db.query.users.findMany() / findFirst() use the same select queue:

mock
  .onMock(users)
  .select()
  .toReturn([{ id: 1, name: "Alice" }]);
await db.query.users.findMany();

mock.onMock(users).select().toReturn({ id: 1, name: "Alice" });
await db.query.users.findFirst();

Transactions

db.transaction(async (tx) => { ... }) is mocked for PG, MySQL, and SQLite: no real BEGIN/COMMIT. Queries inside the callback are intercepted like normal awaits. Nested tx.transaction(...) is supported without savepoints. tx.rollback() throws Drizzle’s dialect rollback error.

Assertions

await db.select().from(users);

mock.onMock(users).select().assertCalled();
mock.onMock(users).select().assertCalledTimes(1);
mock.onMock(users).select().getCalls(); // [{ table, operation, sql, params }, ...]

Also available: mock.getCalls() for all operations.

Reset and restore

mock.onMock(users).select().reset(); // one operation queue
mock.onMock(users).reset(); // one table
mock.resetAll(); // clear queues; keep intercept
mock.restore(); // remove intercept + clear queues

Optional: explicit mock db (DI)

const mock = new MockDrizzle();
const db = mock.createDb({ schema: { users } }); // dialect defaults to 'pg'
const mysqlDb = mock.createDb({ dialect: "mysql", schema: { users } });
const sqliteDb = mock.createDb({ dialect: "sqlite", schema: { users } }); // async
const sqliteSyncDb = mock.createDb({ dialect: "sqlite-sync", schema: { users } }); // better-sqlite3-style

How it works

new MockDrizzle() patches:

  • QueryPromise.prototype.then and dialect *SelectBase.prototype.then (select copies then via mixin)
  • Builder _prepare (so .execute() / .prepare().execute() resolve mocks)
  • PgDatabase / MySqlDatabase / BaseSQLiteDatabase .transaction

On await or execute, the package reads toSQL(), matches table + operation (+ optional params), and returns queued data. createDb() remains available for an isolated mock session.

Lint & format

npm run lint        # oxlint
npm run lint:fix   # auto-fix where safe
npm run fmt         # oxfmt write
npm run fmt:check   # CI check

Limitations

  • Matching is by primary table + operation (+ optional params); join-aware routing and full where AST matching are not implemented
  • Transaction config (isolation level, etc.) is ignored
  • Hoist a single drizzle-orm version so the patched prototypes match your app

License

MIT