使用 Prisma 创建测试数据库

huangapple go评论43阅读模式
英文:

Creating a test database with Prisma

问题

我正在使用 vitest 来测试我的 SvelteKit + Prisma 应用程序,后端使用 SQLite。对于一些测试,仅仅模拟 Prisma 是不够的,我需要填充一个测试数据库以进行测试数据。

使用 Prisma 来创建测试数据库的正确方法是什么?我的模式指定了数据源:

datasource db {
  provider = "sqlite"
  url = env("DATABASE_URL")
}

我是否可以以某种方式覆盖它,然后在运行测试套件之前运行 npx prisma db push(或迁移)?

英文:

I'm using vitest to test my sveltekit+Prisma app with sqlite backend. For some tests, mocking Prisma is not enough and I need to populate a test database with test data.

What is the proper approach of using Prisma to create a test database? My schema specifies the data source:

datasource db {
  provider = "sqlite"
  url = env("DATABASE_URL")
}

Can I somehow override that with another url, and run npx prisma db push (or migrate) before running the test suite?

答案1

得分: 4

以下是您要翻译的内容:

我曾经遇到过相同的问题,我正在使用nestjs,最终使用环境文件进行测试。
类似于这样:

"test:e2e": "env-cmd -f .env.test npx prisma db push";

在模式中,我这样使用它:

generator client {
provider = "prisma-client-js";
output = env("output")
}

datasource db {
provider = "mysql";
url = env("DATABASE_URL")
}

客户端中的输出指定了在哪里生成 prisma 客户端。
在 .env.test 文件中,我放入了测试数据库 URL。

英文:

I have run into sane issue once, I was using nestjs and I ended up using env file for testing.
it was something like this:

  "test:e2e": "env-cmd -f .env.test npx prisma db push"

and in the schema I used it :

generator client {
    provider        = "prisma-client-js"
    output          = env("output")
}

datasource db {
    provider = "mysql"
    url      = env("DATABASE_URL")
}

the output in client to specifies where to generate the prisma client.
in the .env.test I have put the testing db URL.

huangapple
  • 本文由 发表于 2023年2月17日 23:31:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/75486253.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定