SAF
SAF
PPrisma
Created by SAF on 4/30/2025 in #help-and-questions
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?
4 replies