fix unit tests
All checks were successful
Deploy to Test Environment / deploy-to-test (push) Successful in 15m41s

This commit is contained in:
2025-12-30 01:42:03 -08:00
parent ba9228c9cb
commit 1201fe4d3c
5 changed files with 379 additions and 27 deletions

View File

@@ -32,10 +32,25 @@ vi.mock('../services/logger.server', () => ({
// 4. Mock multer to prevent it from doing anything during import.
vi.mock('multer', () => {
const diskStorage = vi.fn((options) => options);
// A more realistic mock for MulterError that maps error codes to messages,
// similar to how the actual multer library works.
class MulterError extends Error {
constructor(public code: string) {
super(code);
code: string;
field?: string;
constructor(code: string, field?: string) {
const messages: { [key: string]: string } = {
LIMIT_FILE_SIZE: 'File too large',
LIMIT_UNEXPECTED_FILE: 'Unexpected file',
// Add other codes as needed for tests
};
const message = messages[code] || code;
super(message);
this.code = code;
this.name = 'MulterError';
if (field) {
this.field = field;
}
}
}
const multer = vi.fn(() => ({
@@ -106,6 +121,7 @@ describe('createUploadMiddleware', () => {
describe('Avatar Storage', () => {
it('should generate a unique filename for an authenticated user', () => {
process.env.NODE_ENV = 'production';
createUploadMiddleware({ storageType: 'avatar' });
const storageOptions = vi.mocked(multer.diskStorage).mock.calls[0][0];
const cb = vi.fn();
@@ -161,7 +177,7 @@ describe('createUploadMiddleware', () => {
expect(cb).toHaveBeenCalledWith(
null,
expect.stringMatching(/^flyerFile-\d+-\d+-my-flyer-special.pdf$/),
expect.stringMatching(/^flyerFile-\d+-\d+-my-flyer-special\.pdf$/i),
);
});