Prisma Mock

1.1.0 · active · verified Wed Apr 22

prisma-mock is an in-memory mocking library designed for unit testing applications that interact with Prisma ORM. It fully simulates the Prisma API, allowing developers to execute database-dependent unit tests rapidly without requiring an actual database connection or external dependencies. The current stable version is 1.1.0, with an active release cadence involving frequent patch and minor updates to support new Prisma versions and features. A key differentiator is its in-memory data storage, which ensures fast and reliable test execution. It offers optional integration with `jest-mock-extended` or `vitest-mock-extended` for enhanced mocking capabilities and provides dedicated entry points (`prisma-mock` vs. `prisma-mock/client`) to accommodate different Prisma client versions. The library also supports initializing the mock client with pre-filled data and includes methods like `$clear()` and `$setInternalState()` for robust state management across tests, making it a comprehensive tool for isolating database logic during testing.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize `prisma-mock` for Prisma 7+ projects, including setting up the `Prisma` namespace and DMMF. It shows basic `create`, `findUnique`, and `update` operations within a typical testing environment (e.g., Jest or Vitest), along with state clearing and optional initial data seeding.

import createPrismaMock from "prisma-mock/client";
import { Prisma } from "./generated/client"; // Adjust path based on your Prisma client generation
import * as dmmf from "./generated/dmmf"; // Ensure DMMF generator is configured and run

let client: ReturnType<typeof createPrismaMock>;

beforeEach(() => {
  // Initialize the mock client before each test
  client = createPrismaMock(Prisma, {
    datamodel: dmmf,
    // Optionally start with some initial data
    data: {
      user: [
        { id: 1, name: 'Initial User', email: 'initial@example.com' }
      ]
    }
  });
  // Clear any state from previous tests
  client.$clear();
});

describe('User operations', () => {
  it('should create a new user', async () => {
    const newUser = await client.user.create({
      data: {
        id: 2,
        name: 'Alice Smith',
        email: 'alice@example.com',
      },
    });
    expect(newUser).toEqual({ id: 2, name: 'Alice Smith', email: 'alice@example.com' });

    const userCount = await client.user.count();
    expect(userCount).toBe(2); // Includes initial user
  });

  it('should find a user by ID', async () => {
    // Create a user specifically for this test if not pre-seeded
    await client.user.create({ data: { id: 3, name: 'Bob Johnson', email: 'bob@example.com' } });
    const foundUser = await client.user.findUnique({ where: { id: 3 } });
    expect(foundUser?.name).toBe('Bob Johnson');
  });

  it('should update an existing user', async () => {
    const updatedUser = await client.user.update({
      where: { id: 1 },
      data: { name: 'Updated Initial User' },
    });
    expect(updatedUser?.name).toBe('Updated Initial User');
  });
});

view raw JSON →