Quramy / jest-prisma

Jest environment for integrated testing with Prisma client

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Is jest-prisma work with transaction?

YutaUra opened this issue · comments

Expected

The following tests succeed

const transaction = async (prisma) => {
  await prisma.$transaction(async (p) => {
    await p.user.create({ data: { /* */ } })

    throw new Error("Something failed. Affected changes will be rollback.")
  }).catch(console.error)
}

// test code
it("test", async () => {
  const prisma = jestPrisma.client
  
  const before = await prisma.user.aggregate({ _count: true })
  expect(before._count).toBe(0)

  await transaction(prisma)

  const after = await prisma.user.aggregate({ _count: true })
  expect(after._count).toBe(0) // Prisma is expected to rollback, so there should be no increase in users
})

Actual

expect(received).toBe(expected)

Expected: 0
Received: 1

Reproduction

Please check the code I forked.

https://github.com/YutaUra/jest-prisma/blob/reproducing_transaction_bug/examples/example-prj/src/service/Transaction.ts
https://github.com/YutaUra/jest-prisma/blob/reproducing_transaction_bug/examples/example-prj/src/service/Transaction.test.ts

Others

Is the unavailability of Transaction a bug or a specification?

If it is a specification at this time, I think documentation needs to be added. If so, I would be happy to help.

jest-prisma work with transaction?

Partially yes. But jest-prisma can't throw rolledback exception in application $transaction

I have worked on adding the document for now. #89
I hope you will check it out when you have time!

If I come up with any ideas on how to implement transaction rollback, I will share them with you!