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.
npm install prisma-mockVerified import paths — ran on the pinned version, not inferred.
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.
Review the `createPrismaMock` signature and options in the `v1.0.0` release notes and README. Update argument passing, especially if custom DMMF or initial data was used. Adapt any test logic that might be affected by default indexing.
Update imports from `prisma-mock/legacy` to either `prisma-mock` or `prisma-mock/client` based on your Prisma version and adjust initialization as per the updated API.
Ensure your `schema.prisma` is configured with a DMMF generator and run `prisma generate`. Import `Prisma` from your generated client path and the DMMF object (e.g., `import * as dmmf from "./generated/dmmf"`), then pass them to `createPrismaMock(Prisma, { datamodel: dmmf })`.Follow the example in the README for mocking a global Prisma instance using `jest-mock-extended` or `vitest-mock-extended`, ensuring `mockReset` is called `beforeEach` test to prevent state pollution.
For Prisma 7+, ensure you have configured and run the DMMF generator in your `schema.prisma`. Then, import `* as dmmf from './generated/dmmf'` (or similar path) and pass it as `createPrismaMock(Prisma, { datamodel: dmmf })`.For ESM projects, use `import createPrismaMock from 'prisma-mock'` or `import createPrismaMock from 'prisma-mock/client'`. If in a CommonJS environment, ensure your project setup correctly handles ESM imports, or verify the package's export map configuration.
Ensure you are importing `Prisma` from your generated Prisma client (e.g., `import { Prisma } from "./generated/client";`) and passing it as the first argument to `createPrismaMock`.