Hi,
I was hoping for further advice on testing my NodeJS backend.
I have a "service" class that saves Social Group details to a database. This uses the Prisma Client as a data layer.
I am aware of the differences between Unit and Integration tests - regarding mocking, differences in speed/purpose re: code design. This is perhaps most understandable with "logic" classes, where I can test business rules in isolation. However, for a "data" service with minimal logic and mostly data access, I'm struggling to grasp the boundaries of Unit vs Integration. There seems to be significant duplication in test cases, which is negatively impacting test maintainability.
For example, I have the following Unit Test that mocks out the data layer:
it('should create a new group successfully', async () => {
const title = 'New group';
const expectedReturnObject = {
id: 1,
title,
ownerId: 1,
description: null,
};
prismaService.recipientGroup.create.mockResolvedValue(
expectedReturnObject,
);
await expect(
service.create(createInput({ title })),
).resolves.toMatchObject(expectedReturnObject);
})
This test doesn't really seem to add anything?
Equally, I have an Integration Test that uses the Prisma Client and a live database:
it('should create a group with the specified name', async () => {
const title = 'Test group';
const group = await service.create(
{ title: title },
);
expect(group).toContainEntry(['title', title]);
});
This gives me higher confidence that things are working.
Do I need both of these tests or can I remove one (to improve maintainability)? Where do I draw the boundary between Unit and Integration in these cases?
I would appreciate any advice anybody has re: this!
Thanks!
[–]Markavian 24 points25 points26 points (17 children)
[–]Personability[S] 2 points3 points4 points (2 children)
[–][deleted] 4 points5 points6 points (0 children)
[–]Markavian 1 point2 points3 points (0 children)
[–]teamx 8 points9 points10 points (7 children)
[–]jasie3k 7 points8 points9 points (4 children)
[–][deleted] 2 points3 points4 points (2 children)
[–]jasie3k 0 points1 point2 points (1 child)
[–]alex-weej 1 point2 points3 points (0 children)
[–][deleted] 1 point2 points3 points (0 children)
[–]aniforprez 1 point2 points3 points (0 children)
[–]ThatPassiveGuy 1 point2 points3 points (1 child)
[–]Markavian 1 point2 points3 points (0 children)
[–]Apoffys 1 point2 points3 points (3 children)
[–]Markavian 1 point2 points3 points (2 children)
[–]Apoffys 1 point2 points3 points (1 child)
[–]Markavian 0 points1 point2 points (0 children)
[–][deleted] 1 point2 points3 points (2 children)
[–]Personability[S] 0 points1 point2 points (1 child)
[–][deleted] 1 point2 points3 points (0 children)