From 9f50d7d942eaeec8391cebdd18fa3f027a53258f Mon Sep 17 00:00:00 2001 From: Torben Sorensen Date: Tue, 9 Dec 2025 14:23:58 -0800 Subject: [PATCH] fix logging tests --- src/services/db/user.db.test.ts | 24 ++++++++++++++++++++---- src/utils/imageProcessor.test.ts | 8 ++------ 2 files changed, 22 insertions(+), 10 deletions(-) diff --git a/src/services/db/user.db.test.ts b/src/services/db/user.db.test.ts index d11c2815..afd73a81 100644 --- a/src/services/db/user.db.test.ts +++ b/src/services/db/user.db.test.ts @@ -6,7 +6,17 @@ // // --- END FIX REGISTRY --- // src/services/db/user.db.test.ts -import { describe, it, expect, vi, beforeEach } from 'vitest'; +import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'; + +// Mock the logger to prevent stderr noise during tests +vi.mock('../logger.server', () => ({ + logger: { + debug: vi.fn(), + info: vi.fn(), + warn: vi.fn(), + error: vi.fn(), + }, +})); // Un-mock the module we are testing to ensure we use the real implementation. vi.unmock('./user.db'); @@ -18,15 +28,16 @@ import { UniqueConstraintError, ForeignKeyConstraintError } from './errors.db'; import type { Profile, ActivityLogItem, SearchQuery } from '../../types'; // Mock other db services that are used by functions in user.db.ts +// Update mocks to put methods on prototype so spyOn works in exportUserData tests vi.mock('./shopping.db', () => ({ ShoppingRepository: class { - getShoppingLists = vi.fn(); - createShoppingList = vi.fn(); // FIX: Add missing mock for createShoppingList. + getShoppingLists() { return Promise.resolve([]); } + createShoppingList() { return Promise.resolve({}); } }, })); vi.mock('./personalization.db', () => ({ PersonalizationRepository: class { - getWatchedItems = vi.fn(); + getWatchedItems() { return Promise.resolve([]); } }, })); @@ -34,6 +45,11 @@ describe('User DB Service', () => { // Instantiate the repository with the mock pool for each test let userRepo: UserRepository; + // Restore mocks after each test to ensure spies on prototypes (like in exportUserData) don't leak + afterEach(() => { + vi.restoreAllMocks(); + }); + beforeEach(() => { vi.clearAllMocks(); userRepo = new UserRepository(mockPoolInstance as any); diff --git a/src/utils/imageProcessor.test.ts b/src/utils/imageProcessor.test.ts index b98e2f00..366d39ee 100644 --- a/src/utils/imageProcessor.test.ts +++ b/src/utils/imageProcessor.test.ts @@ -11,14 +11,10 @@ const mocks = vi.hoisted(() => { // Mock the sharp function and attach static properties required by the implementation const sharp = vi.fn(() => sharpInstance); - Object.assign(sharp, { - fit: { - cover: 'cover', - }, - }); + Object.assign(sharp, { fit: { cover: 'cover' } }); return { - sharp, + sharp: sharp, resize, webp, toFile,