From b162750a67a37a84cdca6f31fd974a41e4992bb9 Mon Sep 17 00:00:00 2001 From: David Gwilliam Date: Sat, 16 May 2026 20:03:10 -0700 Subject: [PATCH] fix: remove permissions module mock from tournament-update tests to prevent test pollution tournament-update.test.ts was mocking the entire @/lib/permissions module, which replaced canManageTournament globally and caused permissions.test.ts to fail when run in the same process. Instead of mocking permissions, mock its dependencies (auth-simple and prisma) so canManageTournament runs through naturally. --- src/__tests__/unit/tournament-update.test.ts | 35 ++++++++++++++------ 1 file changed, 24 insertions(+), 11 deletions(-) diff --git a/src/__tests__/unit/tournament-update.test.ts b/src/__tests__/unit/tournament-update.test.ts index 2f4aa06..48a21a1 100644 --- a/src/__tests__/unit/tournament-update.test.ts +++ b/src/__tests__/unit/tournament-update.test.ts @@ -8,12 +8,32 @@ import { describe, it, expect, mock, beforeEach,} from 'bun:test'; // Create mock functions at module level const eventFindUniqueMock = mock(async () => ({})); const eventUpdateMock = mock(async () => ({})); -const canManageTournamentMock = mock(async () => ({ allowed: true })); -const canDeleteTournamentMock = mock(async () => ({ allowed: true })); +const userFindUniqueMock = mock(async () => ({ + id: 'admin-1', + email: 'admin@example.com', + role: 'club_admin', + emailVerified: false, + name: null, + image: null, + playerId: null, + createdAt: new Date(), + updatedAt: new Date(), +})); -// Mock prisma first +// Mock auth-simple to return a valid session +mock.module('@/lib/auth-simple', () => ({ + getSession: mock(async () => ({ + user: { id: 'admin-1', email: 'admin@example.com' }, + session: { token: 'test', expiresAt: new Date() } + })), +})); + +// Mock prisma with user and event mock.module('@/lib/prisma', () => ({ prisma: { + user: { + findUnique: userFindUniqueMock, + }, event: { findUnique: eventFindUniqueMock, update: eventUpdateMock, @@ -21,12 +41,6 @@ mock.module('@/lib/prisma', () => ({ }, })); -// Mock the permissions module -mock.module('@/lib/permissions', () => ({ - canManageTournament: canManageTournamentMock, - canDeleteTournament: canDeleteTournamentMock, -})); - // Import the route handler after mocking import { PUT } from '@/app/api/tournaments/[id]/route'; import { prisma } from '@/lib/prisma'; @@ -36,8 +50,7 @@ describe('Tournament Update API', () => { // Clear all mock history before each test eventFindUniqueMock.mockClear(); eventUpdateMock.mockClear(); - canManageTournamentMock.mockClear(); - canDeleteTournamentMock.mockClear(); + userFindUniqueMock.mockClear(); }); it('should update allowTies field when provided', async () => {