Refactor tests and improve error handling across various services
All checks were successful
Deploy to Test Environment / deploy-to-test (push) Successful in 10m38s

- Updated `useAuth` tests to use async functions for JSON responses to avoid promise resolution issues.
- Changed `AdminBrandManager` tests to use `mockImplementation` for consistent mock behavior.
- Enhanced `ProfileManager.Authenticated` tests to ensure proper error handling and assertions for partial updates.
- Modified `SystemCheck` tests to prevent memory leaks by using `mockImplementation` for API calls.
- Improved error handling in `ai.routes.ts` by refining validation schemas and adding error extraction utility.
- Updated `auth.routes.test.ts` to inject mock logger for better error tracking.
- Refined `flyer.routes.ts` to ensure proper validation and error handling for flyer ID parameters.
- Enhanced `admin.db.ts` to ensure specific errors are re-thrown for better error management.
- Updated `budget.db.test.ts` to improve mock behavior and ensure accurate assertions.
- Refined `flyer.db.ts` to improve error handling for race conditions during store creation.
- Enhanced `notification.db.test.ts` to ensure specific error types are tested correctly.
- Updated `recipe.db.test.ts` to ensure proper handling of not found errors.
- Improved `user.db.ts` to ensure consistent error handling for user retrieval.
- Enhanced `flyerProcessingService.server.test.ts` to ensure accurate assertions on transformed data.
- Updated `logger.server.ts` to disable transport in test environments to prevent issues.
- Refined `queueService.workers.test.ts` to ensure accurate mocking of email service.
- Improved `userService.test.ts` to ensure proper mock implementations for repository classes.
- Enhanced `checksum.test.ts` to ensure reliable file content creation in tests.
- Updated `pdfConverter.test.ts` to reset shared state objects and mock implementations before each test.
This commit is contained in:
2025-12-15 16:40:13 -08:00
parent 0c590675b3
commit d5f185ad99
32 changed files with 430 additions and 182 deletions

View File

@@ -8,7 +8,11 @@ import * as apiClient from '../services/apiClient';
import type { User, UserProfile } from '../types';
// Mock the dependencies
vi.mock('../services/apiClient');
vi.mock('../services/apiClient', () => ({
// Mock other functions if needed
getAuthenticatedUserProfile: vi.fn(),
}));
vi.mock('../services/logger.client', () => ({
logger: {
info: vi.fn(),
@@ -90,8 +94,8 @@ describe('useAuth Hook and AuthProvider', () => {
localStorageMock.setItem('authToken', 'valid-token');
mockedApiClient.getAuthenticatedUserProfile.mockResolvedValue({
ok: true,
json: () => Promise.resolve(mockProfile),
} as Response);
json: async () => mockProfile,
} as unknown as Response);
const { result } = renderHook(() => useAuth(), { wrapper });
@@ -127,11 +131,10 @@ describe('useAuth Hook and AuthProvider', () => {
describe('login function', () => {
it('sets token, fetches profile, and updates state on successful login', async () => {
// Mock the API response for the login call
mockedApiClient.getAuthenticatedUserProfile.mockResolvedValue({
ok: true,
json: () => Promise.resolve(mockProfile),
} as Response);
json: async () => mockProfile,
} as unknown as Response);
const { result } = renderHook(() => useAuth(), { wrapper });
@@ -181,8 +184,8 @@ describe('useAuth Hook and AuthProvider', () => {
localStorageMock.setItem('authToken', 'valid-token');
mockedApiClient.getAuthenticatedUserProfile.mockResolvedValue({
ok: true,
json: () => Promise.resolve(mockProfile),
} as Response);
json: async () => mockProfile,
} as unknown as Response);
const { result } = renderHook(() => useAuth(), { wrapper });
await waitFor(() => expect(result.current.authStatus).toBe('AUTHENTICATED'));
@@ -206,8 +209,8 @@ describe('useAuth Hook and AuthProvider', () => {
localStorageMock.setItem('authToken', 'valid-token');
mockedApiClient.getAuthenticatedUserProfile.mockResolvedValue({
ok: true,
json: () => Promise.resolve(mockProfile),
} as Response);
json: async () => mockProfile,
} as unknown as Response);
const { result } = renderHook(() => useAuth(), { wrapper });
await waitFor(() => expect(result.current.authStatus).toBe('AUTHENTICATED'));