From 07a9787570155780d661a222b7636ceeb712cfee Mon Sep 17 00:00:00 2001 From: Torben Sorensen Date: Mon, 29 Dec 2025 19:44:25 -0800 Subject: [PATCH] fix unit tests --- src/middleware/errorHandler.test.ts | 5 +++-- src/routes/health.routes.test.ts | 18 ++++++++++-------- src/services/queueService.server.test.ts | 5 ++++- src/tests/setup/tests-setup-unit.ts | 17 ++++++----------- 4 files changed, 23 insertions(+), 22 deletions(-) diff --git a/src/middleware/errorHandler.test.ts b/src/middleware/errorHandler.test.ts index f2321c07..f20b43fe 100644 --- a/src/middleware/errorHandler.test.ts +++ b/src/middleware/errorHandler.test.ts @@ -113,13 +113,14 @@ describe('errorHandler Middleware', () => { expect(response.body.message).toBe('A generic server error occurred.'); expect(response.body.stack).toBeDefined(); expect(response.body.errorId).toEqual(expect.any(String)); + console.log('[DEBUG] errorHandler.test.ts: Received 500 error response with ID:', response.body.errorId); expect(mockLogger.error).toHaveBeenCalledWith( expect.objectContaining({ err: expect.any(Error), errorId: expect.any(String), req: expect.objectContaining({ method: 'GET', url: '/generic-error' }), }), - expect.stringMatching(/Unhandled API Error \(ID: \w+\)/), + expect.stringMatching(/Unhandled API Error \(ID: [\w-]+\)/), ); expect(consoleErrorSpy).toHaveBeenCalledWith( expect.stringMatching(/--- \[TEST\] UNHANDLED ERROR \(ID: \w+\) ---/), @@ -226,7 +227,7 @@ describe('errorHandler Middleware', () => { errorId: expect.any(String), req: expect.objectContaining({ method: 'GET', url: '/db-error-500' }), }), - expect.stringMatching(/Unhandled API Error \(ID: \w+\)/), + expect.stringMatching(/Unhandled API Error \(ID: [\w-]+\)/), ); expect(consoleErrorSpy).toHaveBeenCalledWith( expect.stringMatching(/--- \[TEST\] UNHANDLED ERROR \(ID: \w+\) ---/), diff --git a/src/routes/health.routes.test.ts b/src/routes/health.routes.test.ts index 3b124c18..4cd348d9 100644 --- a/src/routes/health.routes.test.ts +++ b/src/routes/health.routes.test.ts @@ -164,11 +164,12 @@ describe('Health Routes (/api/health)', () => { expect(response.body.message).toBe('DB connection failed'); // This is the message from the original error expect(response.body.stack).toBeDefined(); expect(response.body.errorId).toEqual(expect.any(String)); + console.log('[DEBUG] health.routes.test.ts: Verifying logger.error for DB schema check failure'); expect(mockLogger.error).toHaveBeenCalledWith( expect.objectContaining({ err: expect.any(Error), }), - expect.stringMatching(/Unhandled API Error \(ID: \w+\)/), + expect.stringMatching(/Unhandled API Error \(ID: [\w-]+\)/), ); }); @@ -186,7 +187,7 @@ describe('Health Routes (/api/health)', () => { expect.objectContaining({ err: expect.objectContaining({ message: 'DB connection failed' }), }), - expect.stringMatching(/Unhandled API Error \(ID: \w+\)/), + expect.stringMatching(/Unhandled API Error \(ID: [\w-]+\)/), ); }); }); @@ -220,7 +221,7 @@ describe('Health Routes (/api/health)', () => { expect.objectContaining({ err: expect.any(Error), }), - expect.stringMatching(/Unhandled API Error \(ID: \w+\)/), + expect.stringMatching(/Unhandled API Error \(ID: [\w-]+\)/), ); }); @@ -239,7 +240,7 @@ describe('Health Routes (/api/health)', () => { expect.objectContaining({ err: expect.any(Error), }), - expect.stringMatching(/Unhandled API Error \(ID: \w+\)/), + expect.stringMatching(/Unhandled API Error \(ID: [\w-]+\)/), ); }); }); @@ -300,7 +301,7 @@ describe('Health Routes (/api/health)', () => { expect.objectContaining({ err: expect.any(Error), }), - expect.stringMatching(/Unhandled API Error \(ID: \w+\)/), + expect.stringMatching(/Unhandled API Error \(ID: [\w-]+\)/), ); }); @@ -321,7 +322,7 @@ describe('Health Routes (/api/health)', () => { expect.objectContaining({ err: expect.objectContaining({ message: 'Pool is not initialized' }), }), - expect.stringMatching(/Unhandled API Error \(ID: \w+\)/), + expect.stringMatching(/Unhandled API Error \(ID: [\w-]+\)/), ); }); @@ -336,11 +337,12 @@ describe('Health Routes (/api/health)', () => { expect(response.body.message).toBe('Connection timed out'); expect(response.body.stack).toBeDefined(); expect(response.body.errorId).toEqual(expect.any(String)); + console.log('[DEBUG] health.routes.test.ts: Checking if logger.error was called with the correct pattern'); expect(mockLogger.error).toHaveBeenCalledWith( expect.objectContaining({ err: expect.any(Error), }), - expect.stringMatching(/Unhandled API Error \(ID: \w+\)/), + expect.stringMatching(/Unhandled API Error \(ID: [\w-]+\)/), ); }); @@ -357,7 +359,7 @@ describe('Health Routes (/api/health)', () => { expect.objectContaining({ err: expect.any(Error), }), - expect.stringMatching(/Unhandled API Error \(ID: \w+\)/), + expect.stringMatching(/Unhandled API Error \(ID: [\w-]+\)/), ); }); }); diff --git a/src/services/queueService.server.test.ts b/src/services/queueService.server.test.ts index 5f30f342..300c8e7b 100644 --- a/src/services/queueService.server.test.ts +++ b/src/services/queueService.server.test.ts @@ -190,7 +190,10 @@ describe('Worker Service Lifecycle', () => { }); afterEach(() => { - processExitSpy.mockRestore(); + if (processExitSpy && typeof processExitSpy.mockRestore === 'function') { + console.log('[DEBUG] queueService.server.test.ts: Restoring process.exit spy'); + processExitSpy.mockRestore(); + } }); it('should close all workers, queues, the redis connection, and exit the process', async () => { diff --git a/src/tests/setup/tests-setup-unit.ts b/src/tests/setup/tests-setup-unit.ts index 1a093d03..7f80456d 100644 --- a/src/tests/setup/tests-setup-unit.ts +++ b/src/tests/setup/tests-setup-unit.ts @@ -142,15 +142,6 @@ vi.mock('util', async (importOriginal) => { }; }); -// Mock 'child_process' using the robust `importOriginal` pattern. -// This is needed because some services (systemService, workers.server) import it at the top level. -vi.mock('child_process', () => { - return { - __esModule: true, // Handle CJS/ESM interop - exec: vi.fn(), - }; -}); - // Mock 'jsonwebtoken'. The `default` key is crucial because the code under test // uses `import jwt from 'jsonwebtoken'`, which imports the default export. vi.mock('jsonwebtoken', () => ({ @@ -170,9 +161,13 @@ vi.mock('bcrypt'); vi.mock('crypto', () => ({ default: { randomBytes: vi.fn().mockReturnValue({ - toString: vi.fn().mockReturnValue('mocked-random-string'), + toString: vi.fn().mockImplementation((encoding) => { + const id = 'mocked_random_id'; + console.log(`[DEBUG] tests-setup-unit.ts: crypto.randomBytes mock returning "${id}" for encoding "${encoding}"`); + return id; + }), }), - randomUUID: vi.fn().mockReturnValue('mocked-random-string'), + randomUUID: vi.fn().mockReturnValue('mocked_random_id'), }, }));