P
Prisma7h ago
SAF

What's the purpose of this unit test?

I'm looking into testing database operations so I'm reading this blog post on unit testing. The first unit test written is this:
// src/quotes/tags.service.test.ts
describe('tags.service', () => {
beforeEach(() => {
vi.restoreAllMocks()
})
describe('upsertTags', () => {
it('should return a list of tagIds', async () => {
prismaMock.$transaction.mockResolvedValueOnce([1, 2, 3])
const tagIds = await TagService.upsertTags(['tag1', 'tag2', 'tag3'])
expect(tagIds).toStrictEqual([1, 2, 3])
})
})
})
// src/quotes/tags.service.test.ts
describe('tags.service', () => {
beforeEach(() => {
vi.restoreAllMocks()
})
describe('upsertTags', () => {
it('should return a list of tagIds', async () => {
prismaMock.$transaction.mockResolvedValueOnce([1, 2, 3])
const tagIds = await TagService.upsertTags(['tag1', 'tag2', 'tag3'])
expect(tagIds).toStrictEqual([1, 2, 3])
})
})
})
which tests this function:
// src/quotes/tags.service.ts
// ...
export const upsertTags = async (tags: string[]) => {
return await prisma.$transaction(async tx => {
// ...
})
}
// src/quotes/tags.service.ts
// ...
export const upsertTags = async (tags: string[]) => {
return await prisma.$transaction(async tx => {
// ...
})
}
But I don't get what this is supposed to test. upsertTags is one big transaction. So when you mock a transaction's return value and later test the mock, how does that test the behavior of the function in any way?
1 Reply
Prisma AI Help
You've gone full bot mode! Let me fetch that AI answer for you ASAP. You're asking a great question about the purpose of this unit test for Prisma transactions. This particular test is actually quite limited in what it's testing. It's only verifying that the upsertTags function returns whatever the transaction returns. As you correctly observed, by mocking the transaction's return value and then just checking that the function returns that same value, you're not testing the actual logic inside the transaction. The blog post is starting with a very basic test that only validates the "contract" of the function - that it should return tag IDs. This is just the first of several tests for this function. Later tests in the article go deeper by testing the actual behavior inside the transaction. For properly testing transactions, you'd want to: 1. Mock the individual Prisma operations inside the transaction 2. Test the business logic that happens within the transaction The blog post actually covers a more comprehensive approach to testing transactions in Part 1 of the series, where it explains:
"To test an interactive transaction with important business logic, you may [...] mock the $transaction function's implementation [...] so that you can provide the mocked Prisma Client to the interactive transaction function rather than the actual client instance."
This allows you to test what happens inside the transaction by mocking the individual database operations and verifying the business logic works correctly. The subsequent tests in the article do exactly this - they test specific behaviors like "only creates tags that don't already exist" and "gives new tags a random color" by mocking the internal Prisma operations. ✅ Great, thanks for letting us know that this solved your question!

Did you find this page helpful?