{"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.","language":"javascript","status":"active","last_verified":"Wed Apr 22","install":{"commands":["npm install prisma-mock"],"cli":null},"imports":["import createPrismaMock from \"prisma-mock\";","import createPrismaMock from \"prisma-mock/client\";","import { Prisma } from \"./generated/client\";","import * as dmmf from \"./generated/dmmf\";"],"auth":{"required":false,"env_vars":[]},"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.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}