PrismaP
Prisma9mo ago
3 replies
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])
    })
  })
})


which tests this function:
// 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?
Was this page helpful?