{"id":13660599,"url":"https://github.com/prisma/prisma-test-utils","last_synced_at":"2025-04-24T19:31:27.828Z","repository":{"id":37733409,"uuid":"190009004","full_name":"prisma/prisma-test-utils","owner":"prisma","description":"A collection of data model agnostic test utils.","archived":true,"fork":false,"pushed_at":"2023-07-18T20:56:37.000Z","size":2502,"stargazers_count":114,"open_issues_count":52,"forks_count":11,"subscribers_count":23,"default_branch":"main","last_synced_at":"2025-04-20T11:00:03.048Z","etag":null,"topics":["prisma-test-utils"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/prisma.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2019-06-03T13:17:07.000Z","updated_at":"2024-11-18T01:10:15.000Z","dependencies_parsed_at":"2024-01-12T02:14:15.586Z","dependency_job_id":null,"html_url":"https://github.com/prisma/prisma-test-utils","commit_stats":{"total_commits":158,"total_committers":8,"mean_commits":19.75,"dds":0.08227848101265822,"last_synced_commit":"830f01d5d0d6a6c3f5783049b2cc76c5ecee8212"},"previous_names":[],"tags_count":14,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/prisma%2Fprisma-test-utils","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/prisma%2Fprisma-test-utils/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/prisma%2Fprisma-test-utils/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/prisma%2Fprisma-test-utils/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/prisma","download_url":"https://codeload.github.com/prisma/prisma-test-utils/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250615450,"owners_count":21459495,"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-test-utils"],"created_at":"2024-08-02T05:01:23.498Z","updated_at":"2025-04-24T19:31:22.818Z","avatar_url":"https://github.com/prisma.png","language":"TypeScript","funding_links":[],"categories":["TypeScript"],"sub_categories":[],"readme":"# 🃏 prisma-test-utils\n\n[![npm version](https://badge.fury.io/js/prisma-test-utils.svg)](https://badge.fury.io/js/prisma-test-utils)\n[![CircleCI](https://circleci.com/gh/prisma/prisma-test-utils/tree/main.svg?style=shield)](https://circleci.com/gh/prisma/prisma-test-utils/tree/main)\n[![codecov](https://codecov.io/gh/prisma/prisma-test-utils/branch/main/graph/badge.svg)](https://codecov.io/gh/prisma/prisma-test-utils)\n\n\u003e ⚠️ **This project is temporarily unmaintained. See [this issue](https://github.com/prisma/prisma-test-utils/issues/6).**\n\nIn testing workflows, generating seed data usually includes a lot of boilerplate and hardcoded fixtures that need to be migrated with changing code.\n\n`prisma-test-utils` solves this by generating test util functions based on your Prisma Schema. As your application evolves, the generated data also evolves deterministically.\n\n## Features\n\n- 🙈 **Data model agnostic:** Optimised for you datamodel.\n- 🦑 **Flexible:** Cherry picked default settings.\n- 🐶 **Out-of-the-box usage:** Plug-in generator for your Prisma Schema.\n- 🐠 **Seeds mock data:** Populates your database with mock data.\n- 🦋 **Per-test database:** Creates an isolated database for each test.\n\n## Installation\n\nTBD\n\n## Configuration\n\n```prisma\ngenerator testutils {\n  provider = \"prisma-test-utils\"\n  output = \"node_modules/@generated/prisma-test-utils\"\n}\n```\n\n## Usage\n\n`prisma-test-utils` packs two incredibly useful functions. The first one, `seed`, helps you populate your data with vast amount of data. The second one, `pool`, can be used to create a pool of databases that you can use during testing, and are wiped after you've finished.\n\n### Seeding\n\n```ts\nimport Photon from '@generated/photon'\nimport seed from '@generated/test-utils/seed'\n\ntest('test with seed data', async () =\u003e {\n  await seed({\n    client,\n    models: kit =\u003e ({\n      _: {\n        /* Default number of instances. */\n        amount: 500,\n      },\n      Blog: {\n        factory: {\n          /* Use functions from the kit. */\n          name: kit.faker.sentence,\n          /* Define custom mocks. */\n          description: 'My custom blog description',\n          /* Define custom mock functions. */\n          entry: () =\u003e {\n            return `A generated entry from the function.`\n          },\n          /* Manage relations. */\n          posts: {\n            max: 100,\n          },\n        },\n      },\n    }),\n  })\n\n  const blogs = await client.blogs()\n})\n```\n\n**Options**\n\nIt is possible to selectively override the seed generation making the seeding workflow very flexible.\n\n\u003e All options are autogenerated and checked at compile time. You'll be warned about any relation constraints that your datamodel presents.\n\n```ts\nbeforeAll(async () =\u003e {\n  const data = await seed(\n    photon,\n    bag =\u003e ({\n      Post: {\n        amount: 5,\n        factory: {\n          published: 'false',\n        },\n      },\n    }),\n    {\n      seed: 42,\n      silent: false,\n      instances: 5,\n    },\n  )\n})\n```\n\n### Database Pools\n\nWe can configure our pool requirements before running any test cases.\n\n```js\nimport SQLitePool, { Pool } from '@generated/prisma-test-utils'\n\nlet pool: Pool\n\nbeforeAll(async () =\u003e {\n  pool = new SQLitePool({\n    pool: {\n      min: 3,\n      max: 5,\n    },\n  })\n})\n```\n\nThis allows us to request an isolated database per test case\n\n```ts\ntest('one of my parallel tests', async () =\u003e {\n  /* Acquire new db instance. */\n  const db = await pool.getDBInstance()\n\n  // Write the test case logic\n  const client = new Photon({\n    datasources: {\n      db: db.url,\n    },\n  })\n\n  /* Release the instance. */\n  client.disconnect()\n  pool.releaseDBInstance(db)\n})\n```\n\n**API**\n\n```ts\n/* All pool instances. */\n\nclass Pool {\n  async getDBInstance(): Promise\u003cDBInstance\u003e\n  async releaseDBInstance(db: DBInstance): Promise\u003cvoid\u003e\n  async run\u003cT\u003e(fn: (db: DBInstance) =\u003e Promise\u003cT\u003e): Promise\u003cT\u003e\n  async drain(): Promise\u003cvoid\u003e\n}\n\n/* PostgreSQL */\n\ninterface PostgreSQLConnection {\n  host: string\n  port: number\n  user: string\n  password?: string\n  database: string\n  schema: string\n}\n\ninterface PostgreSQLPoolOptions {\n  connection: (id: string) =\u003e PostgreSQLConnection\n  pool?: {\n    max?: number\n  }\n}\n\n/* MySQL */\n\ninterface MySQLConnection {\n  host: string\n  port: string\n  user: string\n  password?: string\n  database: string\n}\n\ninterface MySQLPoolOptions {\n  connection: (id string) =\u003e MySQLConnection\n  pool?: {\n    max?: number\n  }\n}\n\n/* SQLite */\n\ninterface SQLitePoolOptions {\n  databasePath: (id: string) =\u003e string\n  pool?: {\n    max?: number\n  }\n}\n```\n\n## Local development\n\n\u003e :construction: NOTE: Please comment your work and read the comments that are already in there.\n\nI didn't want to remove half the files of this library - the pool part - and that's why there's more files than you'll usually need for developing seed utils. Please don't remove the extra files as this work very nicely the way it is.\n\nThe most important file for seeding is `src/static/seed.ts` and `src/intellisense/seed.ts`. The first one is the logic and the second one provides customized types.\n\nFurthermore:\n\n- **To create a new DB instance:** Spin up the `docker-compose up -d` and use TablePlus or alternative to import the sql.\n- **To examine the behaviour of the library:** Uncomment `src/__test` file and start the debugger. `src/__test` file references files in the `tests/seed` folder. Read on about that!\n- **To setup `tests/seed` folder:** Navigate to that directory and use `yarn prisma2 \u003ccmd\u003e` to setup everything that you need. I usually use one of these functions:\n\n  - `yarn prisma2 introspect yarn prisma2 introspect --url=\"postgresql://prisma:prisma@127.0.0.1/ruma\"`\n  - `yarn prisma2 migrate up --experimental`\n  - `yarn prisma2 migrate save --name \"init\" --experimental`\n  - `yarn prisma2 generate`.\n    I have also added the `README.md` file in there with missing generator definitions from introspection. Copy and paste them to the top.\n\n- **To push changes:** Preferablly do a PR, and don't forget to comment out `src/__test` file.\n- **To publish a new version:** I use `npx np --no-tests`.\n- **To apply changes in the `intellisense`:** Run `yarn build`.\n- **To test the utils outside debugger:** Run `yarn build:runtime`.\n- **To get new VSCode type definitions after changing the schema:** Reload VSCode :slightly_smiling_face:\n\n## Security\n\nIf you have a security issue to report, please contact us at [security@prisma.io](mailto:security@prisma.io?subject=[GitHub]%20Prisma%202%20Security%20Report%20Test%20Utils)\n\n## LICENSE\n\nMIT @ Prisma\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fprisma%2Fprisma-test-utils","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fprisma%2Fprisma-test-utils","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fprisma%2Fprisma-test-utils/lists"}