{"id":13714805,"url":"https://github.com/morintd/prismock","last_synced_at":"2025-05-15T05:05:15.608Z","repository":{"id":43024115,"uuid":"510493496","full_name":"morintd/prismock","owner":"morintd","description":"A mock for PrismaClient, dedicated to unit testing.","archived":false,"fork":false,"pushed_at":"2025-04-05T09:49:26.000Z","size":3780,"stargazers_count":234,"open_issues_count":24,"forks_count":24,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-07T01:05:48.041Z","etag":null,"topics":["prisma","testing","typescript","unit-testing"],"latest_commit_sha":null,"homepage":"https://www.npmjs.com/package/prismock","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/morintd.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-07-04T20:22:39.000Z","updated_at":"2025-04-06T20:20:22.000Z","dependencies_parsed_at":"2024-01-04T20:28:42.511Z","dependency_job_id":"230857c4-9d52-4f01-8acb-ee68ff3e1221","html_url":"https://github.com/morintd/prismock","commit_stats":{"total_commits":1240,"total_committers":15,"mean_commits":82.66666666666667,"dds":"0.16935483870967738","last_synced_commit":"efe75b20116e63a31446d9ab3917fd995112981e"},"previous_names":[],"tags_count":79,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/morintd%2Fprismock","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/morintd%2Fprismock/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/morintd%2Fprismock/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/morintd%2Fprismock/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/morintd","download_url":"https://codeload.github.com/morintd/prismock/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248837274,"owners_count":21169373,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["prisma","testing","typescript","unit-testing"],"created_at":"2024-08-03T00:00:50.247Z","updated_at":"2025-04-14T06:51:04.140Z","avatar_url":"https://github.com/morintd.png","language":"TypeScript","funding_links":[],"categories":["TypeScript",":safety_vest: Community Prisma Tools"],"sub_categories":[],"readme":"# prismock\n\n[![npm](https://img.shields.io/npm/v/prismock)](https://www.npmjs.com/package/prismock)\n[![Build](https://circleci.com/gh/morintd/prismock.svg?style=shield)](https://app.circleci.com/pipelines/github/morintd/prismock)\n[![npm](https://img.shields.io/npm/dm/prismock)](https://www.npmjs.com/package/prismock)\n\nThis is a mock for `PrismaClient`. It actually reads your `schema.prisma` and generate models based on it.\n\nIt perfectly simulates Prisma's API and store everything in-memory for fast, isolated, and retry-able unit tests.\n\nIt's heavily tested, by comparing the mocked query results with real results from prisma. Tested environments include `MySQL`, `PostgreSQL` and `MongoDB`.\n\n\u003e This library can also be used as an in-memory implementation of Prisma, for reasons such as prototyping, but that's not its primary goal.\n\n# Installation\n\nAfter setting up [Prisma](https://www.prisma.io/docs/getting-started/setup-prisma/add-to-existing-project):\n\nyarn\n\n```sh\n$ yarn add -D prismock\n```\n\nnpm\n\n```\n$ npm add --save-dev prismock\n```\n\n# Usage\n\nThere are a few options here, depending on your application architecture.\n\n## Automatically (recommended)\n\nYou can create a `__mocks__` directory at the root of your project, with a sub-directory named `@prisma`. Inside the `@prisma` directory, create a `client.js` file (or `client.ts` for TypeScript).\n\nInside the `client` file, you can re-export the `@prisma/client` module, and replace `PrismaClient` by `PrismockClient`:\n\n```ts\nimport { PrismockClient } from 'prismock';\n\nexport * from '@prisma/client';\nexport { PrismockClient as PrismaClient };\n```\n\nThat's it, prisma will be mocked in all your tests (tested with Jest \u0026 ViTest)\n\n## PrismaClient\n\nYou can mock the PrismaClient directly in your test, or setupTests ([Example](https://github.com/morintd/prismock/blob/master/src/__tests__/example-prismock.test.ts)):\n\n```ts\njest.mock('@prisma/client', () =\u003e {\n  return {\n    ...jest.requireActual('@prisma/client'),\n    PrismaClient: jest.requireActual('prismock').PrismockClient,\n  };\n});\n```\n\n## Use prismock manually\n\nYou can instantiate a `PrismockClient` directly and use it in your test, or pass it to a test version of your app.\n\n```ts\nimport { PrismockClient } from 'prismock';\n\nimport { PrismaService } from './prisma.service';\n\nconst prismock = new PrismockClient();\nconst app = createApp(prismock);\n```\n\nThen, you will be able to write your tests as if your app was using an in-memory Prisma client.\n\n## Using custom client path\n\nIf you are using a custom [client path](https://www.prisma.io/docs/orm/prisma-client/setup-and-configuration/generating-prisma-client#using-a-custom-output-path), you need the [createPrismock](https://github.com/morintd/prismock/blob/master/docs/using-custom-client-path.md) method.\n\n## Use with decimal.js\n\nSee [use with decimal.js](https://github.com/morintd/prismock/blob/master/docs/use-with-decimal-js.md).\n\n## Internal data\n\nTwo additional functions are returned compared to the PrismaClient, `getData`, and `reset`. In some edge-case, we need to directly access, or reset, the data store management by _prismock_.\n\nMost of the time, you won't need it in your test, but keep in mind they exist\n\n```ts\nconst prismock = new PrismockClient();\nprismock.getData(); // { user: [] }\n```\n\n```ts\nconst prismock = new PrismockClient();\nprismock.reset(); // State of prismock back to its original\n```\n\n# Supported features\n\n## Model queries\n\n| Feature    | State                       |\n| ---------- | --------------------------- |\n| findUnique | ✔                           |\n| findFirst  | ✔                           |\n| findMany   | ✔                           |\n| create     | ✔                           |\n| createMany | ✔                           |\n| delete     | ✔                           |\n| deleteMany | ✔                           |\n| update     | ✔                           |\n| updateMany | ✔                           |\n| upsert     | ✔                           |\n| count      | ✔                           |\n| aggregate  | ✔                           |\n| groupBy    | 💬 [note](#groupby-support) |\n\n## Model query options\n\n| Feature           | State |\n| ----------------- | ----- |\n| distinct          | ✔     |\n| include           | ✔     |\n| where             | ✔     |\n| select            | ✔     |\n| orderBy (Partial) | ✔     |\n| select + count    | ⛔    |\n\n## Nested queries\n\n| Feature         | State |\n| --------------- | ----- |\n| create          | ✔     |\n| createMany      | ✔     |\n| update          | ✔     |\n| updateMany      | ✔     |\n| connect         | ✔     |\n| connectOrCreate | ✔     |\n| upsert          | ✔     |\n| set             | ⛔    |\n| disconnect      | ⛔    |\n| delete          | ⛔    |\n\n## Filter conditions and operators\n\n| Feature   | State |\n| --------- | ----- |\n| equals    | ✔     |\n| gt        | ✔     |\n| gte       | ✔     |\n| lt        | ✔     |\n| lte       | ✔     |\n| not       | ✔     |\n| in        | ✔     |\n| notIn     | ✔     |\n| contains  | ✔     |\n| startWith | ✔     |\n| endsWith  | ✔     |\n| AND       | ✔     |\n| OR        | ✔     |\n| NOT       | ✔     |\n| mode      | ✔     |\n| search    | ⛔    |\n\n## Relation filters\n\n| Feature | State |\n| ------- | ----- |\n| some    | ✔     |\n| every   | ✔     |\n| none    | ✔     |\n| is      | ✔    |\n\n## Scalar list methods\n\n| Feature | State |\n| ------- | ----- |\n| set     | ⛔    |\n| push    | ✔    |\n\n## Scalar list filters\n\n| Feature  | State |\n| -------- | ----- |\n| has      | ⛔    |\n| hasEvery | ⛔    |\n| hasSome  | ⛔    |\n| isEmpty  | ⛔    |\n| equals   | ⛔    |\n\n## Atomic number operations\n\n| Feature   | State |\n| --------- | ----- |\n| increment | ✔     |\n| decrement | ✔     |\n| multiply  | ✔     |\n| divide    | ✔     |\n| set       | ✔     |\n\n## JSON filters\n\n| Feature             | State |\n| ------------------- | ----- |\n| path                | ⛔    |\n| string_contains     | ⛔    |\n| string_starts_withn | ⛔    |\n| string_ends_with    | ⛔    |\n| array_contains      | ⛔    |\n| array_starts_with   | ⛔    |\n| array_ends_with     | ⛔    |\n\n## Attributes\n\n| Feature    | State |\n| ---------- | ----- |\n| @@id       | ✔     |\n| @default   | ✔     |\n| @relation  | ✔     |\n| @unique    | ⛔    |\n| @@unique   | ✔     |\n| @updatedAt | ⛔    |\n\n## Attribute functions\n\n| Feature         | State |\n| --------------- | ----- |\n| autoincrement() | ✔     |\n| now()           | ✔     |\n| uuid()          | ✔     |\n| auto()          | ✔     |\n| cuid()          | ✔     |\n| dbgenerated     | ⛔    |\n\n## Referential actions\n\n| Feature                                     | State |\n| ------------------------------------------- | ----- |\n| onDelete (SetNull, Cascade)                 | ✔     |\n| onDelete (Restrict, NoAction, SetDefault)() | ⛔    |\n| onUpdate                                    | ⛔    |\n\n## Notes\n\n### groupBy Support\n\nBasic groupBy queries are supported, including `having` and `orderBy`. `skip`, `take`, and `cursor` are not yet supported.\n\n# Roadmap\n\n- Complete supported features.\n- Refactoring of update operation.\n- Replace item formatting with function composition\n- Restore test on `_count` for mongodb\n- Add custom client method for MongoDB (`$runCommandRaw`, `findRaw`, `aggregateRaw`)\n\n# Motivation\n\nWhile _Prisma_ is amazing, its `unit testing` section is treated as optional. On the other hand, it should be a priority for developers to write tests.\n\nAs I love _Prisma_, I decided to create this package, in order to keep using it on real-world projects.\n\nI'm also a teacher and believe it's mandatory for students to learn about testing. I needed a similar solution for my [backend course](https://www.scalablebackend.com/), so I created my own.\n\n# Feature request\n\nI'm personally using this library in my day-to-day activities, and add features or fix bugs depending on my needs.\n\nIf you need unsupported features or discover unwanted behaviors, feel free to open an issue, I'll take care of it.\n\n# Credit\n\nInspired by [prisma-mock](https://github.com/demonsters/prisma-mock).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmorintd%2Fprismock","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmorintd%2Fprismock","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmorintd%2Fprismock/lists"}