Refactor test setup and improve mock implementations
Some checks are pending
Deploy to Test Environment / deploy-to-test (push) Has started running

- Introduced `createTestApp` utility to standardize Express app creation for tests, including JSON parsing, mock logger, and error handling.
- Updated all route tests to utilize `createTestApp`, enhancing consistency and reducing boilerplate code.
- Implemented `mockLogger` for cleaner test output and easier logging mock management.
- Adjusted passport mocks to ensure proper user authentication simulation across tests.
- Enhanced type safety by augmenting Express Request interface with `req.log` and `req.user` properties.
- Removed redundant code and improved readability in various test files.
This commit is contained in:
2025-12-14 14:47:19 -08:00
parent edb0f8a38c
commit eb0e183f61
31 changed files with 244 additions and 419 deletions

View File

@@ -14,8 +14,8 @@ export const errorHandler = (err: HttpError, req: Request, res: Response, next:
return next(err);
}
// Use the request-scoped logger if available, otherwise fall back to the global logger.
const log = req.log || logger;
// The pino-http middleware guarantees that `req.log` will be available.
const log = req.log;
// --- 1. Determine Final Status Code and Message ---
let statusCode = err.status ?? 500;
@@ -55,10 +55,15 @@ export const errorHandler = (err: HttpError, req: Request, res: Response, next:
log.error({ err, errorId, req: { method: req.method, url: req.originalUrl, headers: req.headers, body: req.body } }, `Unhandled API Error (ID: ${errorId})`);
} else {
// For 4xx errors, log at a lower level (e.g., 'warn') to avoid flooding error trackers.
// The request-scoped logger already contains the necessary context.
// We log the error itself to capture its message and properties.
// No need to log the full request for client errors unless desired for debugging.
log.warn({ err }, `Client Error: ${statusCode} on ${req.method} ${req.path}`);
// We include the validation errors in the log context if they exist.
log.warn(
{
err,
validationErrors: errors, // Add validation issues to the log object
statusCode,
},
`Client Error on ${req.method} ${req.path}: ${message}`
);
}
// --- TEST ENVIRONMENT DEBUGGING ---