{"id":16503,"library":"prisma-mock","title":"Prisma Mock","description":"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.","status":"active","version":"1.1.0","language":"javascript","source_language":"en","source_url":"https://github.com/demonsters/prisma-mock","tags":["javascript","typescript"],"install":[{"cmd":"npm install prisma-mock","lang":"bash","label":"npm"},{"cmd":"yarn add prisma-mock","lang":"bash","label":"yarn"},{"cmd":"pnpm add prisma-mock","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Peer dependency, required for type definitions and to accurately mimic the Prisma API structure.","package":"@prisma/client","optional":false}],"imports":[{"note":"This is the default export, recommended for Prisma versions 6 and below. It automatically uses the Prisma client from `@prisma/client/default`.","wrong":"const createPrismaMock = require(\"prisma-mock\");","symbol":"createPrismaMock","correct":"import createPrismaMock from \"prisma-mock\";"},{"note":"Use this entry point for Prisma v7 and newer. It requires explicitly passing the `Prisma` namespace and a generated `dmmf` object for proper mock initialization.","wrong":"const createPrismaMock = require(\"prisma-mock/client\");","symbol":"createPrismaMock","correct":"import createPrismaMock from \"prisma-mock/client\";"},{"note":"This represents the Prisma namespace from your generated client, necessary when using `prisma-mock/client` for Prisma v7+ to provide type information. The import path should match your `schema.prisma` output configuration.","symbol":"Prisma","correct":"import { Prisma } from \"./generated/client\";"},{"note":"This imports the Data Model Meta Format (DMMF) object, which `prisma-mock/client` uses for understanding your Prisma schema in Prisma v7+. Ensure you have a DMMF generator configured in your `schema.prisma`.","symbol":"dmmf","correct":"import * as dmmf from \"./generated/dmmf\";"}],"quickstart":{"code":"import createPrismaMock from \"prisma-mock/client\";\nimport { Prisma } from \"./generated/client\"; // Adjust path based on your Prisma client generation\nimport * as dmmf from \"./generated/dmmf\"; // Ensure DMMF generator is configured and run\n\nlet client: ReturnType<typeof createPrismaMock>;\n\nbeforeEach(() => {\n  // Initialize the mock client before each test\n  client = createPrismaMock(Prisma, {\n    datamodel: dmmf,\n    // Optionally start with some initial data\n    data: {\n      user: [\n        { id: 1, name: 'Initial User', email: 'initial@example.com' }\n      ]\n    }\n  });\n  // Clear any state from previous tests\n  client.$clear();\n});\n\ndescribe('User operations', () => {\n  it('should create a new user', async () => {\n    const newUser = await client.user.create({\n      data: {\n        id: 2,\n        name: 'Alice Smith',\n        email: 'alice@example.com',\n      },\n    });\n    expect(newUser).toEqual({ id: 2, name: 'Alice Smith', email: 'alice@example.com' });\n\n    const userCount = await client.user.count();\n    expect(userCount).toBe(2); // Includes initial user\n  });\n\n  it('should find a user by ID', async () => {\n    // Create a user specifically for this test if not pre-seeded\n    await client.user.create({ data: { id: 3, name: 'Bob Johnson', email: 'bob@example.com' } });\n    const foundUser = await client.user.findUnique({ where: { id: 3 } });\n    expect(foundUser?.name).toBe('Bob Johnson');\n  });\n\n  it('should update an existing user', async () => {\n    const updatedUser = await client.user.update({\n      where: { id: 1 },\n      data: { name: 'Updated Initial User' },\n    });\n    expect(updatedUser?.name).toBe('Updated Initial User');\n  });\n});\n","lang":"typescript","description":"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."},"warnings":[{"fix":"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.","message":"The `v1.0.0` release introduced significant breaking changes to the API interface, requiring a review of how `createPrismaMock` is called. It also enabled indexing by default for performance improvements, which might subtly alter behavior if previous versions relied on unindexed behavior.","severity":"breaking","affected_versions":">=1.0.0"},{"fix":"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.","message":"The `prisma-mock/legacy` export is deprecated. While currently maintained for backward compatibility, new projects and refactors should migrate to the recommended `prisma-mock` (for Prisma 6 and below) or `prisma-mock/client` (for Prisma 7+) entry points.","severity":"deprecated","affected_versions":">=1.0.0"},{"fix":"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 })`.","message":"For Prisma v7 and above, `prisma-mock/client` must be used. This entry point explicitly requires the `Prisma` namespace and a `dmmf` object (generated by a DMMF generator) to be passed during initialization. Failure to provide these or properly configure the DMMF generator will lead to runtime errors.","severity":"gotcha","affected_versions":">=1.0.1"},{"fix":"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.","message":"When mocking a global Prisma client instance (e.g., a default export in a `db` module), it's crucial to correctly set up your mocking library (e.g., `jest.mock`) and ensure `mockDeep` and `mockReset` are used with `createPrismaMock({ mockClient: db })`. Incorrect setup can lead to state leakage between tests or unexpected behavior.","severity":"gotcha","affected_versions":"*"}],"env_vars":null,"last_verified":"2026-04-22T00:00:00.000Z","next_check":"2026-07-21T00:00:00.000Z","problems":[{"fix":"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 })`.","cause":"`createPrismaMock` was called without the `datamodel` option when using `prisma-mock/client` for Prisma 7+, or the `dmmf` object was incorrectly imported or passed.","error":"Error: Cannot read properties of undefined (reading 'datamodel')"},{"fix":"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.","cause":"This typically occurs when attempting to `require` the `prisma-mock` or `prisma-mock/client` package in an ESM context, or due to an incorrect import path.","error":"TypeError: createPrismaMock is not a function"},{"fix":"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`.","cause":"When using `prisma-mock/client` (required for Prisma 7+), the `Prisma` namespace was not imported or passed correctly as the first argument to `createPrismaMock`.","error":"ReferenceError: Prisma is not defined"}],"ecosystem":"npm"}