@chax-at/ 交易 - prisma - 测试
此软件包提供了在单个数据库交易中运行测试大小写的一个简单方法 每次测试后都要回滚 这允许快速测试执行,同时仍然为每次测试提供相同的源数据库状态. 它还允许对同一数据库进行并行测试执行.
先决条件
使用量
通过运行安装软件包
npm i -D @chax-at/transactional-prisma-testing
请注意,这将将包装为 Dev 依赖性,仅用于测试(删除 -D 若你想在测试之外使用).
示例
以下例子说明: 网点JS 项目显示如何使用此软件包.
然而,使用这个包与每个测试框架是可能的,只需查看下面的文件并相应修改代码.
你可以简单地替换 PrismaService 与 PrismaClient 如果你没有使用NestJS.
import { PrismaTestingHelper } from '@chax-at/transactional-prisma-testing';
// Cache for the PrismaTestingHelper. Only one PrismaTestingHelper should be instantiated per test runner (i.e. only one if your tests run sequentially).
let prismaTestingHelper: PrismaTestingHelper<PrismaService> | undefined;
// Saves the PrismaService that will be used during test cases. Will always execute queries on the currently active transaction.
let prismaService: PrismaService;
// This function must be called before every test
async function before(): Promise<void> {
if(prismaTestingHelper == null) {
// Initialize testing helper if it has not been initialized before
const originalPrismaService = new PrismaService(); // in newer prisma versions, you may have to pass an adapter here
// Seed your database / Create source database state that will be used in each test case (if needed)
// ...
prismaTestingHelper = new PrismaTestingHelper(originalPrismaService);
// Save prismaService. All calls to this prismaService will be routed to the currently active transaction
prismaService = prismaTestingHelper.getProxyClient();
}
await prismaTestingHelper.startNewTransaction();
}
// This function must be called after every test
function after(): void {
prismaTestingHelper?.rollbackCurrentTransaction();
}
// NestJS specific code: Replace the original PrismaService when creating a testing module
// Note that it is possible to cache this result and use the same module for all tests. The prismaService will automatically route all calls to the currently active transaction
function getMockRootModuleBuilder(): TestingModuleBuilder {
return Test.createTestingModule({
imports: [AppModule],
}).overrideProvider(PrismaService)
.useValue(prismaService);
}
棱镜测试 帮助
这个 PrismaTestingHelper 为棱镜客户端提供代理,并管理此代理.
构造器( 主机: T)
创建单个 PrismaTestingHelper 每个测试跑者(或如果测试按顺序执行的单个全局跑者).
构造参数为原始 PrismaClient 用于开始交易.
请注意,可以使用任何扩展 PrismaClient 的对象,例如 a NestJS 棱镜服务。 。 。 .
棱镜交易客户端上不存在的所有方法都会被路由到这个原始对象(除了 $transaction 电话).
获取ProxyClient (): T
此方法返回 a 代理服务器 到 PrismaClient 执行交易中的所有呼叫. 您可以保存并缓存此引用, 所有调用都会在最新的交易中执行 . 这允许您使用代理客户端而不是普通客户端启动您的应用程序 然后执行所有测试大小写,所有的呼叫都会被传送到最新的交易中.
通常只需一次取出,然后在任何地方都使用这个参考.
启动新交易( 选择 ? ): {超时 ?: 数; 最大等待 : 数} : 保证< 无效>
开始新的交易。 必须在每次测试前调用(并且应该在代理客户端上的任何查询执行之前调用).
您可以提供超时值 - 注意交易 timeout 一定足够长,所以你
将在此暂停期间执行整个测试例 .
你必须打电话 rollbackCurrentTransaction 在再次使用这种方法之前.
回滚当前交易( I): 无效
结束当前活动交易。 每次测试后必须调用, 才能开始新的交易 .
限制/洞穴
- 流利的 API 不被支持.
- 交易回滚时不会重设序列(自动递增ID). 如果你在测试中需要特定的身份证明,你可以 使用 SETVAL 重置所有序列 在每次测试之前.
@default(now())在您的计划( 例如:createdAt日期)或类似功能(如:.CURRENT_TIMESTAMP()在 PostgreSQL 中,将总是使用 交易开始 时间戳。 因此,所有createdAt- 时间戳在测试期间将具有相同的价值(因为它们是在同一个交易中执行的)。 如果这种行为有问题(例如,因为您想要在创建日期前找到最新的条目),那么您可以使用@default(dbgenerated("statement_timestamp()"))改为@default(now())(如果您不依赖默认)createdAt= 交易开始时的时间而不是声明行为)- 通过使用 邮递SQL 保存点。 。 。 .
- 如果您正在使用并行交易( 例如 ) .
await Promise.all(/* Multiple calls that will each start transactions */);然后他们就会 自动被相继执行(否则, Savepoints 将无法工作). 您无需更改代码即可工作 .
- 如果您正在使用并行交易( 例如 ) .