feat(tests): update test configurations and improve mocking setup

This commit is contained in:
Jose Selesan
2026-06-03 09:35:04 -03:00
parent c1b47674c8
commit 473686528e
8 changed files with 27 additions and 19 deletions

View File

@@ -4,7 +4,7 @@
"dev": "bun run --hot src/server.ts",
"start": "bun src/server.ts",
"build": "tsc -b",
"test": "bun test",
"test": "bun test --preload ./test/support/prisma.mock.ts ./test",
"lint": "biome check .",
"lint:fix": "biome check --write .",
"format": "biome format --write .",

View File

@@ -1,6 +1,5 @@
import { beforeEach, expect, mock, test } from 'bun:test';
import { createInviteComplexUserHandler } from '@/modules/complex/handlers/invite-complex-user.handler';
import type { InviteComplexUserResponse } from '@repo/api-contract';
const inviteComplexUserMock = mock(async () => undefined as unknown as InviteComplexUserResponse);
@@ -15,6 +14,10 @@ class MockComplexMembersError extends Error {
}
}
const { createInviteComplexUserHandler } = await import(
'@/modules/complex/handlers/invite-complex-user.handler'
);
const inviteComplexUserHandler = createInviteComplexUserHandler({
inviteComplexUser: inviteComplexUserMock,
ComplexMembersError: MockComplexMembersError,
@@ -59,7 +62,6 @@ function createContext(input: {
beforeEach(() => {
inviteComplexUserMock.mockReset();
mock.clearAllMocks();
});
test('returns 201 with the service result', async () => {

View File

@@ -1,14 +1,15 @@
import { beforeEach, expect, mock, test } from 'bun:test';
import { beforeEach, expect, test } from 'bun:test';
import {
ComplexMembersError,
inviteComplexUser,
} from '@/modules/complex/services/complex-members.service';
import { prismaMock, sendMailMock, transactionMock } from '../support/prisma.mock';
const { ComplexMembersError, inviteComplexUser } = await import(
'@/modules/complex/services/complex-members.service'
);
beforeEach(() => {
prismaMock._reset();
mock.clearAllMocks();
sendMailMock.mockClear();
transactionMock.mockClear();
});
test('creates a pending invitation and sends the email', async () => {
@@ -21,10 +22,10 @@ test('creates a pending invitation and sends the email', async () => {
complexId: 'complex-1',
email: 'new.member@example.com',
tokenHash: 'token-hash',
expiresAt: new Date('2026-04-29T00:00:00.000Z'),
expiresAt: new Date('2027-01-01T00:00:00.000Z'),
acceptedAt: null,
revokedAt: null,
createdAt: new Date('2026-04-22T00:00:00.000Z'),
createdAt: new Date('2026-12-01T00:00:00.000Z'),
} as never);
const result = await inviteComplexUser('admin-1', 'complex-1', {

View File

@@ -49,7 +49,6 @@ function createContext(body: unknown) {
beforeEach(() => {
createSportMock.mockReset();
mock.clearAllMocks();
});
test('returns 201 with the created sport', async () => {

View File

@@ -6,7 +6,6 @@ import type { PrismaClient } from '@/generated/prisma/client';
import type { PrismaClientMock } from 'bun-mock-prisma';
const prismaMock = createPrismaMock<PrismaClient>() as PrismaClientMock<PrismaClient>;
const uuidV7Mock = mock(() => '00000000-0000-4000-8000-000000000001');
mock.module('@/lib/prisma', () => ({
__esModule: true,
@@ -15,10 +14,7 @@ mock.module('@/lib/prisma', () => ({
},
}));
mock.module('uuid', () => ({
__esModule: true,
v7: uuidV7Mock,
}));
const { uuidV7Mock } = await import('../support/prisma.mock');
const { createSport, getSportById, listSports, updateSport } = await import(
'@/modules/sport/services/sport.service'
@@ -26,7 +22,7 @@ const { createSport, getSportById, listSports, updateSport } = await import(
beforeEach(() => {
prismaMock._reset();
mock.clearAllMocks();
uuidV7Mock.mockClear();
});
test('listSports returns sports ordered by name', async () => {

View File

@@ -33,3 +33,10 @@ mock.module('@/lib/mailer', () => ({
__esModule: true,
sendMail: sendMailMock,
}));
export const uuidV7Mock = mock(() => '00000000-0000-4000-8000-000000000001');
mock.module('uuid', () => ({
__esModule: true,
v7: uuidV7Mock,
}));

2
bunfig.toml Normal file
View File

@@ -0,0 +1,2 @@
[test]
preload = ["./apps/backend/test/support/prisma.mock.ts"]

View File

@@ -19,7 +19,8 @@
"lint:frontend": "bun --filter frontend lint",
"lint:frontend:fix": "bun --filter frontend lint:fix",
"lint:backend": "bun --filter backend lint",
"lint:backend:fix": "bun --filter backend lint:fix"
"lint:backend:fix": "bun --filter backend lint:fix",
"test": "bun --filter backend test"
},
"devDependencies": {
"@biomejs/biome": "^1.9.4",